CelineDion
CelineDion

Reputation: 1088

"If (FALSE)" with no condition explicitly stated

I've received some code from someone who apparently likes to use the following syntax:

if(FALSE) {
    opt <- list("cores" = 1, "degradation" = TRUE, "test" = TRUE)
}

What does this mean? If what is FALSE? The last condition evaluated? I'm confused

Upvotes: 5

Views: 1032

Answers (2)

Waldi
Waldi

Reputation: 41240

This one of the available techniques to comment out multiple lines of code.

  • advantages :

    • you don't need to use # for every line
    • as pointed out by @Ben Bolker, code syntax highlighting is still active so that you know that you can anytime switch it on by using if (T) instead of if (F)
  • disadvantage :

    • you don't see as clearly as with # what is commented out

Upvotes: 4

Konrad Rudolph
Konrad Rudolph

Reputation: 545865

If what is FALSE?

FALSE itself. FALSE is a logical value. And the syntax for if is

if (condition) …

That “condition” can be any expression that evaluates to something that R can interpret as a logical value. FALSE is such an expression. So if (FALSE) is basically the same as writing if (a condition that’s never true).

So this construct is quite meaningless: the whole if block will always be skipped, you might as well remove it. As explained in the other answer, some people use this construct to easily disable larger chunks of code. I don’t recommend this practice: code that’s never evaluated is called dead code, and it’s clutter and thus incurs technical debt (all these are effectively terms that say that it decreases the overall code quality).

Upvotes: 3

Related Questions