Cs51 introduction to computer science ii spring 2018 cs51 ocaml style guide

CS Introduction to Computer Science II Spring CS OCAML STYLE GUIDE STUART M SHIEBER Contents Formatting No tab characters column limit No needless blank lines Use parentheses sparely Spacing for operators and delimiters Indentation Indenting if expressions Indenting let expressions Indenting match expressions Documentation Comments before code Comment length should match abstraction level Multi-line commenting Naming and declarations Naming conventions Use meaningful names Function declarations and type annotations Avoid global mutable variables When to rename variables Order of declarations in a structure Pattern matching No incomplete pattern matches Pattern match in the function arguments when possible Pattern match with as few match expressions as necessary Misusing match expressions Avoid using too many projection functions Don ? t use List hd or List tl at all Date January This style guide is reworked from a long line of style guides for courses at Princeton University of Pennsylvania and Cornell including Cornell CS U Penn CIS and CIS and Princeton COS All this shows the great power of recursion Also the joke about recursion was stolen from COS CCS OCaml Style Guide Verbosity Reuse code where possible Do not abuse if expressions Don ? t rewrap functions Avoid computing values twice Other common infelicities This guide provides some simple rules of good programming style both general and OCaml-speci ?c developed for the Harvard course CS The rules presented here tend to follow from a small set of underlying principles Consistency Similar decisions should be made within similar contexts Brevity ??Everything should be made as simple as possible but no simpler ? attr Albert Einstein Clarity Code should be chosen so as to communicate clearly to the human reader Transparency Appearance should summarize and re ect structure Like all rules those below are not to be followed slavishly but seen as instances of these underlying principles These principles may sometimes be in con ict in which case judgment is required in ?nding the best way to write the code This is one of the many ways in which programming is an art not just a science This guide is not complete For more recommendations from the OCaml developers themselves see the o ?cial OCaml guidelines Formatting Formatting concerns the layout of the text of a program on the screen or page such issues as vertical alignments and indentation line breaks and whitespace To allow for repeatable formatting code is typically presented with a ?xed-width font in which all characters including spaces take up the same horizontal pitch No tab characters You may feel inclined to use tab characters ascii x to align text Do not do so use spaces instead The width of a tab is not uniform across all renderings and what looks good on your machine may look terrible on another ? s especially if you have mixed spaces and tabs Some text editors map the tab key to a sequence of spaces rather than a tab character in this case it ? s ?ne to use the tab key column

  • 32
  • 0
  • 0
Afficher les détails des licences
Licence et utilisation
Gratuit pour un usage personnel Attribution requise
Partager