Comments (3)
Thank you for your feedback. I thought about this and want to share my opinion with you.
Basically there can be 3 strategies on how to handle an error if something goes wrong. Throwing an expection, returning null or providing a default value.
Since exception throwing produce a lot of code overhead I prefer the other options. As you correctly pointed out, the current implementation returns null. I understood that you'd like to have an easier way to handle parse errors, by avoiding null.
I don't think we need to change it because since dart 2.3 the language has strong mechanisms to handle nullability. We can handle your case by using the "if null operator" (??
)
var value = "invalid".toInt() ?? 0.0;
If we implement a default value mechanism, it would look like this:
var value = "invalid".toInt(defaultValue: 0.0);
The first option produces less code and is equal to the second option from a readibility point of view. Also each dart developer should be familiar with the ??
operator. It's the language mechanism to handle nullability.
It feels unnatural for me to work around the language.
from supercharged.
You mentioned 3 strategies, and Dart has bulitin support for ALL of them:
Throwing an expection: int.parse()
.
Returning null: int.tryParse()
.
Providing a default value: int.tryParse() ?? 0
.
Currently I use this form without any package:
var value = Int.tryParse('invalid') ?? 0;
.
But It has many ?? In large function fromJSON.
Your package should be more convenient than the built-in function Instead of repeating.
from supercharged.
I don't see any improvement offering a defaultValue
parameter over the language wide ??
. You shouldn't consider a ??
as something bad. Every dart developer will know, you are provoding a default value by using ??
.
Once you learned it, you can produce very readable code. It's a feature on language level. Using defaultValue
is only on API level. Other package might name it onErrorValue
or defaultValue
or elseValue
. It isn't consistent. But using ??
is a concept that can be applied language-wide.
Also toInt
should be a shortage for int.parse
. It's shouldn't do anything more. It has just to goal to offer a fluent api. This allows the following code readability:
var value = someService.getData().someField.toInt() ?? 0;
I access a service, fetch some data, access the field and covert it's value to int. From left to right in logical order.
from supercharged.
Related Issues (19)
- Independent from Flutter HOT 4
- [Suggestion] About maintaining supercharged_dart HOT 2
- Missing ability to tween more than two values per tween HOT 1
- How about offset tween HOT 1
- Iterable.pickOne and Iterable.pickSome throws ArgumentError for optional Random parameter HOT 3
- Most Iterable extensions don't work with implicit-casts: false HOT 6
- firstOrNull conflict with dart IterableExtensions HOT 3
- Dart adopted extension methods HOT 1
- Parse 8 character hex string color HOT 1
- Add Size and Rect tweenTo extensions HOT 2
- Consider adding actions to analyze/test incoming PRs HOT 6
- can this be combined with darq? HOT 1
- singular aliases of int->Duration extensions HOT 2
- sortedByStringSC, sortedByNumSC, forEachIndexedSC are showing deprecated. HOT 6
- Thanks! HOT 1
- Could you remove the flutter dependency? HOT 3
- add between for DateTime HOT 3
- add extension for widgets. HOT 2
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 supercharged.