The rewrite in the second example for ternary operators makes the code very unreadable. The cumbersome part of this example are the else statements. If you use guard clauses if statements, the code becomes much more readable and concise:
functioncustomerValidation(customer){if(!customer.email){returnerror('email is require');}if(!customer.login){returnerror('login is required');}if(!customer.name){returnerror('name is required');}returncustomer;}
In this case, you can even remove the curly braces:
functioncustomerValidation(customer){if(!customer.email)returnerror('email is require');if(!customer.login)returnerror('login is required');if(!customer.name)returnerror('name is required');returncustomer;}
Now both versions are much more readable and the reader can understand the intent of the written code much better: catch errors and return them.
Very nice article. But I think I found one catch:
The rewrite in the second example for ternary operators makes the code very unreadable. The cumbersome part of this example are the
else
statements. If you use guard clausesif
statements, the code becomes much more readable and concise:In this case, you can even remove the curly braces:
Now both versions are much more readable and the reader can understand the intent of the written code much better: catch errors and return them.
thanks for comment!