6
u/Time_Terminal Mar 23 '21
The point came through, but why does the long version unnecessarily include 3 extra steps?
This is like the tabloids of programming.
7
u/irosion Mar 23 '21 edited Mar 23 '21
i think it should be very clear what getImagePath returns so we just make sure it's gonna return a string or null and then the long version becomes this:
const getImagePath = () => logic that returns 'path_to_image.jpg' || null;
const path = () => {
const imagePath = getImagePath();
if (!imagePath) {
return 'default.jpg';
}
return imagePath;
}The short version is good.
1
u/backtickbot Mar 23 '21
1
u/crayonneur Mar 23 '21
I don't think it's a good practice to use a "or" to assign a default value. The getImgPath function should handle all of this. "default.jpg" is a magic string in this example, it should belong to the class that creates image paths, it shouldn't appear outside of it.
3
u/AacidD Mar 23 '21
I agree with the magic string part. He should've create a separate variable DEFAULT_IMG_PATH variable and stored the default value there.
1
1
u/isatrap Mar 23 '21
Why this over
GetImagePath() ? True : false1
u/isatrap Mar 23 '21
Seriously curious and instead of an answer I just get a downvote? Oof.
1
u/trip16661 Mar 23 '21
Because you want to use the result of the
GetImagePath()? whats the point of having true or false there... also that's a typo
-8
u/mburger89 Mar 23 '21
It is generally not good practice to initialize a variable with out giving it a literal value. Cause it will initialize to null or undefined.
1
74
u/CarsonMcKinstry Mar 23 '21
This isn’t quite correct, at least not anymore. This will also catch
falseand0since||will check for falsy values on the left hand side. You can use??nowadays to guard against onlynullandundefined