5

I've been working on writing readable code and styleguides for work. I'm understand the 80 character line limit suggestion.

Occasionally I write a long string of code that becomes less readable if I stick to the 80 character limit.

Here is an example of how I format the code (not 80 character compliant, dashes indicating characters)

0--------1---------2---------3---------4---------5---------6---------7---------8   
df$rslt[df$test == "M. ovipneumoniae by ELISA" |
        df$test == "PCR-Mycoplasma ovipneumoniae"] <- df[df$test == "M. ovipneumoniae by ELISA" |
                                                         df$test == "PCR-Mycoplasma ovipneumoniae",
                                                         "result"]

If I were to follow the 80 character limit I may enter the code as follows

0--------1---------2---------3---------4---------5---------6---------7---------8
df$rslt[df$test == "M. ovipneumoniae by ELISA" |
          df$test == "PCR-Mycoplasma ovipneumoniae"] <- df[df$test == 
                                                             "M. ovipneumoniae 
                                                              by ELISA" |
                                                           df$test == 
                                                             "PCR-Mycoplasma 
                                                              ovipneumoniae",
                                                            "result"]

I find the first example much more readable. Each logical operation is a new line and reads clearly. The second example is easy enough to follow, but becomes convoluted as I reach the 80 character limit. I can read it but I'm breaking strings into multiple rows, single logical operation into multiple rows, etc.

Is it ever acceptable to go over the 80 character limit for longer strings (all potential formatting issues aside)?

Mitch
  • 652
  • 1
  • 8
  • 14
  • 1
    you can add a newline to the right of the assignment `<-` and it will still compile correctly; I usually do this and add, say, two spaces of indentation (or whatever the default is in `RStudio`) in such cases. Also, since the string is repeated, you may consider storing each as `str1` and `str2` (for example), then writing `df$rslt[df$test==str1|df$test==str2]...` – MichaelChirico Aug 24 '15 at 19:33
  • 1
    Just press enter more often and use code indentation. – Rich Scriven Aug 24 '15 at 19:35
  • I don't think he's encountering an error. He's just asked for our opinion. I think the StackOverflow community frowns on opinion driven questions. You could have a look at [this](http://programmers.stackexchange.com/questions/14856/what-popular-best-practices-are-not-always-best-and-why). – Buzz Lightyear Aug 24 '15 at 19:36
  • side note, you may save space by a) using `%in%` instead of `|`, i.e., `df$test %in% c("...")` b) using `data.table`, since `[.data.table` automatically returns the `df` environment so you don't need to extract (`$`) every time you refer to an object, i.e. `df[test %in% c(...),...]`. – MichaelChirico Aug 24 '15 at 19:37
  • @BuzzLightyear indeed... curious that the `coding-style` tag exists, as it seems like every question tagged as such would be similarly plagued... – MichaelChirico Aug 24 '15 at 19:38
  • @MichaelChirico Stole your answer/had the same idea. Happy to delete if you were planning on posting. Arguably, this question should be closed as opinion-based anyways, I suppose. – Frank Aug 24 '15 at 19:39
  • 1
    @Frank no worries, I was only commenting since it'll be closed; also storing both strings in a vector is superior. Have some ups ;-) – MichaelChirico Aug 24 '15 at 19:40
  • @MichaelChirico Yeah. Maybe the topic could be brought up on the meta page about the need of the `coding-style` tag. – Buzz Lightyear Aug 24 '15 at 19:43

1 Answers1

4

First, I'd split off values you're matching:

test_vals = c("M. ovipneumoniae by ELISA", "PCR-Mycoplasma ovipneumoniae")

Then, I'd do as Michael suggested:

df$rslt[ test %in% test_vals ] <-
  df$result[ test %in% test_vals ]   

# or
library(data.table)
setDT(df)[ test %in% test_vals, rslt := result]
Frank
  • 66,179
  • 8
  • 96
  • 180