[R] Google style

Duncan Murdoch murdoch at stats.uwo.ca
Tue Sep 1 12:25:53 CEST 2009


Jim Lemon wrote:
> Duncan Murdoch wrote:
>   
>> On 8/31/2009 11:50 AM, Mark Knecht wrote:
>>     
>>> On Mon, Aug 31, 2009 at 6:36 AM, Terry Therneau<therneau at mayo.edu> 
>>> wrote:
>>> <SNIP>
>>>       
>>>> The authors borrowed so much else from C, the semicolon would have been
>>>> good too.
>>>>         
> Something I have thought myself.
>   
>>>  
>>>
>>> I know real R coders will chuckle
>>>       
>> I'd say cringe, rather than chuckle.  This is going to make you waste 
>> a lot of time some day, when you stare and stare at code like Terry's 
>> and can't figure out what's wrong with it:
>>
>>     zed <- function(x,y,z) {
>>            x + y
>>                  +z;
>>           }
>>
>> The value of the function is +z, not x+y+z, even though the C part of 
>> your brain made you type it that way and reads it as one statement in 
>> the body, not two.
>>     
> This is getting interesting. One habit I have developed in R to 
> emphasize a line continuation is to always write the above as:
>
> zed<-function(x,y,z) {
>  x+y+
>  z
> }
>   

That's a good habit.  An alternative is to put parentheses around the 
expression:

     (x + y
          + z)

will work.
> The trailing operator signalling to me and the interpreter that there's 
> more to come. A semicolon after the z would be innocuous. Now I know 
> that this marks me as a crabby old fart who learned to program on 
> Hollerith cards where there had to be firm conventions on when a line of 
> code ended. Still, given the moiety of global warming attributable to 
> endless discussions about how many spaces should be used for 
> indentation, I think the use of the semicolon as a personal aid to 
> interpretation is at worst a harmless affectation.
>   

I think it's worse.  To me, it's like putting in a comment that is 
wrong, or writing code like this:

  one <- 2
  x <- x + one

Code has meaning, it's not just a bunch of binary instructions to the 
computer.  If the meaning and the look of the code clash, it is going to 
lead to problems.

Duncan Murdoch
> Jim
>
> ______________________________________________
> R-help at 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.
>




More information about the R-help mailing list