Programming notes: Difference between revisions

From csml-wiki.northwestern.edu
Jump to navigation Jump to search
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
This is a collection of recommendations on programming style.
This is a collection of miscellaneous thoughts on coding style recommended for use in our lab.

=== Editor ===


<ol>
<ol>


<li>In general, use [http://www.gnu.org/software/emacs/ Emacs]. GNU Emacs for OS X can be downloaded [http://emacsformacosx.com/ here].</li>
This is a list of miscellaneous thoughts on coding style recommended for use in our lab.

<li>In a pinch, use vi. On our Linux systems as well as on OS X, this is actually [http://en.wikipedia.org/wiki/Vim_%28text_editor%29 vim], which offers syntax highlighting. (Note: on OS X, syntax highlighting seems to be disabled by default. Enable it by adding "syntax on" to your ~/.vimrc file.)</li>

</ol>


=== Notes on C ===
=== Notes on C ===
Line 9: Line 15:
<ol>
<ol>


<li>Don't put multiple commands on a single line. One command per line.</li>
<li>For symmetry reasons, I dislike this layout:

<li>I dislike this layout because of its lack of symmetry
<pre>
<pre>
if (i==2){
if (i==2) {
i++;
i++;
}
}
</pre>
</pre>
and think this is cleaner
Far cleaner is this:
<pre>
<pre>
if (i==2)
if (i==2)
Line 22: Line 30:
}
}
</pre>
</pre>
However, I've now [http://en.wikipedia.org/wiki/Indent_style#Styles learned] that this is a matter of strong debate, and I definitely defer to [http://en.wikipedia.org/wiki/Indent_style#K.26R_style Kernighan & Ritchie] on this (but strongely side with [http://en.wikipedia.org/wiki/Indent_style#Variant:_Stroustrup Stroustrup] on the "else" statement). So, given this situation, choose either of these styles, but be consistent.
</li>
</li>


Line 42: Line 51:


<li>
<li>
The following is an amazingly common programming mistake:
The following is a surprisingly common programming mistake:
<pre>
<pre>
double x, y;
double x, y;
Line 50: Line 59:
}
}
</pre>
</pre>
Due to rounding errors, you should not test for exact equality between two floating point numbers (or a floating point number and a constant). Instead, do:
You should not test for exact equality between two floating point numbers, since this comparison may fail due to rounding errors. Instead, do:
<pre>
<pre>
#define EPS 1e-9 /* or some other threshold small enough for your purposes */
#define EPS 1e-9 /* or some other threshold small enough for your purposes */

double x, y;
double x, y;
if (fabs(x-y) < EPS)
if (fabs(x-y) < EPS)

Latest revision as of 15:21, 7 April 2015

This is a collection of miscellaneous thoughts on coding style recommended for use in our lab.

Editor

  1. In general, use Emacs. GNU Emacs for OS X can be downloaded here.
  2. In a pinch, use vi. On our Linux systems as well as on OS X, this is actually vim, which offers syntax highlighting. (Note: on OS X, syntax highlighting seems to be disabled by default. Enable it by adding "syntax on" to your ~/.vimrc file.)

Notes on C

  1. Don't put multiple commands on a single line. One command per line.
  2. I dislike this layout because of its lack of symmetry
    if (i==2) {
            i++;
    }
    

    and think this is cleaner

    if (i==2)
    {
            i++;
    }
    

    However, I've now learned that this is a matter of strong debate, and I definitely defer to Kernighan & Ritchie on this (but strongely side with Stroustrup on the "else" statement). So, given this situation, choose either of these styles, but be consistent.

  3. For clarity, I prefer indentations to be 8 spaces (not 4). If you follow this, you'll be in good company. Insert the following snippet in your ~/.emacs file:
    (defconst my-c-style
            '((c-basic-offset       . 8)
              (c-offsets-alist      . ((substatement-open . 0)
                                      )
              )
             )
     "C Style")
    (defun my-c-mode-common-hook ()
            (c-add-style "personal" my-c-style t)
            (setq fill-column 135)
    )
    (add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
    
  4. The following is a surprisingly common programming mistake:
    double x, y;
    if (x == y)
    {
            do_something();
    }
    

    You should not test for exact equality between two floating point numbers, since this comparison may fail due to rounding errors. Instead, do:

    #define EPS 1e-9 /* or some other threshold small enough for your purposes */
    
    double x, y;
    if (fabs(x-y) < EPS)
    {
            do_something();
    }