Difference between revisions of "Coding Convention"

From Developer Documents
Jump to navigation Jump to search
m (Created page with "==Argument Assumption== *'''All method arguments are non-null unless explicitely stated otherwise in javadoc. ''' <div style="background: #f3fff3;"> <syntaxhighlight lang="java"...")
 
m
Line 20: Line 20:
 
       */
 
       */
 
     void setValue(Object newValue);
 
     void setValue(Object newValue);
 
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
<br/>
 
</div>
 
</div>
  
 
==Trust your assumptions==
 
==Trust your assumptions==
Just write the code - you have a code of conduct, the callee can trust the caller.
+
'''You have a code of conduct - have faith.''' <br/>
 +
The callee can trust the caller.
 
<div style="background: #f3fff3;">
 
<div style="background: #f3fff3;">
 
<syntaxhighlight lang="java">     
 
<syntaxhighlight lang="java">     
Line 32: Line 33:
 
     }
 
     }
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
<br/>
 
</div>
 
</div>
  
And caller the callee.
+
And the caller the callee.
 
<div style="background: #f3fff3;">
 
<div style="background: #f3fff3;">
 
<syntaxhighlight lang="java">     
 
<syntaxhighlight lang="java">     
     System.out.println( serialiser.deserialize( is ) );
+
     System.out.println( serialiser.deserialize( is ) ); <br/>
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
<br/>
 
</div>
 
</div>
 
 
There is no need to do redundant checking, especially at run-time.  
 
There is no need to do redundant checking, especially at run-time.  
 
<div style="background: #fff3f3;">
 
<div style="background: #fff3f3;">
Line 47: Line 49:
 
         if ( is == null ) throw IllegalArgumentException("Non-null argument");
 
         if ( is == null ) throw IllegalArgumentException("Non-null argument");
 
         int  x = is.read();
 
         int  x = is.read();
     }
+
     }  
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
<br/>
 
</div>
 
</div>
 
+
Nor caller.
 
<div style="background: #fff3f3;">
 
<div style="background: #fff3f3;">
 
<syntaxhighlight lang="java">     
 
<syntaxhighlight lang="java">     
Line 56: Line 59:
 
     if ( x != null ) System.out.println( x );
 
     if ( x != null ) System.out.println( x );
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
<br/>
 
</div>
 
</div>
  
Use assertions if you must. Sometimes they improve quality and debuggability.
+
Use assertions if you must. It sometimes improve quality and debuggability.
 
<div style="background: #fffff3;">
 
<div style="background: #fffff3;">
 
<syntaxhighlight lang="java" style="background: #dfd;">     
 
<syntaxhighlight lang="java" style="background: #dfd;">     
Line 66: Line 70:
 
     }
 
     }
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
<br/>
 
</div>
 
</div>
  
 
==Return value assumption==
 
==Return value assumption==
 
All non-void methods return a non-null value unless expilitely stated otherwise in javadoc.
 
All non-void methods return a non-null value unless expilitely stated otherwise in javadoc.

Revision as of 09:55, 13 October 2010

Argument Assumption

  • All method arguments are non-null unless explicitely stated otherwise in javadoc.

<syntaxhighlight lang="java">

   /**
     * Read value from a file
     *
     * @param file 
     */
   void read(File file);
   // Non-null assumption applies here too, is is mandatory.
   void read(InputStream is);
   /**
     * Write or remove existing value.
     *
     * @param newValue new value or null</t>> to remove the existing value
     */
   void setValue(Object newValue);

</syntaxhighlight>

Trust your assumptions

You have a code of conduct - have faith.
The callee can trust the caller.

<syntaxhighlight lang="java">

   void read(InputStream is) {
       int  x = is.read();
   }

</syntaxhighlight>

And the caller the callee.

<syntaxhighlight lang="java">

   System.out.println( serialiser.deserialize( is ) ); 

</syntaxhighlight>

There is no need to do redundant checking, especially at run-time.

<syntaxhighlight lang="java">

   void read(InputStream is) {
       if ( is == null ) throw IllegalArgumentException("Non-null argument");
       int  x = is.read();
   } 

</syntaxhighlight>

Nor caller.

<syntaxhighlight lang="java">

   Object x = serializer.deserialize( is );
   if ( x != null ) System.out.println( x );

</syntaxhighlight>

Use assertions if you must. It sometimes improve quality and debuggability.

<syntaxhighlight lang="java" style="background: #dfd;">

   void read(InputStream is) {
       assert( is != null );
       int  x = is.read();
   }

</syntaxhighlight>

Return value assumption

All non-void methods return a non-null value unless expilitely stated otherwise in javadoc.