From brian at braverock.com  Wed Jul 27 03:18:22 2011
From: brian at braverock.com (Brian G. Peterson)
Date: Tue, 26 Jul 2011 20:18:22 -0500
Subject: [Rd] Best practices for writing R functions
In-Reply-To: <DC70CFDD-72C3-4010-9EEC-B5EB540AD85E@stat.ubc.ca>
References: <1311341191873-3686674.post@n4.nabble.com>
	<4E2993CE.4020903@prodsyse.com>
	<CADwqtCONeEqXTTg0OyDj=OJWeQxNKBm79M=iBg1jdy6JtO_w0Q@mail.gmail.com>
	<6441154A9FF1CD4386AF4ABF141A056D265A9E9B@WMEXOSCD2-N1.bocad.bank-banque-canada.ca>
	<CABdHhvHvB4vV5BymZfSguxCUH745nd6mrAk8EDw8Kk3RPN-C2Q@mail.gmail.com>
	<1311425861566-3688850.post@n4.nabble.com>
	<DC70CFDD-72C3-4010-9EEC-B5EB540AD85E@stat.ubc.ca>
Message-ID: <1311729502.21063.138.camel@brian-desktop>

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



[Rd] Best practices for writing R functions
Brian G. Peterson