[R] Google's R Style Guide

(Ted Harding) Ted.Harding at manchester.ac.uk
Sat Aug 29 20:26:55 CEST 2009


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.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 29-Aug-09                                       Time: 19:26:51
------------------------------ XFMail ------------------------------




More information about the R-help mailing list