Programming Concepts and Philosophy

classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

Programming Concepts and Philosophy

Murray Jorgensen
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: [hidden email]                                Fax 7 838 4155
Phone  +64 7 838 4773 wk    Home +64 7 825 0441    Mobile 021 1395 862

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Programming Concepts and Philosophy

Simon Blomberg-4
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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Programming Concepts and Philosophy

Wacek Kusnierczyk
Simon Blomberg wrote:

> 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."
>  

i support this view.  i found sicp (structure and interpretation of
computer programs, by abelson & sussman, a real classic in the field)
particularly enlightening, although it certainly is a bit outdated in
many respects -- but just a bit.  functional programming style is great,
but beware -- r will create (afaik, experts please correct me if this is
wrong) a deep copy of about any object you send as an argument to a
function (if only when it is actually used inside the function), and the
result may be that the more beautiful the code, the more the performance
sucks.

in pure functional programming, a function cannot change any part of the
object passed to it as an argument, so there's no need for making a
local copy.  in r everything can happen as you can deparse the call to a
function inside the function body, and you can never tell whether the
object you pass to a function will be changed or not, given only the
function's signature.

vQ

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Programming Concepts and Philosophy

Jan T. Kim
On Fri, Jun 20, 2008 at 09:06:24AM +0200, Wacek Kusnierczyk wrote:

> Simon Blomberg wrote:
> > 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."
> >  
>
> i support this view.  i found sicp (structure and interpretation of
> computer programs, by abelson & sussman, a real classic in the field)
> particularly enlightening, although it certainly is a bit outdated in
> many respects -- but just a bit.  functional programming style is great,
> but beware -- r will create (afaik, experts please correct me if this is
> wrong) a deep copy of about any object you send as an argument to a
> function (if only when it is actually used inside the function), and the
> result may be that the more beautiful the code, the more the performance
> sucks.

Not necessarily -- R's "promise" mechanism quite reasonably ensures
that copies of parameter objects are only made upon modifying these
objects.

However, on a more fundamental "programming philosophy" note, the fact
that R does not allow multiple references to one object is a limitation.
One of the fundamental principles of good programming is that there
should be a one-to-one correspondence between instances in the program
and objects in the "real world" (problem domain etc.), and having no
references makes achieving this difficult (and quite impossible for
more complex systems).

Best regards, Jan
--
 +- Jan T. Kim -------------------------------------------------------+
 |             email: [hidden email]                               |
 |             WWW:   http://www.cmp.uea.ac.uk/people/jtk             |
 *-----=<  hierarchical systems are for files, not for humans  >=-----*

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Programming Concepts and Philosophy

Андрей Парамонов
In reply to this post by Wacek Kusnierczyk
On 20 июн, 11:06, Wacek Kusnierczyk
<[hidden email]> wrote:
> the result may be that the more beautiful the code, the more the performance
> sucks.

Sad but true.

Andrey

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Programming Concepts and Philosophy

hadley wickham
In reply to this post by Jan T. Kim
> However, on a more fundamental "programming philosophy" note, the fact
> that R does not allow multiple references to one object is a limitation.
> One of the fundamental principles of good programming is that there
> should be a one-to-one correspondence between instances in the program
> and objects in the "real world" (problem domain etc.), and having no
> references makes achieving this difficult (and quite impossible for
> more complex systems).

This is a principle of object oriented programming, not good
programming in general!

Hadley

--
http://had.co.nz/

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Programming Concepts and Philosophy

Spencer Graves
      Maybe I missed it, but I haven't seen in this tread mention of
Venables and Ripley (2000) S Programming (Springer).  I found it
interesting and useful, though I have not used it as much as MASS --
partly because it is more specialized and it's coverage is not as broad.

      Spencer

hadley wickham wrote:

>> However, on a more fundamental "programming philosophy" note, the fact
>> that R does not allow multiple references to one object is a limitation.
>> One of the fundamental principles of good programming is that there
>> should be a one-to-one correspondence between instances in the program
>> and objects in the "real world" (problem domain etc.), and having no
>> references makes achieving this difficult (and quite impossible for
>> more complex systems).
>>    
>
> This is a principle of object oriented programming, not good
> programming in general!
>
> Hadley
>
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Programming Concepts and Philosophy

Simon Blomberg-4
In reply to this post by Wacek Kusnierczyk
I read the Wizard Book too. Absolutely classic! I agree about paying attention to performance, of course. Lisp programmers bend over backwards to write tail-recursive functions for similar performance reasons. So you have to pay attention to the language's idiosyncracies. But the time spent coding is usually much longer than the eventual run time. Learning to write well-designed code can do more to save time, even if the run time is slightly longer than optimal. If run time is an issue, then switching to C or Fortran etc. might be the best bet. But even in that case, using R for prototyping code can provide valueable insights.

Cheers,

