Difference between revisions of "Coding Convention"
m |
m |
||
Line 3: | Line 3: | ||
==Argument Assumption== | ==Argument Assumption== | ||
*'''All method arguments are non-null unless explicitely stated otherwise in documentation. ''' | *'''All method arguments are non-null unless explicitely stated otherwise in documentation. ''' | ||
+ | <br /> | ||
+ | The default assumption is that argument is non-null. This applies to undocumented methods. | ||
<div style="background: #f3fff3;"> | <div style="background: #f3fff3;"> | ||
<syntaxhighlight lang="java"> | <syntaxhighlight lang="java"> | ||
+ | void read(InputStream is); | ||
+ | |||
+ | // and | ||
+ | |||
/** | /** | ||
* Read the object from a file. | * Read the object from a file. | ||
Line 12: | Line 18: | ||
*/ | */ | ||
void read(File file); | void read(File file); | ||
+ | </syntaxhighlight> | ||
+ | <br/> | ||
+ | </div> | ||
+ | <br/> | ||
+ | A null option is explicitely stated. | ||
+ | <div style="background: #f3fff3;"> | ||
+ | <syntaxhighlight lang="java"> | ||
/** | /** | ||
* Write or remove existing value. | * Write or remove existing value. | ||
Line 24: | Line 37: | ||
<br/> | <br/> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
Line 37: | Line 43: | ||
*'''All return values are non-null unless explicitely stated otherwise in documentation.'''<br/> | *'''All return values are non-null unless explicitely stated otherwise in documentation.'''<br/> | ||
+ | The thumb rule is that return value is non-null. This applies to undocumented methods. | ||
+ | value returned from this method. | ||
<div style="background: #f3fff3;"> | <div style="background: #f3fff3;"> | ||
<syntaxhighlight lang="java"> | <syntaxhighlight lang="java"> | ||
+ | Object get(); | ||
+ | |||
+ | // and | ||
+ | |||
/** | /** | ||
* Get the value | * Get the value | ||
Line 45: | Line 57: | ||
*/ | */ | ||
Object get() throws ValueUnavailableException; | Object get() throws ValueUnavailableException; | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
<br/> | <br/> | ||
Line 57: | Line 62: | ||
<br/> | <br/> | ||
− | + | Null option as return value is always explicitely documented. | |
<div style="background: #f3fff3;"> | <div style="background: #f3fff3;"> | ||
<syntaxhighlight lang="java"> | <syntaxhighlight lang="java"> | ||
+ | /** | ||
+ | * Get possibly existing value | ||
+ | * | ||
+ | * @return the value is exists, otherwise <tt>null</tt> | ||
+ | */ | ||
Object get(); | Object get(); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<br/> | <br/> | ||
</div> | </div> | ||
− | |||
Revision as of 10:22, 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 argument is non-null. This applies to undocumented methods.
<syntaxhighlight lang="java">
void read(InputStream is);
// and
/** * Read the object from a file. * * @param file */ void read(File file);
</syntaxhighlight>
A null option is 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 return value is non-null. This applies to undocumented methods. value returned from this method.
<syntaxhighlight lang="java">
Object get();
// and
/** * Get the value * * @return the value */ Object get() throws ValueUnavailableException;
</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>