Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #26263

Re: unnecessary code in Oracle example?

From markspace <markspace@nospam.nospam>
Newsgroups comp.lang.java.programmer
Subject Re: unnecessary code in Oracle example?
Date 2013-10-09 14:56 -0700
Organization A noiseless patient Spider
Message-ID <l34jek$782$1@dont-email.me> (permalink)
References <ydnr4bujt72.fsf@shell.xmission.com> <u6h5u.69447$yx6.64233@fx26.iad> <ydneh7ujm7q.fsf@shell.xmission.com> <l34an4$ic1$1@dont-email.me> <ydna9iijivg.fsf@shell.xmission.com>

Show all headers | View raw


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.


Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

unnecessary code in Oracle example? Jim Janney <jjanney@shell.xmission.com> - 2013-10-09 10:38 -0600
  Re: unnecessary code in Oracle example? Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-10-09 11:19 -0700
    Re: unnecessary code in Oracle example? ram@zedat.fu-berlin.de (Stefan Ram) - 2013-10-09 19:04 +0000
    Re: unnecessary code in Oracle example? Jim Janney <jjanney@shell.xmission.com> - 2013-10-09 13:08 -0600
      Re: unnecessary code in Oracle example? markspace <markspace@nospam.nospam> - 2013-10-09 12:27 -0700
        Re: unnecessary code in Oracle example? Jim Janney <jjanney@shell.xmission.com> - 2013-10-09 14:21 -0600
          Re: unnecessary code in Oracle example? markspace <markspace@nospam.nospam> - 2013-10-09 14:56 -0700
            Re: unnecessary code in Oracle example? Jim Janney <jjanney@shell.xmission.com> - 2013-10-09 20:56 -0600
              Re: unnecessary code in Oracle example? Arivald <NOSPAMarivald@interia.pl> - 2013-10-10 06:42 +0200
                Re: unnecessary code in Oracle example? Sebastian <news@seyweiler.dyndns.org> - 2013-10-10 07:47 +0200
                Re: unnecessary code in Oracle example? Arivald <arivald_@AT_interia_DOT_.pl> - 2013-10-10 10:26 +0200
                Re: unnecessary code in Oracle example? Jim Janney <jjanney@shell.xmission.com> - 2013-10-10 08:47 -0600
    Re: unnecessary code in Oracle example? Robert Klemme <shortcutter@googlemail.com> - 2013-10-09 21:11 +0200
      Re: unnecessary code in Oracle example? cuzdav@gmail.com - 2013-10-10 08:49 -0700

csiph-web