9 These are a few guidelines for Ganeti code and documentation.
11 In simple terms: try to stay consistent with the existing code. `PEP 8`_ says:
13 .. _PEP 8: http://www.python.org/dev/peps/pep-0008/
15 A style guide is about consistency. Consistency with this style guide is
16 important. Consistency within a project is more important. Consistency
17 within one module or function is most important.
21 You might also want to take a look at the `Google style guide`_, since we
22 have some things in common with it.
24 .. _Google style guide: http://google-styleguide.googlecode.com/svn/trunk/pyguide.html
28 In general, always indent using two (2) spaces and don't use tabs.
30 The two spaces should always be relative to the previous level of indentation,
31 even if this means that the final number of spaces is not a multiple of 2.
33 When going on a new line inside an open parenthesis, align with the content of
34 the parenthesis on the previous line.
40 list_elem, # 7 spaces, but 2 from the previous indentation level
46 Always use double quotes (``""``), never single quotes (``''``), except for
47 existing code. Examples for formatting strings::
51 # Note: The space character is always on the second line
52 var = ("The quick brown fox jumps over the lazy dog. The quick brown fox"
53 " jumps over the lazy dog. The quick brown fox jumps over the lazy"
56 fn("The quick brown fox jumps over the lazy dog. The quick brown fox jumps"
57 " over the lazy dog.")
59 fn(constants.CONFIG_VERSION,
60 ("The quick brown fox jumps over the lazy dog. The quick brown fox"
61 " jumps over the lazy dog. The quick brown fox jumps over the lazy"
64 Don't format strings like this::
66 # Don't use single quotes
69 # Don't use backslash for line continuation
70 var = "The quick brown fox jumps over the lazy dog. The quick brown fox"\
71 " jumps over the lazy dog."
73 # Space character goes to the beginning of a new line
74 var = ("The quick brown fox jumps over the lazy dog. The quick brown fox "
75 "jumps over the lazy dog. The quick brown fox jumps over the lazy "
80 Built-in sequence types are list (``[]``), tuple (``()``) and dict (``{}``).
81 When splitting to multiple lines, each item should be on its own line and a
82 comma must be added on the last line. Don't write multiline dictionaries in
83 function calls, except when it's the only parameter. Always indent items by
92 # Longer sequences and dictionary
94 constants.XYZ_FILENAME_EXTENSION,
95 constants.FOO_BAR_BAZ,
102 # Multiline tuples as dictionary values
105 ("long value taking the whole line, requiring you to go to a new one",
110 var = frozenset([1, 2, 3])
112 "xyz": constants.XYZ,
113 "abc": constants.ABC,
117 F(123, "Hello World",
118 { "xyz": constants.XYZ })
120 We consider tuples as data structures, not containers. So in general please
121 use lists when dealing with a sequence of homogeneous items, and tuples when
122 dealing with heterogeneous items.
126 Positional arguments must be passed as positional arguments, keyword arguments
127 must be passed as keyword arguments. Everything else will be difficult to
133 def F(data, key, salt=None, key_selector=None):
137 F("The quick brown fox", "123456")
138 F("The quick brown fox", "123456", salt="abc")
139 F("The quick brown fox", "123456", key_selector="xyz")
140 F("The quick brown fox", "123456", salt="foo", key_selector="xyz")
142 # No: Passing keyword arguments as positional argument
143 F("The quick brown fox", "123456", "xyz", "bar")
145 # No: Passing positional arguments as keyword argument
146 F(salt="xyz", data="The quick brown fox", key="123456", key_selector="xyz")
153 `PEP 257`_ is the canonical document, unless epydoc overrules it (e.g. in how
154 to document the type of an argument).
156 For docstrings, the recommended format is epytext_, to be processed via
157 epydoc_. There is an ``apidoc`` target that builds the documentation and puts it
158 into the doc/api subdir. Note that we currently use epydoc version 3.0.
160 .. _PEP 257: http://www.python.org/dev/peps/pep-0257/
161 .. _epytext: http://epydoc.sourceforge.net/manual-epytext.html
162 .. _epydoc: http://epydoc.sourceforge.net/
164 Note that one-line docstrings are only accepted in the unittests.
166 Rules for writing the docstrings (mostly standard Python rules):
168 * the docstring should start with a sentence, with punctuation at the end,
169 summarizing the the aim of what is being described. This sentence cannot be
171 * the second line should be blank
172 * afterwards the rest of the docstring
173 * special epytext tags should come at the end
174 * multi-line docstrings must finish with an empty line
175 * do not try to make a table using lots of whitespace
176 * use ``L{}`` and ``C{}`` where appropriate
181 """Compute the sum of foo and bar.
183 This functions builds the sum of foo and bar. It's a simple function.
186 @param foo: First parameter.
188 @param bar: The second parameter. This line is longer
191 @return: the sum of the two numbers
196 Some rules of thumb which should be applied with good judgement on a case-to-
199 * If the meaning of parameters is already obvious given its name and the
200 methods description, don't document it again. Just add a ``@type`` tag.
201 * Refer to the base methods documentation when overwriting methods. Only
202 document more if it applies to the current subclass only, or if you want to
203 clarify on the meaning of parameters for the special subclass.
205 Rules for classes and modules
206 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
207 As `PEP 257`_ says, the docstrings of classes should document their attributes
208 and the docstrings of modules should shortly document the exported
209 functions/variables/etc.
211 See for example the pydoc output for the ``os`` or ``ConfigParser`` standard
217 .. highlight:: haskell
219 The most important consideration is, as usual, to stay consistent with the
222 As there's no "canonical" style guide for Haskell, this code style has been
223 inspired from a few online resources, including the style guide for the
224 `Snap framework`_, `this style guide`_ and `this other style guide`_.
226 .. _Snap framework: http://snapframework.com/docs/style-guide
227 .. _this style guide: https://github.com/tibbe/haskell-style-guide/blob/master/haskell-style.md
228 .. _this other style guide: http://www.cs.caltech.edu/courses/cs11/material/haskell/misc/haskell_style_guide.html
232 Use ordinary, non-`literate`_ Haskell ``.hs`` files.
234 .. _literate: http://www.haskell.org/haskellwiki/Literate_programming
236 Use proper copyright headers, and proper Haddock style documentation headers::
238 {-| Short module summary.
240 Longer module description.
248 This program is free software ...
252 If there are module-level pragmas add them right at the top, before the short
257 Imports should be grouped into the following groups and inside each group they
258 should be sorted alphabetically:
260 1. standard library imports
261 2. third-party imports
264 It is allowed to use qualified imports with short names for:
266 * standard library (e.g. ``import qualified Data.Map as M``)
267 * local imports (e.g. ``import qualified Ganeti.Constants as C``), although
268 this form should be kept to a minimum
272 Use only spaces, never tabs. Indentation level is 2 characters. For Emacs,
273 this means setting the variable ``haskell-indent-offset`` to 2.
275 Line length should be at most 78 chars, and 72 chars inside comments.
277 Use indentation-based structure, and not braces/semicolons.
281 Special indendation of if/then/else construct
283 For the ``do`` notation, the ``if-then-else`` construct has a non-intuitive
284 behaviour. As such, the indentation of ``if-then-else`` (both in ``do``
285 blocks and in normal blocks) should be as follows::
291 i.e. indent the then/else lines with another level. This can be accomplished
292 in Emacs by setting the variable ``haskell-indent-thenelse`` to 2 (from the
295 If you have more than one line of code please newline/indent after the "=". Do
307 or if it is just one line::
313 Multiline strings are created by closing a line with a backslash and starting
314 the following line with a backslash, keeping the indentation level constant.
315 Whitespaces go on the new line, right after the backslash.
320 longString = "This is a very very very long string that\
321 \ needs to be split in two lines"
326 Note that this is different from the Python style!
328 When declaring either data types, or using list literals, etc., the columns
329 should be aligned, and for lists use a comma at the start of the line, not at
332 data OpCode = OpStartupInstance ...
333 | OpShutdownInstance ...
336 data Node = Node { name :: String
346 The choice of whether to wrap the first element or not is up to you; the
347 following is also allowed::
354 For records, always add spaces around the braces and the equality sign.
357 foo = Foo { fBar = "bar", fBaz = 4711 }
359 foo' = Foo { fBar = "bar 2"
363 node' = node { ip = "127.0.0.1" }
368 Like in Python, surround binary operators with one space on either side. Do no
369 insert a space after a lamda::
374 foldl (\x y -> ...) ...
376 Use a blank line between top-level definitions, but no blank lines between
377 either the comment and the type signature or between the type signature and
378 the actual function definition.
381 Ideally it would be two blank lines between top-level definitions, but the
382 code only has one now.
384 As always, no trailing spaces. Ever.
399 Functions should be named in mixedCase style, and types in CamelCase. Function
400 arguments and local variables should be mixedCase.
402 When using acronyms, ones longer than 2 characters should be typed capitalised,
403 not fully upper-cased (e.g. ``Http``, not ``HTTP``).
405 For variable names, use descriptive names; it is only allowed to use very
406 short names (e.g. ``a``, ``b``, ``i``, ``j``, etc.) when:
408 * the function is trivial, e.g.::
412 * we talk about some very specific cases, e.g.
413 iterators or accumulators in folds::
415 map (\v -> v + 1) lst
417 * using ``x:xs`` for list elements and lists, etc.
419 In general, short/one-letter names are allowed when we deal with polymorphic
420 values; for example the standard map definition from Prelude::
422 map :: (a -> b) -> [a] -> [b]
424 map f (x:xs) = f x : map f xs
426 In this example, neither the ``a`` nor ``b`` types are known to the map
427 function, so we cannot give them more explicit names. Since the body of the
428 function is trivial, the variables used are longer.
430 However, if we deal with explicit types or values, their names should be
433 .. todo: add a nice example here.
435 Finally, the naming should look familiar to people who just read the
436 Prelude/standard libraries.
438 Naming for updated values
439 *************************
441 .. highlight:: python
443 Since one cannot update a value in Haskell, this presents a particular problem
444 on the naming of new versions of the same value. For example, the following
447 def failover(pri, sec, inst):
448 pri.removePrimary(inst)
449 pri.addSecondary(inst)
450 sec.removeSecondary(inst)
453 .. highlight:: haskell
455 becomes in Haskell something like the following::
457 failover pri sec inst =
458 let pri' = removePrimary pri inst
459 pri'' = addSecondary pri' inst
460 sec' = removeSecondary sec inst
461 sec'' = addPrimary sec' inst
464 When updating values, one should add single quotes to the name for up to three
465 new names (e.g. ``inst``, ``inst'``, ``inst''``, ``inst'''``) and otherwise
466 use numeric suffixes (``inst1``, ``inst2``, ``inst3``, ..., ``inst8``), but
467 that many updates is already bad style and thus should be avoided.
472 Always declare types for functions (and any other top-level bindings).
474 If in doubt, feel free to declare the type of the variables/bindings in a
475 complex expression; this usually means the expression is too complex, however.
477 Similarly, provide Haddock-style comments for top-level definitions.
479 Use sum types instead of exceptions
480 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
482 Exceptions make it hard to write functional code, as alternative
483 control flows need to be considered and compiler support is limited.
484 Therefore, Ganeti functions should never allow exceptions to escape.
485 Function that can fail should report failure by returning an appropriate
486 sum type (``Either`` or one of its glorified variants like ``Maybe`` or
487 ``Result``); the preferred sum type for reporting errors is ``Result``.
489 As other Ganeti functions also follow these guide lines, they can safely
490 be composed. However, be careful when using functions from other libraries;
491 if they can raise exceptions, catch them, preferably as close to their
492 origin as reasonably possible.
494 Parentheses, point free style
495 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
497 Prefer the so-called `point-free`_ style style when declaring functions, if
501 let a x = f (g (h x))
505 Also use function composition in a similar manner in expressions to avoid extra
515 .. _`point-free`: http://www.haskell.org/haskellwiki/Pointfree
523 It is recommended to keep the use of extensions to a minimum, so that the code
524 can be understood even if one is familiar with just Haskel98/Haskell2010. That
525 said, some extensions are very common and useful, so they are recommended:
527 * `Bang patterns`_: useful when you want to enforce strict evaluation (and better
528 than repeated use of ``seq``)
529 * CPP: a few modules need this in order to account for configure-time options;
530 don't overuse it, since it breaks multi-line strings
531 * `Template Haskell`_: we use this for automatically deriving JSON instances and
532 other similar boiler-plate
534 .. _Bang patterns: http://www.haskell.org/ghc/docs/latest/html/users_guide/bang-patterns.html
535 .. _Template Haskell: http://www.haskell.org/ghc/docs/latest/html/users_guide/template-haskell.html
537 Such extensions should be declared using the ``Language`` pragma::
539 {-# Language BangPatterns #-}
541 {-| This is a small module... -}
546 Always use proper sentences; start with a capital letter and use punctuation
547 in top level comments::
549 -- | A function that does something.
552 For inline comments, start with a capital letter but no ending punctuation.
553 Furthermore, align the comments together with a 2-space width from the end of
554 the item being commented::
556 data Maybe a = Nothing -- ^ Represents empty container
557 | Just a -- ^ Represents a single value
559 The comments should be clear enough so that one doesn't need to look at the
560 code to understand what the item does/is.
562 Use ``-- |`` to write doc strings rather than bare comment with ``--``.
567 We generate the API documentation via Haddock, and as such the comments should
568 be correct (syntax-wise) for it. Use markup, but sparingly.
570 We use hlint_ as a lint checker; the code is currently lint-clean, so you must
571 not add any warnings/errors.
573 .. _hlint: http://community.haskell.org/~ndm/darcs/hlint/hlint.htm
575 Use these two commands during development::
580 QuickCheck best practices
581 *************************
583 If you have big type that takes time to generate and several properties to
584 test on that, by default 500 of those big instances are generated for each
585 property. In many cases, it would be sufficient to only generate those 500
586 instances once and test all properties on those. To do this, create a property
587 that uses ``conjoin`` to combine several properties into one. Use
588 ``printTestCase`` to add expressive error messages. For example::
590 prop_myMegaProp :: myBigType -> Property
594 ("Something failed horribly here: " ++ show b) (subProperty1 b)
596 ("Something else failed horribly here: " ++ show b)
598 , -- more properties here ...
601 subProperty1 :: myBigType -> Bool
604 subProperty2 :: myBigType -> Property
612 Use ``genMaybe genSomething`` to create ``Maybe`` instances of something
613 including some ``Nothing`` instances.
615 Use ``Just <$> genSomething`` to generate only ``Just`` instances of
621 To generate strings, consider using ``genName`` instead of ``arbitrary``.
622 ``arbitrary`` has the tendency to generate strings that are too long.