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)?
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With