    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Java Style &amp; Idioms</title>
        <link>https://members.accu.org/index.php/articles/771</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + CVu Journal Vol 11, #2 - Feb 1999</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c77/">CVu</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c133/">112</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-133/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+133/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Java Style &amp; Idioms</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 03 February 1999 13:15:29 +00:00 or Wed, 03 February 1999 13:15:29 +00:00</p>
<p><strong>Summary:</strong>&nbsp;<p>There is no one &quot;true&quot; way to layout Java code. There are two common styles.</p></p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e20" id="d0e20"></a>Java Idioms :
code layout</h2>
</div>
<p><span class="bold"><b>Name</b></span>: <span class=
"emphasis"><em>code layout</em></span></p>
<p><span class="bold"><b>Rationale</b></span>: There is no one
&quot;true&quot; way to layout Java code. There are two common styles.</p>
<p><span class="bold"><b>Advantages</b></span>: A consistent layout
is useful for maintenance purposes. It can also make intentions
clear.</p>
<p><span class="bold"><b>Disadvantages</b></span>: Can be extremely
religious!</p>
<p><span class="bold"><b>Description</b></span>:</p>
<p>The most idiomatic Java code layout style is that used by
JavaSoft. It is generally used in the literature. This layout style
typically has the form:</p>
<pre class="programlisting">
    public class Test {
        public static void main(String[] args) {
            try {
                if (args.length == 0) {
                    System.err.println(&quot;Usage: Test args&quot;);
                } else {
                    for (int i=0 ; i &lt; args.length ; ++i) {
                         System.out.println(&quot;[&quot;+i+&quot;]= &quot;+args[i]);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
</pre>
<p>I think this style is fairly similar to &quot;classic&quot; K&amp;R. I
don't like this style. I find it slightly &quot;cramped&quot;. However, it
does allow you to get more code on the screen or printed page. I
prefer the matching braces idiom. So the above example with
matching braces would like so:</p>
<pre class="programlisting">
    public class Test
    {
        public static void main(String[] args)
        {
            try
            {
                if (args.length == 0)
                {
                    System.err.println(&quot;Usage: Test args&quot;);
                }
                else
                {
                    for (int i=0 ; i &lt; args.length ; ++i)
                    {
                         System.out.println(&quot;[&quot;+i+&quot;]= &quot;+args[i]);
                    }
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
</pre>
<p>I think this style is more pleasant to read. Obviously it
reduces the amount of code you get on the screen or printed page -
but this does allow one more space to make notes! Another common
habit that I have come across is the use of tabs for each indent
level. Developers then define the size of a tab to give the
indentation they prefer (e.g. 2,3,4,...). In most development
environments today this should be a perfectly acceptable
solution.</p>
<p>The idiom is that you DON'T have to layout your code in the
JavaSoft manner (unless you work for them, of course &#61514;). The
only real requirement is that your layout is consistent. I just
happen to like my code to be &quot;pretty&quot; too :-)</p>
<p>Hopefully, Java indent/pretty printers will mean that everybody
can have code in their favourite style. A really cool approach
would be to have an indent/pretty printer tool between the
IDE/editor and the source control system.</p>
<p>On checking out the IDE/editor receives the code in the
programmer's preferred style. On checking in the code is formatted
to the &quot;company&quot; style so that all differences are consistent. Then
everybody would happy!</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e61" id="d0e61"></a>Java Idioms :
finalizers are evil</h2>
</div>
<p><span class="bold"><b>Name</b></span>: <span class=
"emphasis"><em>finalizers are evil</em></span></p>
<p><span class="bold"><b>Rationale</b></span>: Avoid finalizers if
you can because they might not do what you expected or hoped
for!</p>
<p><span class="bold"><b>Advantages</b></span>: You'll sleep easier
at night.</p>
<p><span class="bold"><b>Disadvantages</b></span>: None.</p>
<p><span class="bold"><b>Description</b></span>:</p>
<p>Plenty of Java programmers think that finalizers are Java's
equivalent of C++ destructors. It is a bit of an eye opener to
discover that this is (generally) not the case!</p>
<p>If you read the Java Language Specification section 20.1.11 you
get the idea that using a finalizer for object cleanup is not
unusual. It states &quot;the usual purpose of <tt class=
"methodname">finalize</tt> is to perform cleanup actions before the
object is irrevocably discarded&quot;.</p>
<p>So a typical class attempting to use this behaviour would look
like:</p>
<pre class="programlisting">
    public class SomeClass
    {
        public SomeClass()
        {
            // constructor
        }
        
        protected void finalize() throws Throwable
        {
            // destructor!?
        }
    }
</pre>
<p>However, the default behaviour for the JDK1.1 JVM is NOT to run
finalizers on exit of the VM. So it is entirely possible that one's
application might perform a complete run without a finalizer having
been executed! In JDK1.1 the class method <tt class=
"methodname">runFinalizersOnExit</tt> was added to <tt class=
"classname">java.lang.System</tt> to allow the programmer to
specify that finalizers should be run on exit of the VM. So does
this mean that it is now safe to use finalizers? Nope! Because even
if you do <tt class="literal">System.runFinalizersOnExit(true)</tt>
at the start of your application and then exit the application with
a non zero status (e.g. <tt class="literal">System.exit(1)</tt>) at
some point during the execution then the finalizers are not
executed!</p>
<p>The problem becomes slightly worse under JDK1.2! Under this JDK
version <tt class="methodname">runFinalizersOnExit</tt> has been
deprecated. This is due to problems arising when finalizers were
being called on live objects when the JVM exited [BUG:4119554]!
Programmers have naturally assumed that finalizers would be called
on a &quot;dead&quot; object during garbage collection.</p>
<p>So what's the idiom? Realising that finalizers are NOT
destructors! Trying to avoid a dependency on finalizers that may
never execute. Having a &quot;close&quot; method (as do <tt class=
"classname">InputStream</tt>/<tt class=
"classname">OutputStream</tt> or JDBC <tt class=
"classname">ResultSet</tt>) is a way of avoiding an unnecessary
dependence on finalizer methods. It is likely that JavaSoft will
introduce an &quot;<tt class="methodname">onExit</tt>&quot; mechanism in a
future JDK (i.e. post JDK 1.2!). This will then fix the calling
finalizers on live objects problem.</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
