Difference between revisions of "Coding Convention"

From Developer Documents
Jump to navigation Jump to search
m
m
Line 127: Line 127:
 
<br/>
 
<br/>
 
</div>
 
</div>
 +
 +
 +
==Maintainment==
 +
These rules apply to code that has already been published and taken into use by other developers.
 +
 +
*'''API doesn't change between minor releases.'''
 +
In case of faulty design, old methods are preserved and are marked '''@deprecated'''. They can be removed in the next major version release.
 +
 +
<div style="background: #fff3f3;">
 +
<syntaxhighlight lang="java">   
 +
    @deprecated
 +
    Object getValue(Object newValue);
 +
</syntaxhighlight>
 +
<br/>
 +
</div>
 +
 +
*'''Documentation is correct, the implementation is faulty.'''
 +
 +
In case there is a mismatch between the documentation and the implementation, then the documentation prevails and the fault is in the implementation.
 +
 +
 +
In this example the method returns an unexpected null.
 +
<div style="background: #fff3f3;">
 +
<syntaxhighlight lang="java">   
 +
    /**
 +
    * Deserialize an object from an input stream.
 +
    *
 +
    * @param is source stream
 +
    * @return an object
 +
    **/
 +
    Object deserialize(InputStream is) {
 +
        try {
 +
            int x = is.read();
 +
            ...
 +
            return result;
 +
        } catch (IOException e) {
 +
          return null;
 +
        }
 +
    }
 +
</syntaxhighlight>
 +
<br/>
 +
</div>
 +
The '''implementation''' is must be corrected.
 +
<div style="background: #f3fff3;">
 +
<syntaxhighlight lang="java">   
 +
    /**
 +
    * Deserialize an object from an input stream.
 +
    *
 +
    * @deprecated use deserialize2, it has better error control
 +
    * @param is source stream
 +
    * @return an object
 +
    * @throws RuntimeSerializationException in case of IO problems
 +
    **/
 +
    Object deserialize(InputStream is) throws RuntimeSerializationException
 +
    {
 +
        try {
 +
            int x = is.read();
 +
            ...
 +
            return result;
 +
        } catch (IOException e) {           
 +
            throw new RuntimeSerializationException( e );
 +
        }
 +
    }
 +
 +
</syntaxhighlight>
 +
<br/>
 +
</div>
 +
 +
It can be replaced with correct method in the next major version release.
 +
<div style="background: #f3fff3;">
 +
<syntaxhighlight lang="java">   
 +
    /**
 +
    * Deserialize an object from an input stream.
 +
    *
 +
    * @param is source stream
 +
    * @return an object
 +
    * @throws IOException in case of problems
 +
    **/
 +
    Object deserialize(InputStream is) throws IOException
 +
    {
 +
        int x = is.read();
 +
        return result;
 +
    }
 +
 +
</syntaxhighlight>
 +
<br/>
 +
</div>
 +
 +
==Exceptions==
 +
 +
* Encapsulated exceptions
 +
* Rethrowing exceptions
 +
* Runtime Exceptions
 +
*

Revision as of 10:49, 13 October 2010

Simantics coding conventions are gathered in this document. These rules apply to all org.simantics projects.

Argument Assumption

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

The default assumption is that an argument is non-null. This applies to undocumented methods too.

<syntaxhighlight lang="java">

   /**
     * Read the object from a file.
     *
     * @param file 
     */
   void read(File file);
   // and
   void read(File file);

</syntaxhighlight>


A null possibility must be explicitely stated.

<syntaxhighlight lang="java">

   /**
     * Write or remove existing value.
     *
     * @param newValue new value or null</t> to remove the existing value
     */
   void setValue(Object newValue);

</syntaxhighlight>



Return value assumption

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

The thumb rule is that the return value is non-null. It applies to undocumented methods aswell.

<syntaxhighlight lang="java">

   /**
     * Get the value
     *
     * @return the value
     */
   Object get();
   // and
   Object get();

</syntaxhighlight>


Null option as return value is always explicitely documented.

<syntaxhighlight lang="java">

   /**
     * Get possibly existing value
     *
     * @return the value is exists, otherwise null
     */
   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">

   Object deserialize(InputStream is) {
       int x = is.read();
       ...
       return result;
   }

</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">

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

</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;">

   Object deserialize(InputStream is) {
       assert( is != null );
       ...
       return result;
   }

</syntaxhighlight>


Maintainment

These rules apply to code that has already been published and taken into use by other developers.

  • API doesn't change between minor releases.

In case of faulty design, old methods are preserved and are marked @deprecated. They can be removed in the next major version release.

<syntaxhighlight lang="java">

   @deprecated
   Object getValue(Object newValue);

</syntaxhighlight>

  • Documentation is correct, the implementation is faulty.

In case there is a mismatch between the documentation and the implementation, then the documentation prevails and the fault is in the implementation.


In this example the method returns an unexpected null.

<syntaxhighlight lang="java">

   /**
    * Deserialize an object from an input stream.
    *
    * @param is source stream
    * @return an object
    **/
   Object deserialize(InputStream is) {
       try {
           int x = is.read();
           ...
           return result;
       } catch (IOException e) {
          return null;
       }
   }

</syntaxhighlight>

The implementation is must be corrected.

<syntaxhighlight lang="java">

   /**
    * Deserialize an object from an input stream.
    *
    * @deprecated use deserialize2, it has better error control
    * @param is source stream
    * @return an object
    * @throws RuntimeSerializationException in case of IO problems
    **/
   Object deserialize(InputStream is) throws RuntimeSerializationException
   {
       try {
           int x = is.read();
           ...
           return result;
       } catch (IOException e) {            
           throw new RuntimeSerializationException( e );
       }
   }

</syntaxhighlight>

It can be replaced with correct method in the next major version release.

<syntaxhighlight lang="java">

   /**
    * Deserialize an object from an input stream.
    *
    * @param is source stream
    * @return an object
    * @throws IOException in case of problems
    **/
   Object deserialize(InputStream is) throws IOException
   {
       int x = is.read();
       return result;
   }

</syntaxhighlight>

Exceptions

  • Encapsulated exceptions
  • Rethrowing exceptions
  • Runtime Exceptions