Home | Download/Install | Documentation | Packages | Screenshots | News | Forum/Mailing-lists | Contact | GForge
There are two principles that are essential to writing good computer code.
⇒ Keep your code as simple as you can.
We report in this document common coding rules. Try to respect them, but be consistent with surrounding existing code.
Please read first the “official” Python style guide written by the Python's author.
You can use tools like PyLint which analyzes Python source code looking for bugs and signs of poor quality.
For example, you can use and complete the following header:
# -*- python -*- # # OpenAlea.MyModule: MyModule Description # # Copyright 2013 INRIA - CIRAD - INRA # # File author(s): FirstName LastName <firstname.lastname@lab.com> # # File contributor(s): # # Distributed under the Cecill-C License. # See accompanying file LICENSE.txt or copy at # http://www.cecill.info/licences/Licence_CeCILL-C_V1-en.html # # OpenAlea WebSite : http://openalea.gforge.inria.fr # ############################################################################### """ Write the doc here... """ __revision__ = "$Id: $"
Don't mix tab and spaces. For new project, use only spaces.
Add a Doctring for all classes and functions.
Modules or packages
Module names are the same as the file name. Modules should have short, lowercase names, without underscore. Singular is better than plural.
Yes:
import tree
No:
import Tree
For C/C++/Fortran extension modules embedded in a Python package that provide a more Pythonic interface, the C/C++/Fortran module has a leading underscore (e.g. _amlpymodule).
Class Names
Class names use the CapsWords convention.
class Tree: pass
Class Methods and Function Names
Class methods and function names should be lowercase, with words separated by underscores.
def this_is_a_function(): pass class ClassName: def class_method(self): pass
Exception Names
Use class naming convention with Error as a suffix.
class VertexError(TypeError): pass
Whitespace in Expressions and Statements
Avoid extraneous whitespace in the following situations:
# Yes: spam(ham[1], {eggs: 2}) # No: spam( ham[ 1 ], { eggs: 2 } )
# Yes: if x == 4: print x, y; x, y = y, x # No: if x == 4 : print x , y ; x , y = y , x
list of a function call:
# Yes: spam(1) # No: spam (1)
slicing:
# Yes: dict['key'] = list[index] # No: dict ['key'] = list [index]
# Yes: i = i + 1 submitted += 1 x = x * 2 - 1 hypot2 = x * x + y * y c = (a + b) * (a - b) # No: i=i+1 submitted +=1 x = x*2 - 1 hypot2 = x*x + y*y c = (a+b) * (a-b)
keyword argument or a default parameter value.
#Yes: def complex(real, imag=0.0): return magic(r=real, i=imag) #No: def complex(real, imag = 0.0): return magic(r = real, i = imag)
Use property instead of get, set methods. Hide get and set methods with a leading underscore. Classes with properties have to inherit from object.
Yes:
class Tree(object): def _get_root(self): return self._root def _set_root(self, _root): self._root = _root root = property(_get_root, _set_root)
No:
class Tree(object): def get_root(self): return self._root def set_root(self, _root): self._root = _root root = property(get_root,set_root)
/* -*-c++-*-
* ----------------------------------------------------------------------------
*
* VPlants.MyModule: : MyModule Description
*
* Copyright 1995-2015 INRIA - CIRAD - INRA
*
* File author(s): FirstName LastName <firstname.lastname@inria.fr>
*
* File contributor(s):
*
*
* Distributed under the Cecill-C License.
* See accompanying file LICENSE.txt or copy at
* http://www.cecill.info/licences/Licence_CeCILL-C_V1-en.html
*
* OpenAlea WebSite : http://openalea.gforge.inria.fr
*
*
* ----------------------------------------------------------------------------
*/
Indentation
Use 4 spaces per indentation level. Do not use tabs.
Functions
int fct( int i, int j ) { // ... }
Loops
for( int i= 1; i <= n; ++i ) { // ... }
while( cond ) { // ... }
do{ //... } while( cond );