coding_style.mdwn
1 If you do nothing else, avoid use of partial functions from the Prelude! 2 `import Utility.PartialPrelude` helps avoid this by defining conflicting 3 functions for all the common ones. Also avoid `!!`, it's partial too. 4 5 The rest of this coding style is followed to keep the code in Propellor 6 consistent. You don't have to follow these rules in your own config.hs, or 7 in Propellor modules that you don't intend to get merged into mainstrain 8 Propellor. 9 10 Start a module with a comment indicating what software it provides 11 properties for, and who maintains the module. 12 13 -- | Maintainer: Your Name Here <optional-email-address@example.org> 14 -- 15 -- Support for the Foo daemon <https://foo.example.com/> 16 17 module Propellor.Property.Foo 18 19 Use tabs for indentation. 20 21 Code should make sense with any tab stop setting, but 8 space tabs are 22 the default. With 8 space tabs, code should not exceed 80 characters 23 per line. (With larger tabs, it may of course.) 24 25 Use spaces for layout. For example, here spaces (indicated with `.`) 26 are used after the initial tab to make the third test line up with 27 the others. 28 29 when (foo_test || bar_test || 30 ......some_other_long_test) 31 print "hi" 32 33 As a special Haskell-specific rule, "where" clauses are indented with two 34 spaces, rather than a tab. This makes them stand out from the main body 35 of the function, and avoids excessive indentation of the where cause content. 36 The definitions within the where clause should be put on separate lines, 37 each indented with a tab. 38 39 main = do 40 foo 41 bar 42 foo 43 where 44 foo = ... 45 bar = ... 46 47 Where clauses for instance definitions and modules tend to appear at the end 48 of a line, rather than on a separate line. 49 50 module Foo (Foo, mkFoo, unFoo) where 51 instance Show Property where 52 53 When a function's type signature needs to be wrapped to another line, 54 it's typical to switch to displaying one parameter per line. 55 56 foo :: Bar -> Baz -> (Bar -> Baz) -> IO Baz 57 58 foo' 59 :: Bar 60 -> Baz 61 -> (Bar -> Baz) 62 -> IO Baz 63 64 Note that the "::" then starts its own line. It is not put on the same 65 line as the function name because then it would not be guaranteed to line 66 up with the "->" at all tab width settings. Similarly, guards are put 67 on their own lines: 68 69 splat i 70 | odd i = error "splat!" 71 | otherwise = i 72 73 Multiline lists and record syntax are written with leading commas, 74 that line up with the open and close punctuation. 75 76 list = 77 [ item1 78 , item2 79 , item3 80 ] 81 82 foo = DataStructure 83 { name = "bar" 84 , address = "baz" 85 } 86 87 Similarly, data structures line up the leading `=` with the following `|` 88 89 data Foo 90 = Bar 91 | Baz 92 | Quux Foo 93 deriving (Eq, Ord) 94 95 Module imports are separated into two blocks, one for third-party modules, 96 and one for modules that are part of propellor. (Additional blocks can be used 97 if it makes sense.) 98 99 Using tabs for indentation makes use of `let .. in` particularly tricky. 100 There's no really good way to bind multiple names in a let clause with 101 tab indentation. Instead, a where clause is typically used. To bind a single 102 name in a let clause, this is sometimes used: 103 104 foo = let x = 42 105 in x + (x-1) + x 106 107 ----- 108 109 If you feel that this coding style leads to excessive amounts of horizontal 110 or vertical whitespace around your code, making it hard to fit enough of it 111 on the screen, consider finding a better abstraction, so the code that 112 does fit on the screen is easily understandable. ;) 113 114 ---- 115 116 Note for emacs users: You can put the following snippet into a file called 117 `.dir-locals.el` at root of propellor's source tree to use tabs for indentation: 118 119 ((nil . ((indent-tabs-mode . t) 120 (tab-width . 8) 121 (fill-column . 80))) 122 ;; Warn about spaces used for indentation: 123 (haskell-mode . ((eval . (highlight-regexp "^ +"))))) 124 125 Also consider [haskell-tab-indent-mode](https://spwhitton.name/tech/code/haskell-tab-indent/). The standard indentation modes that come with haskell-mode do not work well with tabs for indentation. This mode works well for hacking on Propellor.