In this article I will share with you some news tips about JS that can improve your skills!
Don't use delete to remove property
delete ...
For further actions, you may consider blocking this person and/or reporting abuse
I understand the objective and the reasoning behind the suggestions, but we shouldn't be totally against the usage of
delete
.It can still be useful in situations where you absolutely need to remove a property from an object before an operation, and the object will no longer be used after this operation.
In this case, personally it seems to be a lot more straightforward than the functional approach you've described. Thoughts?
If you absolutely need to remove a property from an object before an operation and the object will no longer be used after this operation. Why don't use a new object that will no have this property instead of delete it from the original object? If you have any things that depend on this property you will create bug and side effects! If you create a new one, you will not :D
What's the problem with using "splice" to delete an element from an array? Works even in ES5, and I can't imagine that the "functional" approach (in essence copying the whole array except one element) would be faster ...
once you work with pure functions, and data is being transformed and used in a long sequence of functions, moving to store, to state, to helpers... functions that are changing "in place" arrays are very dangerous. Splice, Sort, Push, Pop, Unshift... they need to be used only in the "data formation" phase, but never as part of any data transformation, as they will lead to side effects very fast
Yeah but the point of the original author was that "delete" is slow, and therefore you need to use the approach that he proposed ... to which I replied "what if you don't use delete but splice", probably the argument about performance goes away then.
The FP (functional programming/immutable) approach is cool, but there's still a legit case to be made for the old fashioned way - it's only "dangerous" if you do it wrong, heaps of code are written this way which are perfectly fine.
I said "dangerous" and not "forbidden for any case". Every rule has some exceptions. It's always dangerous to modify arrays in place, that's why it needs double attention, very nice test coverage, and a lot of edge cases coverage, and a good programming team behind.
In this article there is a kind of "show off some syntax hacks", which IMHO opinion, some are hard to read, some are dangerous, especially for immature programmers, and some are "please read Ecma Script latest changes and don't be so cocky" :D
Haha agreed, especially the second paragraph ;)
The techniques in this article are okay-ish, but I'm not blown away if I may put it like that.
is it destructing of boolean variable?
can you help point from where you learnt this? MDN/other?
Thanks
I will divide the answer in two parts to be more clearly!
First it's not destructing but spread operator.
When you use spread for example ->
const toto = { ...anotherObject }
it's a shortcut ofconst toto = Object.assign({}, anotherObject)
Js engine will check all enumerable property (it's like when you make a for...in loop on an object or Object.entries()), for example ->
Object.entries(anotherObject)
and put all key/value to the new object!So now let's get the example from the article.
In case of condition is
true
, JS engine will check the expression, and see that condition is true so it will go to{ name: 'toto' }
. And then we add all property from this object (here name: toto) to the other object.In case of condition is
false
, JS engine will try to do thisObject.assign({}, condition)
but condition is a boolean, not an object! So it will convert it (in really case wrapping it) to an object like thisnew Boolean(false)
. So it will create a boolean object, but this boolean object don't have any enumerable properties! So Js will make thisObject.entries(booleanObject) = []
so it add 0 property to the new object!It not really easy to understand, I will take another example
Imagine this code
const toto = { ...'hello' }
JS Engine will make thisObject.assign({}, new String('hello'))
when you makeObject.entries(new String('hello'))
you will have a list of all value key/pair, and with a string object you will have a value for each char, so JS engine will add all property (except length) to the new object so we will havetoto = {0: 'h', 1: 'e', 2: 'l', 3: 'l', 4: 'o'}
hey I'm looking for the article that explain it! Share it after ;D
I'm agree that short circuit should not be abuse! I love ternary but it cannot be applied in all case!
For the operator I am totally agree that
??
(Nullish coalescing) should be replace by the OR Operator, I made this change thank you a lot LUKE !I agree with some of the suggestions here maybe smart looking but certainly do not make for more readable code, maybe a developer comes to new project and doesn't know the "tricks" of syntax, they would waste time googling what on earth it means and googling syntax tricks is difficult in itself.
Unless they offer significant speed enhancement or use need them for extreme optimization these are more confusing than useful.
Thanks for your comment!
I was this guys that you speak when I come in my first job! I take a few time to understand but when I understand all, I was happy to see that it improve code quality!
It's why I'm trying to help people to understand and know those 'tricks'!
I assume that is not a reason to abuse it!
Good one! Although I personally prefer to avoid short-circuits for that very specific example.
The if block enhances readability and also allows for future modifications easily.
I prefer this approach also! It's more readable, but I have already have discussions with other dev that like this short-circuits 😆
Hey I miss I mean "side effects " 😅! I think benchmark is not the good approach about this, the focus is on "side effect" that can be leads by
delete
operator. In general in programming we don't encourage people to remove property from a current object since some "logic" can be linked to this property and lead to bug.For the example you highlight, I mean that the
if
condition can be avoid, so we should avoid it when we can. In this case we have only one condition and only one property. Imagine that we need to add 10 properties with 10 conditions differents, you will use 10if
?I'm agree that short circuit don't need to be abusing but it can lead to less DRY (don't repeat yourself).
What's the reason for
...condition && { ...toto }
instead of...condition && toto
?Just edit it ! thank you I miss it 😃
Thanks
happy to help you bro!
Congrats.
We need to pen attention that when create dynamic objects, it can influently directly in v8 optimizations.
totally agree and thanks :D
Nice article!
Small suggestion: "these" is misspelled in the subject title
Like it !
Please, enable linter rules and you will find a new world of dos and dont's. Many of the things you advice here are hard to read and in fact bad practices :D
Hi thank you for this article it's brilliant. Could you please review code structure on my website top-resume-reviews.com/?
Nice
thank you Emma :D