Skip to content

Commit 66a5171

Browse files
committed
Add some guidance on code style
1 parent f43047f commit 66a5171

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

CONTRIBUTING.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,48 @@ Once installed, you can check your changes with command:
329329
stack exec -- sh ./etc/scripts/hlint.sh
330330
~~~
331331

332+
## Code Style
333+
334+
A single code style is not applied consistently to Stack's code and Stack is not
335+
Procrustean about matters of style. Rules of thumb, however, are:
336+
337+
* keep pull requests that simply reformat code separate from those that make
338+
other changes to code; and
339+
* when making changes to code other than reformatting, follow the existing style
340+
of the function(s) or module(s) in question.
341+
342+
That said, the following may help:
343+
344+
* Stack's code generally avoids the use of C preprocessor (CPP) directives.
345+
Windows and non-Windows code is separated in separate source code directories
346+
and distinguished in Stack's Cabal file. `Stack.Constants.osIsWindows :: Bool`
347+
is provided. Multi-line strings are generally formatted on the assumption that
348+
GHC's `CPP` language pragma is not being used.
349+
* Language pragmas usually start with `NoImplictPrelude`, where applicable, and
350+
then all others are listed alphabetically. The closing `#-}` are aligned, for
351+
purely aesthetic reasons.
352+
* Stack is compiled with GHC's `-Wall` enabled, which includes `-Wtabs` (no tabs
353+
in source code). Most modules are based on two spaces (with one space for a
354+
`where`) for indentation but older and larger modules are still based on four
355+
spaces.
356+
* Stack's code and documentation tends to be based on lines of no more than 80
357+
characters or, if longer, no longer than necessary.
358+
* Stack uses export lists.
359+
* Stack's imports are listed alphabetically, including `Stack.Prelude`, where
360+
applicable. The module names are left aligned, with space left for `qualified`
361+
where it is absent.
362+
* Stack's code is sufficiently stable that explict import lists can sensibly be
363+
used. The exception is the import of `Stack.Prelude`. Not all modules have
364+
comprehensive explicit import lists.
365+
* Short explicit import lists follow the module name. Longer lists start on the
366+
line below the module name. Spaces are used to separate listed items from
367+
their enclosing parentheses.
368+
* As noted above, the types used to implement Stack's exceptions and the related
369+
`instance` definitions are usually located at the top of the relevant module.
370+
* In function type signatures, the `::` is kept on the same line as the
371+
function's name. This format is Haskell syntax highlighter-friendly.
372+
* If `where` is used, the declarations follow on a separate line.
373+
332374
## Testing
333375

334376
The Stack code has both unit tests and integration tests. Integration tests can

0 commit comments

Comments
 (0)