If I was learning about knives, I would refuse to learn how to cut man's throat with it.
A weird sentence to start a post on programming? Perhaps, yet I find it very adequate.
1 (BTW, why I don't use React, nor Angular? Because I love simplicity. Angular was never simple - it was always complex as hell. And React became much more complex, than a simple library it was in 2014.)
class keyword in my code. Well, not voluntarily.
What happens when objects are added obj1 + obj2, subtracted obj1 - obj2 or printed using alert(obj)?
In that case, objects are auto-converted to primitives, and then the operation is carried out.
I decided to skip that chapter. I'm simply not interested, what is there. I'm not interested in this topic. You may call it ignorance, and perhaps it is. Yet I'm also going to explain the reason for my complete and utter lack of interest in this knowledge.
First things first
Let's start with some fundamental remarks.
In order to process data, programmers write functions. Functions solve two different problems:
- they are a way to group computations, to reuse them, whenever neccessary,
- by giving functions proper names, the code becomes more readable.
So, when numbers should be added, the
+ operator (function) is used, when strings should be joined (or concatenated), some
concat) function is used, and so on. Names for functions are very important, and they should be meaningful, and descriptive. When both strings and arrays are concatenated, it can be done with
Array.concat functions/methods, or with
join function for strings, and
concat function for array, or vice versa, or whatever else sane name convention is used.
Quite obvious, and trivial, at least for someone, who has some experience in programming.
It should be obvious, that functions meant to perform the same (or similar) task, should be given the same (or similar) names.
Just imagine the opposite situation: you call a function named
Math.sin, and it returns the input argument... squared. It would be a disaster: not only the expected result is incorrect, but also the function name is misleading. No one should ever write such a code, right?
So, in sane programming languages, the
+ operators are used to add numbers (in OCaml there are even separate operators for adding integers and floating point numbers!), and for joining strings other operators (or functions) are available.
Of course, sometimes the same function should be available for different data types. For example, some convention can be used to convert a value of some type, to a string. It can be
toString function/method, in Haskell there's the
Show type class for that purpose, and in OOP languages with classes, interfaces are probably the way to go in such cases. (I'm not an OOP expert, but I would expect something like IPrintable, or perhaps IStringify?).
(It can be also achieved with inheritance, yet inheritance bounds object's classes, and sometimes it's not desirable.)
Once again I'm going to state it: quite obvious, and trivial, at least for someone, who has some experience in programming.
So, why the heck the
- adding numbers,
- strings concatenation,
- and for performing possibly weird things with objects (which are first auto-converted to primitives, and then the
+operator is applied to the obtained values)?
It means, that there are a few constraints for activites, which can be done with data types. So:
- When you try something like
1 + 2, the result will be
- When you try
"1" + "2", the result will be
"12". It can be surprising, especially when you take both values from input fields in some browser form, and expect the result to be a number... You have to properly convert the values to numbers, before adding them.
- When you try
1 + "2", the result will be still
1to string, and then just concatenates two strings. If you'd rather convert the string to number, and then wanted both numbers to be added, you'd have to perform explicit type conversion manually.
The third case may seem to be weird: who would write code
1 + "2"? Perhaps no one - such code is pointless. Yet it is possible to write functions like the following one:
const plusTimesMinus = (a, b) => (a + b) * (a - b);
Let's get real, though, and try to focus on serious, production-quality code.
In such code, functions, which perform data processing of the same type, will be given the same name. Functions, which perform data processing of different types, will be named differently. The code should be written for sane humans to read.
+ operator should be used. For adding strings... it is possible to use the
+ operator (when you are 100% sure, that both arguments are strings), or the
concat method (of the String object) can be used, or the
join method (of the
Array object) can be used. And for sending messages to objects, a proper object's method should be used.
I would never use the
+ operator on objects!
And I would strongly advice against it for anyone.
That's the reason, why I skipped the mentioned chapter.
(I decided this post deserves not only a weird sentence for beginning, but also for the end, so...)
Such as cutting people's throats should be avoided, condemned, and prosecuted.