DEV Community

Discussion on: I work hard in order to avoid work - My view on refactoring

Collapse
 
rnkrnk profile image
RN • Edited

yes, I'm looking at you, the one who uses + to convert strings to numbers in js

What's wrong with unary plus? I googled it and found out that some people think "it has poor readability", "it could be easy to miss". It seems to me that if a developer "misses" characters, the problem is his poor concentration, and he should be more careful in his work. I doubt that the code will become "easy to read" if we replace all single-character operators with words like "plus", "minus", "multiply", "percentage", etc.

Collapse
 
nombrekeff profile image
Keff

There's nothing wrong in using it, and yeah I agree in part. But you can't expect developers to be 100% concentrated and careful 100% of the time, we're humans and as humans we might have a bad day, we might have not slept well, etc... A new developer might not yet know how the unary operator work, it's not intuitive at all. You already need a good knowledge of the language.

So if I have the choice I will use other options before the unary operator to convert strings to numbers. This way we reduce the possibilities of a miss. And I usually prefer to make my code literal, ie using Number('1') or parseInt('1') instead of +1. It might be a personal preference, but I really think it can be beneficial to not use it if it can be omitted.