Simon.

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
T: +61 7 3365 2506
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.



-----Original Message-----
From: Wacek Kusnierczyk [mailto:[hidden email]]
Sent: Fri 20/06/2008 5:06 PM
To: R help
Cc: Simon Blomberg
Subject: Re: [R] Programming Concepts and Philosophy
 
Simon Blomberg wrote:

> 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."
>  

i support this view.  i found sicp (structure and interpretation of
computer programs, by abelson & sussman, a real classic in the field)
particularly enlightening, although it certainly is a bit outdated in
many respects -- but just a bit.  functional programming style is great,
but beware -- r will create (afaik, experts please correct me if this is
wrong) a deep copy of about any object you send as an argument to a
function (if only when it is actually used inside the function), and the
result may be that the more beautiful the code, the more the performance
sucks.

in pure functional programming, a function cannot change any part of the
object passed to it as an argument, so there's no need for making a
local copy.  in r everything can happen as you can deparse the call to a
function inside the function body, and you can never tell whether the
object you pass to a function will be changed or not, given only the
function's signature.

vQ


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Programming Concepts and Philosophy

hadley wickham
In reply to this post by Андрей Парамонов
2008/6/20 [hidden email] <[hidden email]>:
> On 20 июн, 11:06, Wacek Kusnierczyk
> <[hidden email]> 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/
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Programming Concepts and Philosophy

Marc Schwartz
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Programming Concepts and Philosophy

Ramon Diaz-Uriarte-2
In reply to this post by hadley wickham
2008/6/20 hadley wickham <[hidden email]>:

> 2008/6/20 [hidden email] <[hidden email]>:
>> On 20 июн, 11:06, Wacek Kusnierczyk
>> <[hidden email]> 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

But that doesn't seem to be true anymore (if we are talking single
CPU), and it seems not to have been true for at least a few years
(e.g.,
http://www.eecs.berkeley.edu/Pubs/TechRpts/2006/EECS-2006-183.html
for instance, their figure 2 in p. 6).

> will not get easier to understand by itself.
>

No it won't.

Best,

R.

> Hadley
>
>
> --
> http://had.co.nz/
> ______________________________________________
> [hidden email] 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.
>



--
Ramon Diaz-Uriarte
Statistical Computing Team
Structural Biology and Biocomputing Programme
Spanish National Cancer Centre (CNIO)
http://ligarto.org/rdiaz
Phone: +34-91-224-6900 ext. 3019
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Programming Concepts and Philosophy

esmail
In reply to this post by hadley wickham
hadley wickham wrote:
> 2008/6/20 [hidden email] <[hidden email]>:
>
> 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!

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Programming Concepts and Philosophy

Philipp Pagel-5
In reply to this post by hadley wickham
On Fri, Jun 20, 2008 at 10:04:46AM -0500, hadley wickham wrote:

> 2008/6/20 [hidden email] <[hidden email]>:
> > On 20 июн, 11:06, Wacek Kusnierczyk
> > <[hidden email]> 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Programming Concepts and Philosophy

jholtman
To quote Jon Bentley (Programming Pearls):

"The fastest, cheapest, most reliable piece of code is that which
isn't there; design as much out of your code as you design in."

On Mon, Jun 23, 2008 at 3:57 AM, Philipp Pagel <[hidden email]> wrote:

> On Fri, Jun 20, 2008 at 10:04:46AM -0500, hadley wickham wrote:
>> 2008/6/20 [hidden email] <[hidden email]>:
>> > On 20 июн, 11:06, Wacek Kusnierczyk
>> > <[hidden email]> 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
>
> ______________________________________________
> [hidden email] 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.
>



--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Programming Concepts and Philosophy

ssefick
This is a lot like music- a good musician knows better when not to play than
when to (guitar teacher I had 15+ years ago).

stephen

On Mon, Jun 23, 2008 at 8:33 AM, jim holtman <[hidden email]> wrote:

> To quote Jon Bentley (Programming Pearls):
>
> "The fastest, cheapest, most reliable piece of code is that which
> isn't there; design as much out of your code as you design in."
>
> On Mon, Jun 23, 2008 at 3:57 AM, Philipp Pagel <[hidden email]> wrote:
> > On Fri, Jun 20, 2008 at 10:04:46AM -0500, hadley wickham wrote:
> >> 2008/6/20 [hidden email] <[hidden email]>:
> >> > On 20 июн, 11:06, Wacek Kusnierczyk
> >> > <[hidden email]> 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
> >
> > ______________________________________________
> > [hidden email] 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.
> >
>
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem you are trying to solve?
> ______________________________________________
> [hidden email] 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.
>


--
Let's not spend our time and resources thinking about things that are so
little or so large that all they really do for us is puff us up and make us
feel like gods. We are mammals, and have not exhausted the annoying little
problems of being mammals.

-K. Mullis

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.