How to assign variable default values?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
7 views
It often happens that any variables need to assign the default value if not given otherwise. Examples can be optional function arguments, the data from localStorage, etc. There are different ways to do this, but they boil down to one meaning:

to check, does it have some significance / if Yes, complete the following task / if not, assign a default value and perform the following task.


A couple of possible examples of code:

// 1st option
if (!argument) argument = 'value';

// 2nd variant
argument = argument || 'value';



The question is — which of the options is the best? Which one should I use?
by | 7 views

7 Answers

0 like 0 dislike
The first option is the most used and more understandable to read. I would recommend to stick to it.
Showed colleagues the second option... many did not realize it. So it is better not to use if you work in a team
by
0 like 0 dislike
Personally, I more enjoyable the second option:
\r
\r
argument = argument || 'value';

\r
It is a pity that in JS it is impossible to do so (as in Ruby):
\r
\r
argument ||= 'value';
by
0 like 0 dislike
IMHO the first, because it is more "native"
by
0 like 0 dislike
You gave two equally good options. The first is the most versatile. The second is more ategorization. Choice someone like that.
One of the options (I try to do always) — initialization of variables. To complete the actions describe variables and assign them default values. If the appropriate parameters are set, the variable values are updated.
If you do not assign the argument values to certain variables, and directly appeal to these arguments, the second variant of the record, in my opinion, more beautiful. And the first one is more readable for the person who is poorly familiar with additional language constructs like the 2nd option. plus the second option can generate more compact code, but hardly on the General background of such savings that will give :)
Type options 2 I personally try to only use if the arguments in the expression is rather short. Too bulky design illegible. Forget, then he will understand what is meant by that :)
by
0 like 0 dislike
In Perl 5.10+, by the way, there is a special operator for this: $arg // "value" — the right part is not evaluated when the Boolean falsity, and when undefined (undef) left. Accordingly, there is such construction: $arg //= "value"; — short and correct way to solve the problem.
by
0 like 0 dislike
Yes, do not bother you.
Use the one like in your own scripts and the one that is already used in alien.
by
0 like 0 dislike
in the Treasury
!isset($ar) && $ar = 1;
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
asked May 21, 2019 by azerphoenix
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
28,033 users