Difference between revisions of "Coding Convention"

From Developer Documents
Jump to navigation Jump to search
m
m
Line 1: Line 1:
 
==Argument Assumption==
 
==Argument Assumption==
*'''All method arguments are non-null unless explicitely stated otherwise in javadoc. '''
+
*'''All method arguments are non-null unless explicitely stated otherwise in documentation. '''
  
 
<div style="background: #f3fff3;">
 
<div style="background: #f3fff3;">
 
<syntaxhighlight lang="java">     
 
<syntaxhighlight lang="java">     
 
     /**
 
     /**
       * Read value from a file
+
       * Read the object from a file.
 
       *
 
       *
 
       * @param file  
 
       * @param file  
Line 11: Line 11:
 
     void read(File file);
 
     void read(File file);
  
     // Non-null assumption applies here too, ''is'' is mandatory.
+
     /**
 +
      * Write or remove existing value.
 +
      *
 +
      * @param newValue new value or <tt>null</t> to remove the existing value
 +
      */
 +
    void setValue(Object newValue);
 +
</syntaxhighlight>
 +
<br/>
 +
</div>
 +
<br/>
 +
 
 +
Non-null assumption applies in undocumented methods aswell. This method mustn't be called with a null argument.
 +
<div style="background: #f3fff3;">
 +
<syntaxhighlight lang="java">   
 
     void read(InputStream is);
 
     void read(InputStream is);
 +
</syntaxhighlight>
 +
<br/>
 +
</div>
 +
 +
  
 +
==Return value assumption==
 +
*'''All return values are non-null unless explicitely stated otherwise in documentation.'''<br/>
 +
 +
<div style="background: #f3fff3;">
 +
<syntaxhighlight lang="java">   
 
     /**
 
     /**
       * Write or remove existing value.
+
       * Get the value
 +
      *
 +
      * @return the value
 +
      */
 +
    Object get() throws ValueUnavailableException;
 +
 
 +
    /**
 +
      * Get possibly existing value
 
       *
 
       *
       * @param newValue new value or <tt>null</t>> to remove the existing value
+
       * @return the value is exists, otherwise <tt>null</tt>
 
       */
 
       */
     void setValue(Object newValue);
+
     Object get();
 +
</syntaxhighlight>
 +
<br/>
 +
</div>
 +
<br/>
 +
 
 +
Non-null assumption applies in undocumented methods too, there is always a non-null return value.
 +
<div style="background: #f3fff3;">
 +
<syntaxhighlight lang="java">   
 +
    Object get();
 
</syntaxhighlight>
 
</syntaxhighlight>
 
<br/>
 
<br/>
 
</div>
 
</div>
 +
 +
  
 
==Trust your assumptions==
 
==Trust your assumptions==
'''You have a code of conduct - have faith.''' <br/>
+
*'''You have a code of conduct - give it a chance.''' <br/>
 
The callee can trust the caller.
 
The callee can trust the caller.
 
<div style="background: #f3fff3;">
 
<div style="background: #f3fff3;">

Revision as of 10:03, 13 October 2010

Argument Assumption

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

<syntaxhighlight lang="java">

   /**
     * Read the object from a file.
     *
     * @param file 
     */
   void read(File file);
   /**
     * Write or remove existing value.
     *
     * @param newValue new value or null</t> to remove the existing value
     */
   void setValue(Object newValue);

</syntaxhighlight>


Non-null assumption applies in undocumented methods aswell. This method mustn't be called with a null argument.

<syntaxhighlight lang="java">

   void read(InputStream is);

</syntaxhighlight>


Return value assumption

  • All return values are non-null unless explicitely stated otherwise in documentation.

<syntaxhighlight lang="java">

   /**
     * Get the value
     *
     * @return the value
     */
   Object get() throws ValueUnavailableException;
   /**
     * Get possibly existing value
     *
     * @return the value is exists, otherwise null
     */
   Object get();

</syntaxhighlight>


Non-null assumption applies in undocumented methods too, there is always a non-null return value.

<syntaxhighlight lang="java">

   Object get();

</syntaxhighlight>


Trust your assumptions

  • You have a code of conduct - give it a chance.

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.