3

Coming from the rubyesque crowd I was very fond of the following 'lazy' initialization pattern.

myhash[:property]||='value'

And sometimes during my career I adapted it to javascript as following:

myhash.property || (myhash.property = 'value')

A co-worker remarked that it's quite irregular, and to be honest I can't remember seeing other people using that pattern.

So my question is; Is it just another innocent initialization flavor or am I unintentionally asking for trouble?

Given the execution flow of conditional statements like that I suppose It is possible to use it as an if statement aswell:

aModel.valid && aModel.save();

or even something more insane..

willYouMarryMe === 'yes' && (function(){
  console.log('Wohoo!');
})();
1
  • Remember that JavaScript has many more false-y values than Ruby: sometimes you want a 0 to stay a 0. As far as code style, I avoid strict side-effects in conditionals, as I find it harder to understand. Commented Jan 2, 2014 at 2:58

1 Answer 1

10

Lazy initialization is often used in JavaScript, but it's more common to see:

namespace.property = namespace.property || 'default';

As far as calling functions is concerned, it's good practice to use if statements where they apply instead of && and ||. Minifiers will often automatically convert the if statements into && and || for production to reduce file size, so it's not necessary to do so directly.

Writing easily readable code makes future maintenance easier:

Good:
if (!foo) {
    bar();
}

Writing code that's harder to read makes code harder to maintain. Particularly because using one clever trick often indicates that other clever tricks are going to be used:

Bad:
foo || bar();

The one major exception to this with regard to functions tends to be when calling console.log:

window.console&&console.log('lorem', 'ipsum');
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that pretty much answers my question. My followup question would be, is it safe to expect that expression evaluation behaves the same across different js-engines? Somewhere i get the feeling that there might not be an explicit prohibition to evaluate the right-hand expression if the former expression is false in the ECMA standard. ( Gee.. I probably should go dig there for further info )
@telamon, it sounds like you're asking whether the short-circuit evaluation behavior of the && and || operators is part of JavaScript as a language, or whether it's just part of some implementations. If I've interpreted your question correctly the answer is that it's a defined behavior as part of the ECMAScript standard (&& and || will not evaluate the second argument unless the first argument is truthy or falsey respectively). If I've misunderstood your question, please elaborate.
Thanks, you nailed it! In retrospect that was exactly the question I've been searching for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.