Code Monkey home page Code Monkey logo

Comments (4)

vitalets avatar vitalets commented on June 7, 2024

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.

zrudzionis avatar zrudzionis commented on June 7, 2024

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.

vitalets avatar vitalets commented on June 7, 2024

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.

zrudzionis avatar zrudzionis commented on June 7, 2024

Agree.

from babel-plugin-runtyper.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.