Personal tools

Means of expression

From HaskellWiki

Revision as of 11:24, 4 March 2008 by Lemming (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, 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.

Contents

1 Examples

1.1 Types instead of comments

1.2 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

1.3 Functions instead of comments

2 See also

Johannes Waldmann: Haskell mit Stil