Re: [R] R programming style

From: Scillieri, John <John.Scillieri_at_constellation.com>
Date: Mon, 11 Feb 2008 14:50:13 -0500

I second that, Code Complete is a great book! For anyone interested in improving their code no matter what language, (it has a C++/Java-type focus but is definitely applicable to R), it would definitely be a good place to start.

I've read some negative reviews claiming that everything he writes is 'obvious' (use good variable names, short concise functions, limit nested conditionals, etc) but on more than one occasion I've gone back over the book and thought of new places to improve my code.

HTH,
John

-----Original Message-----
From: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-project.org] On Behalf Of Earl F. Glynn
Sent: Monday, February 11, 2008 2:30 PM
To: r-help_at_stat.math.ethz.ch
Subject: Re: [R] R programming style

"David Scott" <d.scott_at_auckland.ac.nz> wrote in message news:alpine.LRH.1.00.0802112343200.27944_at_stat12.stat.auckland.ac.nz...
>
> Can anyone provide further pointers to good style?

While not written for R specifically, the book "Code Complete: A Practical Handbook of Software Construction" (2nd Edition) discusses a number of good concepts for writing good code in any language: http://www.amazon.com/Code-Complete-Practical-Handbook-Construction/dp/0 735619670

In particular, Part IV "Statements" gives a number of useful suggestions by type of statement, e.g., straight-line code, conditionals, loops, ...

There are some practices used in R that I think should be improved. For example, many years ago I was taught in a software engineering class that the use of "magic numbers" was a bad practice, yet we find magic numbers used in R in many places.

Instead of using "1" or "2" in an "apply", I'll write something like this trying for some sort of mnemonic

apply(x, BY.ROW<-1, sum)
or
apply(z, BY.COL<-2, mean)

I find BY.ROW or BY.COL to be more mnemonic than the magic numbers 1 and 2.

The "sides" 1, 2, 3, and 4 in an axis statement should have some sort of mnemonic definition, too, perhaps:

axis(BOTTOM<-1, ...)

But I believe I was ostracized in this E-mail list the last time I suggested such mnemonics instead of magic numbers.

efg
Earl F. Glynn
Bioinformatics
Stowers Institute for Medical Research



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
>>> This e-mail and any attachments are confidential, may contain legal, professional or other privileged information, and are intended solely for the addressee. If you are not the intended recipient, do not use the information in this e-mail in any way, delete this e-mail and notify the sender. CEG-IP1


R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Mon 11 Feb 2008 - 19:55:26 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 11 Feb 2008 - 20:30:13 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive