DEV Community

Cover image for Improve your JS skills with those tips #2

Improve your JS skills with those tips #2

Code Oz on September 24, 2021

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 ...
Collapse
 
matheusgoncalves profile image
Matheus Goncalves

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?

Collapse
 
codeoz profile image
Code Oz

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

Collapse
 
leob profile image
leob

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 ...

Collapse
 
silverium profile image
Soldeplata Saketos

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

Collapse
 
leob profile image
leob

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.

Thread Thread
 
silverium profile image
Soldeplata Saketos

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

Thread Thread
 
leob profile image
leob

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.

Collapse
 
manoharreddyporeddy profile image
Manohar Reddy Poreddy • Edited
const condition = true
const other = {
   other: 'other',
   ...condition && { name: 'toto' }
}
Enter fullscreen mode Exit fullscreen mode

is it destructing of boolean variable?
can you help point from where you learnt this? MDN/other?
Thanks

Collapse
 
codeoz profile image
Code Oz

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 of const 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 this Object.assign({}, condition) but condition is a boolean, not an object! So it will convert it (in really case wrapping it) to an object like this new Boolean(false). So it will create a boolean object, but this boolean object don't have any enumerable properties! So Js will make this Object.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 this Object.assign({}, new String('hello')) when you make Object.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 have toto = {0: 'h', 1: 'e', 2: 'l', 3: 'l', 4: 'o'}

Collapse
 
codeoz profile image
Code Oz

hey I'm looking for the article that explain it! Share it after ;D

Collapse
 
codeoz profile image
Code Oz

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 !

Collapse
 
justummar profile image
Justummar

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.

Collapse
 
codeoz profile image
Code Oz

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!

Collapse
 
ihton profile image
IhToN

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.

Collapse
 
codeoz profile image
Code Oz

I prefer this approach also! It's more readable, but I have already have discussions with other dev that like this short-circuits 😆

Collapse
 
codeoz profile image
Code Oz

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 10 if?

I'm agree that short circuit don't need to be abusing but it can lead to less DRY (don't repeat yourself).

Collapse
 
nickmessing profile image
Nick Messing

What's the reason for ...condition && { ...toto } instead of ...condition && toto?

Collapse
 
codeoz profile image
Code Oz

Just edit it ! thank you I miss it 😃

Collapse
 
ayabouchiha profile image
Aya Bouchiha

Thanks

Collapse
 
codeoz profile image
Code Oz

happy to help you bro!

Collapse
 
leandroandrade profile image
Leandro Andrade

Congrats.

We need to pen attention that when create dynamic objects, it can influently directly in v8 optimizations.

Collapse
 
codeoz profile image
Code Oz

totally agree and thanks :D

Collapse
 
jzombie profile image
jzombie

Nice article!

Small suggestion: "these" is misspelled in the subject title

Collapse
 
majscript profile image
MajScript

Like it !

Collapse
 
silverium profile image
Soldeplata Saketos

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

Collapse
 
dorisjones91 profile image
DorisJones91

Hi thank you for this article it's brilliant. Could you please review code structure on my website top-resume-reviews.com/?

Collapse
 
emmaisa59135261 profile image
Emma Isabella

Nice

Collapse
 
codeoz profile image
Code Oz

thank you Emma :D