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. ______________________________________________ R-help@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.