question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Functions should throw exception on invalid args

See original GitHub issue

In general I believe that it’s better when functions called with invalid argument throw early, because such approach leaves less space for bugs.

Question is how far should we go implementing this, considering that we have no builtin facilities like languages like TypeScript have.

For example see #7125.

  • I’m sure that more strict conditions (introduced there) are good. There is no explicit type checking, but code is composed with type-checking in mind, and intentionally fails on many cases of invalid args.
  • But should we put that to the test (like done there)? I have some doubts, because that “fails” are not in specs, so they are specific to current implementation. Anyway, it should not be a problem to fix (or even remove) tests when we will need that.
  • Still things are not consistent now: not all cases of invalid args are covered. E.g. DomEvent.off doesn’t fail if pass invalid object instead of el. And (in other functions) event argument is not validated at all. All such cases could be covered with explicit type checking, but I suppose that would be overkill.

Thoughts?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
johnd0ecommented, Jan 16, 2021

If you want to implement this yourself then better discuss all details in new PR.

This issue is created to discuss general questions, e.g. how far should we go in arguments validation. Should we cover as more functions/methods/constructors as we can? What of them are error prone (more often misused)? How should we avoid (potential) performance loss?

  1. One possible approach is adding validation to factories (e.g. toPoint()) but not to constructors (new Point()), thus leaving space for custom optimization. E.g. when you need to process huge dataset - you may prefer Point.
  2. But it’s not clear to me if such optimization is ever needed. To understand this we should compare real performance wit/without arguments validation. And it may appear that difference is miserable.
0reactions
Falke-Designcommented, Oct 10, 2021

There is also a problem with Tiles. If the maxNativeZoom is set with a string value, the tile-request contains sometimes a high zoom value: https://github.com/Leaflet/Leaflet/blob/d6d6051ac6acbc2c68b6b0f8ef1707cf91054f16/src/layer/tile/TileLayer.js#L209-L220

this._tileZoom / zoom is then for example "17" and zoomOffset which is 0 is added to it, which returns 170. If a tile is requested with zoom 170, the browser window breaks / stops running. But I didn’t found out why this happens

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Throw exception if method arguments are invalid
If you want to throw an error, you have to use the throw keyword, followed by an appropriate Exception . public static int...
Read more >
Exceptions
A throw expression accepts an expression (in this case, an unnamed object of the class invalid_argument ), which is passed as an argument...
Read more >
How to Throw IllegalArgumentException in Java - Rollbar
The IllegalArgumentException is thrown to indicate an illegal or unsuitable argument passed to a method. Learn how to solve it.
Read more >
Should I raise an exception/error when an optional argument ...
But wait, it's worse: if your function throws an exception when the ... Such as an Invalid Combination of Required and Optional Parameters....
Read more >
std::invalid_argument - cppreference.com
Defines a type of object to be thrown as exception. It reports errors that arise because an argument value has not been accepted....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found