[R] R programming style

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Mon Feb 11 18:37:37 CET 2008


Roland Rau wrote:
> Hi,
> 
> I think using Emacs+ESS [1,2] is always a good starting point for a 
> clear layout with consistent and meaningful indentation.
> 
> I don't know how other people think about it, but in my opinion, 
> "Elements of Programming Style" by Kernighan and Plauger is still an 
> interesting read -- although their programs are either Fortran or PL/1 
> and the book itself is 30 years or old. Of course, I am not always 
> successful but at least I try to incorporate their 'mantras':
> - write clearly, don't be too clever [3]
> - say what you mean, simply and directly
> - use library functions
> - write clearly -- don't sacrifice clarity for "efficiency"
> - let the machine do the dirty work
> - parenthesize to avoid ambiguity
> - 10.0 times 0.1 is hardly ever 1.0
> - ...

  Reminiscent of The Zen Of Python, which you get by typing 'import 
this' at the python prompt:

 >>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

  [Note that Guido Van Rossum, inventor of Python, is Dutch]

Barry



More information about the R-help mailing list