summaryrefslogtreecommitdiff
path: root/STYLE.md
blob: e52afe987b945e84a72217e86894a9922fef3976 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
LambdaCat Style Guide
=====================

This style guide is based on Johan Tibell's Haskell Style Guide at
<https://github.com/tibbe/haskell-style-guide/blob/master/haskell-style.md>.

This is a short document describing the preferred coding style for
this project.  We've tried to cover the major areas of formatting and
naming.  When something isn't covered by this guide you should stay
consistent with the code in the other modules.

Table of Contents
-----------------

1. Formatting
2. Imports
3. Comments
4. Naming
5. Misc

1. Formatting
-------------

### Line Length

Maximum line length is *78 characters*.

### Indentation

Tabs are illegal. Use spaces for indenting.  Indent your code blocks
with *4 spaces*.  Indent the `where` keyword two spaces to set it
apart from the rest of the code and indent the definitions in a
`where` clause 2 spaces.  Some examples:

    sayHello :: IO ()
    sayHello = do
        name <- getLine
        putStrLn $ greeting name
      where
        greeting name = "Hello, " ++ name ++ "!"

    filter :: (a -> Bool) -> [a] -> [a]
    filter _ []     = []
    filter p (x:xs)
        | p x       = x : filter p xs
        | otherwise = filter p xs

### Blank Lines

One blank line between top-level definitions.  No blank lines between
type signatures and function definitions.  Add one blank line between
functions in a type class instance declaration if the functions bodies
are large.  Use your judgement.

### Whitespace

Surround binary operators with a single space on either side.  Don't
insert a space after a lambda.

### Data Declarations

Align the constructors in a data type definition.  Indent deriving
declarations with two spaces.  Example:

    data Tree a = Branch a (Tree a) (Tree a)
                | Leaf
      deriving (Eq, Show)

For long type names the following formatting is also acceptable:

    data HttpException
        = InvalidStatusCode Int
        | MissingContentHeader
      deriving (Eq, Show)

Format records as follows:

    data Person = Person
        { firstName :: String  -- ^ First name
        , lastName  :: String  -- ^ Last name
        , age       :: Int     -- ^ Age
        }
      deriving (Eq, Show)

### List Declarations

Align the elements in the list.  Example:

    exceptions =
        [ InvalidStatusCode
        , MissingContentHeader
        , InternalServerError
        ]

Optionally, you can skip the first newline.  Use your judgement.

    directions = [ North
                 , East
                 , South
                 , West
                 ]

### Pragmas

For language extensions use the LANGUAGE pragma.  Keep the
extensions in alphabetical order.  Align the extensions. Indent
the closing bracket with two spaces.  Example:

    {-# LANGUAGE ExistentialQuantification
               , FunctionalDependencies
               , MultiParamTypeClasses
      #-}

If you only have one extension put the closing bracket on the
same line:

    {-# LANGUAGE ExistentialQuantification #-}

If you need to specify compiler options per file (e.g. you need
to disable certain warnings) align the options and keep them in
alphabetical order if reasonable.  Example:

    {-# OPTIONS_GHC -fno-warn-orphans
                    -fno-warn-unused-binds
      #-}

Again if there is only one option put the closing bracket on the
same line.

Put pragmas immediately following the function they apply to.
Example:

    id :: a -> a
    id x = x
    {-# INLINE id #-}

In the case of data type definitions you must put the pragma before
the type it applies to.  Example:

    data Array e = Array
        {-# UNPACK #-} !Int
        !ByteArray

### Hanging Lambdas

You may or may not indent the code following a "hanging" lambda.  Use
your judgement. Some examples:

    bar :: IO ()
    bar = forM_ [1, 2, 3] $ \n -> do
              putStrLn "Here comes a number!"
              print n

    foo :: IO ()
    foo = alloca 10 $ \a ->
          alloca 20 $ \b ->
          cFunction a b

### Export Lists

Format export lists as follows:

    module Data.Set
        (
          -- * The @Set@ type
          Set

        , empty
        , singleton

          -- * Querying
        , member
        )
    where

2. Imports
----------

Imports should be grouped in the following order:

1. standard library imports
2. related third party imports
3. local application/library specific imports

Put a blank line between each group of imports.  The imports in each
group should be sorted alphabetically, by module name.

It is recommended to use explicit import lists or `qualified` imports
for standard and third party libraries.  This makes the code more
robust against changes in these libraries.  Exception: The Prelude.
Use your judgement.

Format the imports as follows:

    import Prelude hiding
        ( lookup
        , null
        )
    import qualified Data.Map as Map
    import Data.Maybe
        ( fromJust
        )
    import GHC.Exts ()

3. Comments
-----------

### Punctuation

Write proper sentences; start with a capital letter and use proper
punctuation.

### Top-Level Definitions

Comment every top level function (particularly exported functions),
and provide a type signature; use Haddock syntax in the comments.
Comment every exported data type.  Some examples:

    -- | Send a message on a socket.  The socket must be in a connected
    -- state.  Returns the number of bytes sent.  Applications are
    -- responsible for ensuring that all data has been sent.
    send :: Socket      -- ^ Connected socket
         -> ByteString  -- ^ Data to send
         -> IO Int      -- ^ Bytes sent

    -- | Bla bla bla.
    data Person = Person
        { age  :: Int     -- ^ Age
        , name :: String  -- ^ First name
        }

For functions the documentation should give enough information to
apply the function without looking at the function's definition.

### End-of-Line Comments

Separate end-of-line comments from the code using 2 spaces.  Align
comments for data type definitions.  Some examples:

    data Parser = Parser
        Int         -- Current position
        ByteString  -- Remaining input

    foo :: Int -> Int
    foo n = salt * 32 + 9
      where
        salt = 453645243  -- Magic hash salt.

### Links

Use in-line links economically.  You are encouraged to add links for
API names.  It is not necessary to add links for all API names in a
Haddock comment.  We therefore recommend adding a link to an API name
if:

* The user might actually want to click on it for more information (in
  your judgment), and

* Only for the first occurrence of each API name in the comment (don't
  bother repeating a link)

4. Naming
---------

For readability reasons, don't capitalize all letters when using an
abbreviation.  For example, write `HttpServer` instead of
`HTTPServer`.  Exception: Two letter abbreviations, e.g. `IO`.

5. Misc
-------

### Warnings ###

Code should be compilable with `-Wall -Werror`. There should be no
warnings.