Haskell Style Guide
The syntax of the Haskell language can be really beautiful. Even if you don't continue to write programs for GHC after the end of the semester, you may continue to use this syntax to express your ideas, because it just works. Furthermore, many people will be reading the code that you produce in this course---you, your partner, the course staff, and perhaps the whole class during a homework discussion.
So make it pretty! The style guidelines below help. But the best advice is to revise, revise, revise...
|RD||Every top-level function must have an explicit type.|
For example, write
instead of just:
These declarations will sometimes be fairly obvious (from the function definition itself, its name, nearby comments, or whatever), but getting into the habit of writing them all the time is good discipline. And, good type signatures lead to better error messages.
|RT||Every significant piece of functionality must have testing code demonstrating that it works.|
|In particular, every solution to a homework exercise must include a "test driver" that shows how it behaves on several examples.|
|RC||Submitted code must compile.|
Any code you submit must be accepted by GHC without errors or warnings. Never submit anything that you have changed, no matter how small the change, without checking it with GHC. Make sure your code begins with the line:
Code for which GHC generates warnings or errors will not be graded.
|RH||Include a header comment.|
|It must contain least your name, the date, and the number of the assignment.|
|FI||Use consistent indentation.|
|There are several reasonable styles for indenting code in Haskell. Choose one and stick with it.|
|FT||No Tab Characters.|
|Do not use the tab character (0x09). Instead, use spaces to control indenting. This is because the width of a tab is not uniform across all computers, and what looks good on your machine may look terrible on ours, especially if you have mixed spaces and tabs. The homework assignments include a flag that warns against inadvertant tab insertion. Don't remove this flag.|
|No line in your program should be longer than 80 characters. Although most screens are wide enough now to display much longer lines, there are still many editors whose default width is 80 characters, and 80 character lines are the natural width for printing programs in reasonable-sized fonts.|
|ND||Use descriptive names.|
|Choose names that reflect the intended use of the value referred to by the name. Names are documentation.|
|As a general rule, short names (one or a few characters) are appropriate for variables with small scopes, like local definitions or parameters to functions, whereas longer names are appropriate for global definitions, such as top-level functions.|
|NC||Follow standard Haskell naming conventions.|
|For long names composed of multiple words, use "camelCase." Smash the words into a long string and capitalize the first letters of the second and following words-- e.g.,
|When pattern matching against a list, if you call the head
|For readability reasons, don't capitalize all letters when using an abbreviation. For example, write
Top-level declarations (data types and functions) should be preceded by a short comment, written with Haddock syntax.
|CW||Comment what the code does, not how.|
|It should be obvious how the code works, just by looking at it. If this is not the case, you need to rewrite the code.|
|CO||Do not over-comment.|
Very many or very long comments (especially within the body of a function) are more distracting than helpful. Long comments may appear at the top of a file or section of code if you need to explain the overall design of the code or refer to any sources that have more information about the algorithms or data structures. All other comments in the file should be as short as possible. Judicious choice of variable names can help minimize the need for comments.
Avoid comments that state the obvious:
|CE||Use proper English.|
|Comments need not always be written in complete sentences, but when they are, standard rules of English grammar apply. Spelling also counts.|
|FS||Write simple functions.|
|"Functions should be short and sweet, and do just one thing. They should fit on one or two screenfuls of text (the ISO/ANSI screen size is 80x24, as we all know) and do one thing and do that well." From the Linux kernel coding style|
|FD||No dead code.|
|Do not leave unused or commented out code in your submission. It detracts from the story line of your program. If you are worried about saving this code for later, copy it to a different file or use version control.|
|PI||No incomplete Cases.|
|Incomplete pattern matches are flagged with compiler warnings, which are tantamount to errors for grading purposes. Thus, if your program exhibits this behavior, the problem will get no points.|
|PF||Match in the function arguments.|
Tuples, records and datatypes can be deconstructed using pattern matching. If you simply deconstruct the function argument before you do anything useful, it is better to pattern match in the function argument. Consider these examples:
|PP||Avoid Unnecessary Projections.|
|Prefer pattern matching to projections with function arguments or a value declarations. Using projections is okay as long as it is infrequent and the meaning is clearly understood from the context. The above rule shows how to pattern-match in the function arguments.|
|PN||Combine nested cases.|
Rather than nest case expressions, you can combine them by pattern matching against a tuple, provided the tests in the
|VL||Don't rewrite library functions.|
|The Haskell library has a great number of functions and data structures -- use them! Often students will recode
|Remember that the type of the condition in an
|VM||Other common misuses.|
Welcome to CIS 552!
See the home page for basic information about the course, the schedule for the lecture notes and assignments, the resources for links to the required software and online references, and the syllabus for detailed information about the course policies.