I believe being a Software Engineer is just like being a Super Hero! And with great power, comes great responsibility. While writing code is an int...
For further actions, you may consider blocking this person and/or reporting abuse
Nice article, a few things to add:
We just need to get rid of the block we are not using, and we can also make it curried so is more reusable. And instead of calling it
splitName, we just call it what it is:spaceSplit:Object.assignwhen you want to actually update a newly created object that's more complex than a plain one, take this example:With some luck we'll get the pipe operator soon, and this will be even cleaner:
async/awaitsolution can be also simplified without the need forasync/await:Cheers!
The article was about general advises, and you are suggesting a functional programming approach which is worth a separate article :)
The approach is tricky, leads to unclean code (what createElement does? creates a function!) so I wouldn't recommend it as a general advise
You and me seem to have pretty different definitions of "unclean" .... you might want to take a look at currying ... and "unclean" libraries such as ramda 😅
Guess what
isQueenOfSpadescontains in ramda docs? Guessing not works in here, in such way need to read source to know. While in normal JS where naming matters it is a boolean.Is it clean? In common programming sense, name is given to describe what function or variable does, so in this way no, it's not clean. Functional programming has roots in math where naming doesn't matter, they are cool with formulas like this one, but it's not right to expect your teammates will be happy of guessing how your
logworks.Let's go one thing at a time:
isQueenOfSpadesdoes? Do you prefer the name to beisAnObjectWhichContainsARankQAndASuitOfSpadesinstead?Do you use libraries without knowing what the utils they provide will return, and relying only on the name? I imagine back in the day you had a really hard time with jQuery's
$.getif that's the case. My teammates are perfectly fine with alogfunction, because when they use it they get autocompletion in their editors telling them the arguments that function takes, the thing it returns, and if they use it wrong they get a red squiggly underline to let them know, not to mention that if they hover over the function or the arguments, they get examples from the JSDocs 😄Agree, makes sense now, I get used to rely on naming and I'm not hovering things at all, while in this practice programmer has to rely on hovering hint. I've tested and it works nicely, even in plain JS I got hint
Libraries is a different thing and only way is it read docs and memoize frequently used stuff.
$.get - jQuery was intended to do what document.querySelector does before it appeared, so I expect it to get element by selector. I honestly didn't check docs. But if it was ramda I would expect: get(key, object) and get(key)(object), but wrong, it's a 'prop' in ramda.
1 nowadays we have inline docs and typing systems.
So in general thanks for response, in editors it's not hard to get hints.
But not everywhere, while reviewing code in github it can give you hints as well, but far less intelligent and it won't work so nicely.
2 and 3
isAnObjectWhichContainsARankQAndASuitOfSpadesI would have a type Card = { rank: string, suite: string }
And a function
getIsCardAQueenOfSpaces.And now I can assign it to local variable
isQueenOfSpacesand use it in imperative way4 Makes sense, but it means whole project need to follow FP and currying to stay predictable, everyone in team must be good with FP and be on same page
Yes, sure, other teammates may introduce libs which I never used and I'm reviewing the code, and usually it's understandable just by looking on name and usage
@lukeshiru You're right. Thanks for the tips. I tried to keep things in this article (and the video). But yeah, once we get the pipe operator, things are going to be much more fun.
I don't see the value to use piped functions (even with pipe operator) ? It's less readable and requires more code and characters.
Have you actually tried currying? Let's use split as an example to compare different approaches:
You can see you how you only write "more" if you use it once, but the power of currying comes from reuse, and that's where it becomes more readable and where you end up writing more. Keeping the examples above, let's say a new requirement appears in which some strings might come with the
_character, here's how we would solve that with each approach:I understand that every time we do something thinking in reuse it will require a little bit more characters first, but then it requires less everywhere else. In general readability is improved because we go from stuff like this:
To stuff like this:
Not to mention that map itself can be curried like this:
And then you get even more reuse and readability:
Currying is amazing for code size and reuse, and is very readable if we stay away from "Haskell like" practices like having single character arguments. Hope this helps see the value of currying more clearly. I was requested a few times to do a full post on the subject and I will do it soon, so I'll try to remember to let you know about it if you still don't see the value in this.
Cheers!
I'm seeing a useful of pipe operator now. Thank Luke Shiru.
Great writeup Muhammud .
Q: const DAY_IN_MILLISECONDS = 3600 * 24 * 1000; // 86400000
isnt it better to put the breakup in comments to avoid the calculations being done every time ? Performance over ease of understanding
Thanks Raghav,
I didn't understand the question. With
const DAY_IN_MILLISECONDS = 3600 * 24 * 1000;we're performing the calculations one time and that constant can be used multiple times now. And we can definitely have a better comment around it.I believe that what Raghav meant was, if you put this variable in an kind of component that calculation will be done every time that i use the component.
use this:
Oh. Yeah, definitely. This should most likely be in a constants file or something. You're right.
I'm normally somewhat sceptical of posts in this category, simply because so many of these points end up being very closely tied to the wonderful concept of "personal preference".. that said, this has some real gems, with some good clear examples. nicely done!
with regards to the idea of avoiding short-hand variable names, I agree wholeheartedly with what you're saying, but (..and there it is) I have one small exception to this "rule": and that is dinky, tiny, one-liner helper functions..
using your first
splitNamefunction as an example, simply because of how small it actually is, instead of writing it like this:I personally would have written is like this:
with that said, as soon as the function arguments start growing, or the function body requires more than a single line, then both the implicit return and short-hand variable names go straight out the window.. tied to a rock.. shot by a cannon.. sometimes without even opening the window first.. (:
For default object values is there a reason you are spreading out the two objects. Is there an advantage over doing
or using the default values in the arguments?
Awesome Article, Thanks for the great techniques, you provided in this great article
Anytime!! Thanks.
Hey.. Remember update your Twitter in your Dev Profile: dev.to/codewithahsan
Great article! "Fewer or Named Parameters" and "Avoid Hard-coded values" are the ones that really stood out to me the most!