jldupont
jldupont

Reputation: 96716

Why would one wrap a code block in an `if (1)` statement? Isn’t that redundant?

if (1) {
  google_conversion_value = 1;
}

What is the meaning of the above statement? I mean, this looks like it will always execute so why bother with the if statement?

Upvotes: 6

Views: 145

Answers (8)

medwards
medwards

Reputation: 218

This is actually the javascript recommended by Google on http://support.google.com/adwords/bin/answer.py?hl=en&answer=1722054#nocomments (click on Step 2 for the sample HTML)

Upvotes: 0

McStretch
McStretch

Reputation: 20645

Perhaps the if statement used to check for a legitimate conditional, and then someone replaced it with a truthy value for testing/debugging/etc.

You're right, it will always execute because 1 is truthy. I would go through your source control history and investigate that line to see if it used to contain a real conditional. If the conditional was always 1, then it's likely a debugging statement. Otherwise someone might have meant for it to be a temporary change, and may not have meant to check that in (which could be bad).

Upvotes: 2

Ayyash
Ayyash

Reputation: 4399

actually, this happens when the "if" condition is driven from server, so instead of doing the right thing and not produce the script when the condition is false, they do something like this:

if (<% if (my_server_condition) then Response.Write("1") else Response.Write("0") %>){
    // code goes here
}

Upvotes: 2

ThiefMaster
ThiefMaster

Reputation: 318508

There are two likely explanations:

  1. It's a leftover from debugging.
  2. The file containing this code is generated dynamically and the original sourcecode contains something like if(<?php echo $some_stuff_enabled; ?>)

However, in the latter case it would have been cleaner to output that code block only if the condition is met - but maybe it's used in some crappy template engine that just allows replacements but no conditionals...

Upvotes: 4

onteria_
onteria_

Reputation: 70497

I'm not sure where this code is from, but as you indicated it will always execute. As for why you'd do this, there are times where you want to see what the result of branch code would be, without having to setup an environment. In this case you can comment out the actual value and replace it with if(1) instead for testing:

// if( ... some hard to achieve condition )
if (1) {
  // Now you can see what happens if this value is set quickly
  google_conversion_value = 1;
}

Of course the problem with this is that it's sometimes easy to forget to remove the if(1) and uncomment the proper condition.

Upvotes: 0

BenM
BenM

Reputation: 53198

More than likely left in from a debug release or something similar. You're right, it will always execute. It could also have been done like this so that it can be easily enabled / disabled by setting the if to 0. Perhaps the developer intended to use it as a flag somewhere else in the code?

Upvotes: 2

ADW
ADW

Reputation: 4080

Potentially because the person writing the code wanted an easy way to turn it off and on again, this is especially useful if there is a lot of code inside the block (not the case here).

Another possibility is that the original programmer couldn't be bothered writing the logic or, more likely, it hadn't been specified so the "if" was left as a placeholder.

Upvotes: 2

Dutchie432
Dutchie432

Reputation: 29160

I've seen this before, and I've always assumed it was a remnant of some old condition that was no longer needed, but never removed. I can't see any actual reason to do something like that otherwise.

Upvotes: 2

Related Questions