Context Navigation



Ignore:
Timestamp:
Jan 23, 2008, 1:17:27 AM (18 years ago)
Author:
neil.c.c.brown
Message:

Added the section about writing code for Tock

File:
1 edited

Legend:

Unmodified
Added
Removed
  • docs/trunk/hacking-guide/tock-intro.tex

    r174 r175
    233233\section{Add Your Own Code}
    234234
    235%TODO explan any coding conventions (readability over all else?)
    235Tock, as a project, does not have any particular coding conventions. Lists of module imports should be
    236in alphabetical order, and that's about it. The code is littered with slightly curious code indentation,
    237one or two letter variable names, incredibly long expressions stretching long and wide, various bits of
    238uncommented code, and the use of many similar but different language features (e.g. if/then/else and
    239pattern guards, map and list comprehensions), which may be blamed in varying measures on the current
    240developers!
    241
    242Which is not to say the code is bad, just that there is not tight control on coding style. In general:
    243
    244\begin{enumerate}
    245\item Use your common sense
    246\item Vaguely follow the style of the existing code
    247\item Favour readability and clarity over conciseness and cleverness
    248\item Optimise in terms of algorithms (e.g. O($N \log N$) over O($N^2$)) but never look for small savings.
    249Besides the effort being wasted, it would be very hard in Haskell to judge which of several pieces of
    250code would be faster. The compiler does not have to be blindingly fast, but it does need to be
    251maintainable.
    252\item If a function foo is specifically needed by only the function bar, place foo inside the where
    253clause of bar (unless this is particularly untenable). This keeps the code neater, and foo can always
    254be moved to the top-level later if necessary.
    255\item Try to provide type signatures for every function (i.e. anything of type a -> b -> c) in a where clause.
    256Providing types for values in where clauses is also never a bad thing.
    257\item Favour map, zip, etc over list comprehensions; it is usually neater.
    258\item Never allow any possibility of a non-graceful run-time error. For example, do not use head, which
    259can fail directly with a non-helpful error message. Instead, use a case statement (with a pattern-match),
    260and in the case where the list is empty, use the die/dieP/dieInternal functions to provide a more helpful
    261error message (such as ``list of types was not expected to be empty in function foo''). This is for
    262practical reasons; if we used head everywhere in the code, then when the program failed with ``head: empty list''
    263it would be very hard to work out exactly which instance of head had given the error. Similarly, try to
    264ensure that you either always match all possible cases in pattern-matching, or you provide a default case that
    265then gives an error message. Although this is not quite as crucial, because at least the error message
    266for a failed pattern match gives the relevant line numbers.
    267\end{enumerate}
    268
    269As for other patterns of working: use the Tock mailing list (tock-discuss) for any questions you may have.
    270All questions welcome, simple or complex, and asking there will save time, trouble, and should allow us
    271to improve documentation such as this guide. It is especially worth asking if you want to revamp
    272an existing section of code; other people may know of a reason why this is not wise, or may suggest a
    273good way to go about it.
    236274
    237275\section{Test Your Code}
Note: See TracChangeset for help on using the changeset viewer.

AltStyle によって変換されたページ (->オリジナル) /