[R] Google's R Style Guide

David Scott d.scott at auckland.ac.nz
Mon Aug 31 11:54:34 CEST 2009


(Ted Harding) wrote:
> On 29-Aug-09 17:51:54, diegol wrote:
>> Max Kuhn wrote:
>>> Perhaps this is obvious, but Ive never understood why this is the
>>> general convention:
>>>
>>>> An opening curly brace should never go on its own line;
>>> I tend to do this:
>>>
>>> f <- function()
>>> {
>>>   if (TRUE)
>>>     {
>>>       cat("TRUE!!\n")
>>>     } else {
>>>       cat("FALSE!!\n")
>>>     }
>>> }
>> I favor your approach. BUT I add one more level of indentation.
>> Your function would look like:
>>
>> f <- function()
>>   {
>>     if (TRUE)
>>       {
>>         cat("TRUE!!\n")
>>       } else {
>>         cat("FALSE!!\n")
>>       }
>>   }
>>
>> This way I quickly identify the beginning of the function, which is
>> the one line at the top of the expression AND sticking to the left
>> margin.
>> In your code you use this same indentation in the if/else construct.
>> I find it also useful for the function itself.
> 
> When I want to rely on indentation and vertical alignments to keep
> track of program structure, I would tend to write the above like
> 
>   f <-
>   function()
>   { if (TRUE)
>     {
>       cat("TRUE!!\n") 
>     } else
>     {
>       cat("FALSE!!\n")
>     }
>   }
> 
> so that an opening "{" is aligned with the keyword it is associated
> with, and then at the end of the block so also is the closing "}".
> 
> However, in this case (if I keep all the "{...}" for the sake of
> structure) I would also tend to "save on lines" with
> 
>   f <-
>   function()
>   { if (TRUE)
>     { cat("TRUE!!\n")  } else
>     { cat("FALSE!!\n") }
>   }
> 
> which is still clear enough for me. This probably breaks most
> "guidelines"! But in practice it depends on what it is, and on
> how readily I find I can read it.
> 
> Ted.
> 

I have to say Ted, I find this as ugly as sin and you would have to 
break my legs to make me code like this.

I am with Hadley on not taking extra lines and I think this is really 
unclear because it is so disjointed. And the 'else' way over to the 
right I just think is crazy.

It just goes to show how personal this can be because despite my 
loathing this code I know Ted to be a thoughtful and experienced R user.

I think this discussion is valuable, and have previously asked about 
style which I think is very important. Base R does suffer from very 
inconsistent naming and as I think Duncan said it makes it very 
difficult sometimes to remember names when you have variations in case 
and separators as with things related to system.

David



_________________________________________________________________
David Scott	Department of Statistics
		The University of Auckland, PB 92019
		Auckland 1142,    NEW ZEALAND
Phone: +64 9 923 5055, or +64 9 373 7599 ext 85055
Email:	d.scott at auckland.ac.nz,  Fax: +64 9 373 7018

Director of Consulting, Department of Statistics




More information about the R-help mailing list