P
P
Pavel Rybin2010-11-11 12:56:55
JavaScript
Pavel Rybin, 2010-11-11 12:56:55

How to assign default values ​​to variables?

It often happens that some variables need to be assigned a default value, if not given otherwise. Examples could be optional function arguments, data from localStorage, etc. There are different ways to do this, but they boil down to one meaning:
check if a value is assigned / if yes, then perform the next task / if not, then assign a default value and perform the following task.
A couple of possible code examples:

// 1-й вариант<br/>
if (!argument) argument = 'value';<br/>
<br/>
// 2-й вариант<br/>
argument = argument || 'value';

The question is, which of the following options is the best? Which one is better to use?

Answer the question

In order to leave comments, you need to log in

10 answer(s)
S
SergeyGrigorev, 2010-11-11
@drfisher

The first option is the most used and easier to read. I would recommend sticking with it.
I showed my colleagues the second option ... many did not immediately understand it. Therefore, it is better not to use it if you work in a team.

E
exIV, 2010-11-11
@exIV

IMHO the first, because it is more "native"

B
bit, 2010-11-11
@bit

You gave two equivalent options. The first one is the most versatile. The second one is more language-oriented. The choice is what you like.
One of the options (I myself always try to do this) is the initial initialization of variables. Before performing all the actions, you describe the variables and assign them default values. If the appropriate parameters are set, the values ​​of the variables are updated.
If you do not assign the values ​​of the arguments to certain variables, but directly access these arguments, then the second version of the notation, in my opinion, is more beautiful. And the first one is more readable for a person who is poorly familiar with additional language constructs like the 2nd option. plus the second option can generate more compact code, but it is unlikely that such savings will give in the general background :)
Type 2 options I personally try to use only if the arguments in the expression are short enough. Too bulky construction is unreadable. Forget it, then you'll figure out what you meant to say :)

T
Tagir Valeev, 2010-11-12
@tagir_valeev

In Perl 5.10+, by the way, there is a special operator for this: $arg // "value" - the right side is evaluated not on boolean falsy, but on the left side undef. Accordingly, there is such a construction: $arg //= "value"; - a short and correct way to solve the problem.

H
habrrich, 2010-11-13
@habrrich

Yes, don't bother.
Use the one you like in your own scripts and the one you already use in others.

N
nutz, 2010-11-11
@nutz

to the piggy bank
!isset($ar) && $ar = 1;

T
Tagir Valeev, 2010-11-11
@tagir_valeev

Both options are dangerous if applied without thinking. In many languages, the condition tests the argument not only for "uncertainty", but also for equality to the number 0, the empty string, or even the string "0". I would write slowly but surely:

if(argument == undefined) argument = "value";

Sometimes a similar construction is wrapped in a simple function:
function default(val, defval) {
  return val == undefined?defval:val;
}
...
argument = default(argument, "value");
Such a function, for example, is built into the AviSynth scripting language. In Java, the Properties class (hash table add-on) has a getProperty(key, defValue) method (will return defValue if there is no entry with the key key in the table).

V
vanxant, 2010-11-12
@vanxant

Both methods are not working.
Suppose the function has a feature for which the last, optional parameter is responsible, equal to true by default.
Now attention, let's try to turn it off:
// argument=false passed to the function
// 1st option
if (!argument) argument = 'true';
//agrument = true
// 2nd option
argument = argument || 'true';
//same, argument=true

M
maximw, 2010-11-12
@maximw

Both options are unacceptable. Because an implicit cast of the type of the argument variable to a Boolean value is used. When casting the type, the current value, which is completely legal according to the logic of the program, can be cast to false, and be replaced by the default one.
It is necessary to check the existence of a variable and / or check for emptiness (null).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question