Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: markspace Newsgroups: comp.lang.java.programmer Subject: Re: unnecessary code in Oracle example? Date: Wed, 09 Oct 2013 14:56:33 -0700 Organization: A noiseless patient Spider Lines: 39 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 9 Oct 2013 21:56:36 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="9185b88a2b5381a82810209530bb8295"; logging-data="7426"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18tN3Sj3Mlqlu8/5euARWaPQ7FkEkZgxuA=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130801 Thunderbird/17.0.8 In-Reply-To: Cancel-Lock: sha1:ue8WwQuomklDCeQTstll8QbHSOc= Xref: csiph.com comp.lang.java.programmer:26263 On 10/9/2013 1:21 PM, Jim Janney wrote: > Yes, in Java 7 you can have clean syntax and clean semantics. Looks > like a win all around. > For Java 5+, with varargs you could do (not compiled): public class MyIoUtils { public static void closeAll( Closeable ... toClose ) { for( Closeable closeMe : toClose ) try { if( closeMe != null ) closeMe.close(); } catch( IOException ex ) { Logger.getLogger( "MyIoUtils" ).log( ex ); } } } and get a somewhat cleaned up try catch OutPutStream io1 = null; BufferedStream bs = null; try { io1 = new OutputStream( ... bs = new BufferedStream( io1, ... // do stuff } finally { MyIoUtils.closeAll( bs, io1 ); } Mostly I don't care for deep nesting, I think it reads poorly, and I think it can also be hard to trace out for a programmer reading the source. Giving the concept of closing a lot of objects a name like "closeAll" promotes literate programming, imo.