Means of expression

From HaskellWiki
Revision as of 11:24, 4 March 2008 by Lemming (talk | contribs) (introduction)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Programming languages have different types of means of expression:

  • Primary means of expression: variables, types, parentheses, etc. in general all things that are relevant to the compiler
  • Secondary means of expression: layout, comments, etc. that is language elements that are irrelevant for the compiler, and thus are made exclusively for the human reader

In Haskell (as well as in Python) one has to weaken that because layout is interpreted by the compiler.

It is generally good style to remember the rule:

Prefer primary means of expression to secondary ones!

The reason is that, elements that are relevant to the compiler are checked by the compiler and can be processed by documentation, analysis and refactoring tools. Thus it is not good style to comment intensively if there are better ways to express our ideas.

Examples

Types instead of comments

Expressive function names instead of comments

I recently saw code like

-- | first name
fn :: Person -> String

-- | surname
sn :: Person -> String

It was hard to read the program because there were several other two-character function names to remember, and the comment was only attached to the function definition, not the calls. The comments would be better replaced by expressive function names like

firstName :: Person -> String
surname :: Person -> String

Functions instead of comments

See also

Johannes Waldmann: Haskell mit Stil