Comments (4)
For me personally explicit usage of .valueOf
in that case looks more clear.
For example, when somebody see such line:
date1 - date2
he can be confused with that result is? Days, hours, seconds, milliseconds?
When I explicitly write:
date1.valueOf() - date2.valueOf()
it creates less cognitive payload imho.
from babel-plugin-runtyper.
If we would talk about cognitive load I think that both cases are equally wrong. Your example with valueOf
depends on assumption that you know what value of date actually means. In the same sense subtracting two dates depends on assumption that you know what the value of subtraction is. Problem would not exist if we would explicitly state what the result is like (date1 - date2).seconds
.
Now what troubles me is not cognitive load but warnings that I get when I perform legit operations on builtin types. Since this is valid operation that does not involve type conversions or anything in that manner I think that there should be no warning. What do you think @vitalets ?
from babel-plugin-runtyper.
I agree that cognitive load exists in both cases. Also it is rather personal.
Since this is valid operation that does not involve type conversions or anything in that manner I think that there should be no warning.
This is not so. It involves silent type conversion, actually call of valueOf()
method. The same can occur in the app with usual objects:
var a = {valueOf: () => 3};
var b = {valueOf: () => 2};
// somewhere later...
console.log(a - b); // => 1
But for readers of code this is magic and can lead to potential bugs.
Looking at a - b
I can think that a
is number, but
typeof a // => "object"
Also thinking that it is a number I can do the following:
a++;
// now typeof a === "number"
and so on.
Imho overloaded operators and implicit conversions are always bad.
Subtracting dates for me is very similar to, for example, adding Arrays:
[1,2,3] + [4,5,6] // => "1,2,34,5,6"
but so many magic here.
Although example with dates is mentioned on MDN, I don't think this is recommended.
from babel-plugin-runtyper.
Agree.
from babel-plugin-runtyper.
Related Issues (12)
- Readme images HOT 1
- Add an option to disable the warning about using the plugin in production HOT 2
- Make it work with Babel 7 HOT 1
- Check for NaN in all assertions
- Support unary operators
- When compare objects split to Arrays, Date, etc HOT 1
- Uncaught ReferenceError: global is not defined HOT 2
- Guess variable type by it's name and check in assignment HOT 1
- prevent checking in library files or exclude directories HOT 4
- implicitCompareNull and implicitCompareUndefined options do not work HOT 2
- Ignore different non-custom types in === HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from babel-plugin-runtyper.