<p>My JavaScript is pretty nominal, so when I saw this construction, I was kind of baffled:</p>

<pre><code>var shareProxiesPref = document.getElementById("network.proxy.share_proxy_settings");
shareProxiesPref.disabled = proxyTypePref.value != 1;
</code></pre>

<p>Isn't it better to do an if on <code>proxyTypePref.value</code>, and then declare the var inside the result, only if you need it?</p>

<p>(Incidentally, I also found this form very hard to read in comparison to the normal usage. There were a set of two or three of these conditionals, instead of doing a single if with a block of statements in the result.)</p>

<hr>

<p><strong>UPDATE:</strong></p>

<p>The responses were very helpful and asked for more context. The code fragment is from Firefox 3, so you can see the code here:</p>

<p><a href="http://mxr.mozilla.org/firefox/source/browser/components/preferences/connection.js" rel="nofollow noreferrer">http://mxr.mozilla.org/firefox/source/browser/components/preferences/connection.js</a></p>

<p>Basically, when you look at the <strong>Connect</strong> preferences window in Firefox, clicking the proxy <strong>modes</strong> (radio buttons), causes various form elements to enable|disable.</p>

[{'answer_id': 27039, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 2, 'selected': False, 'text': "<blockquote>\n  <p>(Incidentally, I also found this form very hard to read in comparison to the normal usage.</p>\n</blockquote>\n\n<p>Not necessarily, although that was my first thought, too. A code should always emphasize its function, especially if it has side effects. If the writer's intention was to emphasize the assignment to <code>sharedProxiesPref.disabled</code> then hey, roll with it. On the other hand, it could have been clearer that the action taking place here is to disable the object, in which case the conditional block would have been better.</p>\n"}, {'answer_id': 27098, 'author': 'pottedmeat', 'author_id': 2120, 'author_profile': 'https://Stackoverflow.com/users/2120', 'pm_score': 3, 'selected': True, 'text': "<p>It depends on the context of this code. If it's running on page load, then it would be better to put this code in an if block.</p>\n\n<p>But, if this is part of a validation function, and the field switches between enabled and disabled throughout the life of the page, then this code sort of makes sense.</p>\n\n<p>It's important to remember that setting disabled to false also alters page state.</p>\n"}, {'answer_id': 27319, 'author': '17 of 26', 'author_id': 2284, 'author_profile': 'https://Stackoverflow.com/users/2284', 'pm_score': 1, 'selected': False, 'text': "<p>It's hard to say what's better to do without more context.</p>\n\n<p>If this code being executed every time that proxyTypePref changes, then you're always going to need set <code>shareProxiesPref.disabled</code>.</p>\n\n<p>I would agree than an if statement would be a bit more readable than the current code.</p>\n\n<blockquote>\n  <p>Isn't it better to do an if on <code>proxyTypePref.value</code>, and then declare the var inside the result, only if you need it?</p>\n</blockquote>\n\n<p>If you're talking strictly about variable declaration, then it doesn't matter whether or not you put it inside an if statement.  Any Javascript variable declared inside a function is in scope for the entire function, regardless of where it is declared.</p>\n\n<p>If you're talking about the execution of <code>document.getElementById</code>, then yes, it is much better to not make that call if you don't have to.</p>\n"}]
['https://Stackoverflow.com/questions/27034', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']