Writing tests === trying to make yourself feel better about your shitty code
If you need to write tests for your code it simply means you *know* you wrote shitty code and you are trying to make yourself feel better about it by seeing your tests pass. If you have a simple function:
function add(a = NaN, b = NaN) {
return a + b;
}
This function is made obviously for the purpose of adding 2 numbers together and returning their sum. But what if we passed two strings to the function?
const result = add('a', 'b'); // result === 'ab'
In such case if we really want to make the result a number we could return a *NaN* value in case of incorrect arguments:
function add(a = NaN, b = NaN) {
if (typeof a !== 'number') {
return NaN;
}
if (typeof b !== 'number') {
return NaN;
}
return a + b;
}
We could say this functions is bulletproof now, because it will always return a number (*NaN* is a number, open up a console and check if you don't believe me). There might be another problem here, namely passing *Infinity* as argument, but this is rather a business logic concern rather than incorrect input type. We could add additional logic to return 0 or some other numbers in case of *Infinity* but that's not our problem here. We want to return a number from this function. By the way we can shorten the *if* statement to something like this
function add(a = NaN, b = NaN) {
if (typeof (a + b) !== 'number') {
return NaN;
}
return a + b;
}
Or even something like this
function add(a = NaN, b = NaN) {
const isNumber = typeof (a + b) !== 'number';
return isNumber ? (a + b) : NaN;
}
We could use the first version of our *add* function if we were lazy, but we can be more disciplined and write solid code instead of shitty one and praying for good fortune. The example I've shown here is obviously very basic and does not resemble any real world case scenario. It is merely to show that if you want to be able to say 'fuck testing, tests are for noobs' then you need to be consistent in your code, make sure you understand every line of code you write and there is nothing that could possibly slip through your type/value checks down the road.
In my opinion it's a waste of time to write tests for something that *obviously* is correct and will never fail. Based on our example here: if you want to return the result of adding *a* and *b* then just make sure both values are numbers and stop fucking around with testing it.
Now, I understand that in many companies people test the shit out of their code because there are lots of developers writing code of varying quality and hence the need for checking if the whole app won't break if some non-standard user comes along. But this is a matter of hiring rock-solid coders who know exactly what the fuck they're doing instead of random jQuery fanboys who don't even have a clue how to do basic stuff without their holy dollar function.
Yes, I think jQuery is shit and if I had a startup or whatever I would never hire someone who even thinks jQuery is useful *at all*. But that's another story. I might elaborate on that one in some post in the future.
Brak komentarzy:
Prześlij komentarz