I am wondering if people on the list could recommend books that they have found helpful about programming concepts and style? I often find that students write R programs by copying existing code but could really benefit from the understanding of more general programming ideas. An example would be to avoid writing functions which attempt to modify their parameters. Another principle would be not to write programs with numbers used as constants but to assign them to named objects as in n <- 120 # number of observations p <- 10 # number of variables near the beginning of a program rather than using "10" and "120" throughout the script. This sort of stuff is not specifically R but can be a problem for students with little programming background. I am happy to summarise responses. Murray Jorgensen -- Dr Murray Jorgensen http://www.stats.waikato.ac.nz/Staff/maj.html Department of Statistics, University of Waikato, Hamilton, New Zealand Email: maj at waikato.ac.nz Fax 7 838 4155 Phone +64 7 838 4773 wk Home +64 7 825 0441 Mobile 021 1395 862
I try to use a functional programming style. I define functions within functions when it is helpful in terms of information hiding. I avoid writing functions with side-effects as much as possible, so the only communication of the called function with the caller function is through the arguments and the returned value. I try to keep the code as simple and clear as possible (this is one of the things I fail at most). An appropriate amount of comments is also useful, especially when returning to old code after a long break. OOP is useful for really big projects, but I find OOP unnecessarily complicated for small jobs. I found "Code Complete", by McConnell (http://www.cc2e.com/) to be very helpful. I'm sure there are other books around with similar tips. Before I switched to R, I used XLisp-Stat. I found learning Lisp to be a really good way to learn good programming practices. Good Lisp code is the closest thing you can get to poetry in computer programming. "Lisp Style & Design", by Miller and Benson was helpful. I'd like to see an "S Style & Design." Cheers, Simon. On Fri, 2008-06-20 at 14:35 +1200, Murray Jorgensen wrote:> I am wondering if people on the list could recommend books that they > have found helpful about programming concepts and style? I often find > that students write R programs by copying existing code but could really > benefit from the understanding of more general programming ideas. An > example would be to avoid writing functions which attempt to modify > their parameters. Another principle would be not to write programs with > numbers used as constants but to assign them to named objects as in > > n <- 120 # number of observations > p <- 10 # number of variables > > near the beginning of a program rather than using "10" and "120" > throughout the script. > > This sort of stuff is not specifically R but can be a problem for > students with little programming background. > > I am happy to summarise responses. > > Murray Jorgensen >-- Simon Blomberg, BSc (Hons), PhD, MAppStat. Lecturer and Consultant Statistician Faculty of Biological and Chemical Sciences The University of Queensland St. Lucia Queensland 4072 Australia Room 320 Goddard Building (8) T: +61 7 3365 2506 http://www.uq.edu.au/~uqsblomb email: S.Blomberg1_at_uq.edu.au Policies: 1. I will NOT analyse your data for you. 2. Your deadline is your problem. The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. - John Tukey.
2008/6/20 cmr.Pent at gmail.com <cmr.Pent at gmail.com>:> On 20 ???, 11:06, Wacek Kusnierczyk > <Waclaw.Marcin.Kusnierc... at idi.ntnu.no> wrote: >> the result may be that the more beautiful the code, the more the performance >> sucks. > > Sad but true.If you do nothing to your code, in 18 months time its performance will have doubled because computers will have become faster. Your code will not get easier to understand by itself. Hadley -- http://had.co.nz/
on 06/20/2008 10:04 AM hadley wickham wrote:> If you do nothing to your code, in 18 months time its performance will > have doubled because computers will have become faster. Your code > will not get easier to understand by itself.I suspect a good "fortunes" candidate... :-) Marc
hadley wickham wrote:> 2008/6/20 cmr.Pent at gmail.com <cmr.Pent at gmail.com>: > > If you do nothing to your code, in 18 months time its performance will > have doubled because computers will have become faster. Your code > will not get easier to understand by itself.Very nicely put .. and true too!
On Fri, Jun 20, 2008 at 10:04:46AM -0500, hadley wickham wrote:> 2008/6/20 cmr.Pent at gmail.com <cmr.Pent at gmail.com>: > > On 20 ???, 11:06, Wacek Kusnierczyk > > <Waclaw.Marcin.Kusnierc... at idi.ntnu.no> wrote: > >> the result may be that the more beautiful the code, the more the performance > >> sucks. > > > > Sad but true. > > If you do nothing to your code, in 18 months time its performance will > have doubled because computers will have become faster. Your code > will not get easier to understand by itself.Very true. And I would like to contribute one of my favorite quotations: "It is easier to optimize correct code than to correct optimized code." --Bill Harlan cu Philipp -- Dr. Philipp Pagel Lehrstuhl f?r Genomorientierte Bioinformatik Technische Universit?t M?nchen Wissenschaftszentrum Weihenstephan 85350 Freising, Germany http://mips.gsf.de/staff/pagel