Changes between Version 12 and Version 13 of CodingStandards


Ignore:
Timestamp:
2011-06-30T00:02:08Z (13 years ago)
Author:
zooko
Comment:

coding standards update!

Legend:

Unmodified
Added
Removed
Modified
  • CodingStandards

    v12 v13  
    1818
    1919{{{
    20 # Copyright (c) 2009 Allmydata, Inc.
     20# Copyright (c) 2011 The Tahoe-LAFS Software Foundation
    2121# This file is part of Tahoe-LAFS; see doc/about.html for licensing terms.
    2222
     
    3434}}}
    3535
     36 * Put two blank lines between classes.
    3637 * Feel free to ignore the part of PEP-8 that says to put each module import on a separate line, but don't import modules from multiple separate packages on the same line.
    3738 * Ignore the part of PEP-257 which says to put the trailing {{{"""}}} of a multi-line docstring on a separate line separated by a blank line.  (That rule appears to have been motivated by a limitation of Emacs which has been fixed.)
     
    9495Now you can put {{{assert self._assert_consistency()}}} everywhere in your class where the class ought to be in an internally consistent state. For example, at the beginning of every externally-callable method. This technique can be very valuable in developing a complex class -- it catches bugs early, it isolates bugs into specific code paths, and it clarifies the internal structure of the class so that other developers can hack on it without subtle misunderstandings.
    9596
     97=== configuration ===
     98
     99==== minimizing configuration ====
     100
     101 * Do not implement configuration files for modules or libraries -- code that is going to be used by other code. Only applications -- code that is going to be used by humans -- have configuration files. Modules and libraries get "configured" by the code that calls them, for example by passing arguments to their constructors.
     102 * If there are constant values which end-users do not need to modify, then do not make them configurable, but put them in all-caps variables at the beginning of the Python file in which they are used.
     103 * Design algorithms so that they have as few "voodoo constants" and "tweakable parameters" as possible.
     104
     105==== how to implement configuration ====
     106
     107Whether in application code or in library code, never pass configuration values via a configuration object. Instead use Python parameters. For example -- here's another real-life example -- do not write
     108
     109{{{
     110class BlockStore:
     111    def __init__(self, confdict={}, recoverdb=True, name='*unnamed*'):
     112        if confdict.has_key('MAX_MEGABYTES'):
     113            self.maxspace = (2**20) * int(confdict.get('MAX_MEGABYTES'))
     114        else:
     115            self.maxspace = None
     116        self.basepath = os.path.abspath(confdict.get("PATH", ""))
     117        self.maintainertype = confdict.get("MAINTAINER", "rnd").lower()
     118        self.backendtype = confdict.get("BACKEND", "flat").lower()
     119}}}
     120
     121, but instead write
     122
     123{{{
     124class BlockStore:
     125    def __init__(self, maxspace=None, path="", maintainertype="rnd", backendtype="flat", recoverdb=True, name='*unnamed*'):
     126        self.basepath = os.path.abspath(path)
     127        self.maintainertype = maintainertype
     128        self.backendtype = backendtype
     129}}}
     130.
     131
     132
    96133==== assertion policy ====
    97134
     
    144181
    145182
    146 === configuration ===
    147 
    148 ==== minimizing configuration ====
    149 
    150  * Do not implement configuration files for modules or libraries -- code that is going to be used by other code. Only applications -- code that is going to be used by humans -- have configuration files. Modules and libraries get "configured" by the code that calls them, for example by passing arguments to their constructors.
    151  * If there are constant values which end-users do not need to modify, then do not make them configurable, but put them in all-caps variables at the beginning of the Python file in which they are used.
    152  * Design algorithms so that they have as few "voodoo constants" and "tweakable parameters" as possible.
    153 
    154 ==== how to implement configuration ====
    155 
    156 Whether in application code or in library code, never pass configuration values via a configuration object. Instead use Python parameters. For example -- here's another real-life example -- do not write
    157 
    158 {{{
    159 class BlockStore:
    160     def __init__(self, confdict={}, recoverdb=True, name='*unnamed*'):
    161         if confdict.has_key('MAX_MEGABYTES'):
    162             self.maxspace = (2**20) * int(confdict.get('MAX_MEGABYTES'))
    163         else:
    164             self.maxspace = None
    165         self.basepath = os.path.abspath(confdict.get("PATH", ""))
    166         self.maintainertype = confdict.get("MAINTAINER", "rnd").lower()
    167         self.backendtype = confdict.get("BACKEND", "flat").lower()
    168 }}}
    169 
    170 , but instead write
    171 
    172 {{{
    173 class BlockStore:
    174     def __init__(self, maxspace=None, path="", maintainertype="rnd", backendtype="flat", recoverdb=True, name='*unnamed*'):
    175         self.basepath = os.path.abspath(path)
    176         self.maintainertype = maintainertype
    177         self.backendtype = backendtype
    178 }}}
    179 .
    180 
    181 
    182183== official Python standards ==
    183184