Re: [Rd] Best practices for writing R functions

From: Brian G. Peterson <brian_at_braverock.com>
Date: Tue, 26 Jul 2011 20:18:22 -0500

On Tue, 2011-07-26 at 15:19 -0700, Davor Cubranic wrote:
> On 2011-07-23, at 5:57 AM, Alireza Mahani wrote:
>
> > Another trick to reduce verbosity of code (and focus on algorithm logic
> > rather than boilerplate code) is to maintain a global copy of variables (in
> > the global environment) which makes them visible to all functions (where
> > appropriate, of course). Once the development and testing is finished, one
> > can tidy things up and modify the function prototypes, add lines for
> > unpacking lists inside functions, etc.
>
> I think you'd be better off to stay away from such tricks. It's asking for trouble later on, because unless you have really good unit tests it is very easy to miss a variable during "tidying up" and end up with code that works fine in your development environment but is full of bugs once you distribute it to others.

Isn't this specifically one of the things that environment are *for*?

Have your package/script/functions create an environment, and store 'loose variables' there. Use get/assign to manage. Don't clutter .GlobalEnv.

-- 
Brian

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Wed 27 Jul 2011 - 01:30:10 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

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 Fri 29 Jul 2011 - 07:50:12 GMT.

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

list of date sections of archive