Giter VIP home page Giter VIP logo

Comments (3)

felixblaschke avatar felixblaschke commented on July 25, 2024

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.

cdhigh avatar cdhigh commented on July 25, 2024

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.

felixblaschke avatar felixblaschke commented on July 25, 2024

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)

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.