Sdk: Can we implement String.IsNullOrEmpty and String.IsNullOrWhiteSpace as part of the core libraries

Created on 8 Jun 2013  路  7Comments  路  Source: dart-lang/sdk

_This issue was originally filed by ir...@google.com_


These two methods are very commonly used in almost every application. Is it possible to make them as part of the core libraries to avoid having to create utility classes for very basic functionality.

area-library closed-not-planned type-enhancement

Most helpful comment

_This comment was originally written by @jonaskello_


For anyone else arriving at this issue from searching it might be of interest to know that the quiver package has these methods:

quiver.strings
isBlank checks if a string is null, empty or made of whitespace characters.
isEmpty checks if a string is null or empty.

All 7 comments

_Added Area-Library, Triaged labels._

The methods cannot be instance methods of String, because then they won't be called if you try to call it on null.
They could be static methods on String, but then there is no great saving from using your own library methods.

I don't particularly like the functions. I don't find it more readable than checking explicitly, and depending on variable names, it's not even shorter.

 s == null || s == ""
vs
 String.isNullOrEmpty(s)

 s == null || s.trim() == ""
vs
 String.isNullOrWhiteSpace(s)

(but that's admittedly a too short variable name in most cases).

I also think the white-space one is too specialized to really make sense as a library function.


_Removed Type-Defect label._
_Added Type-Enhancement label._

I agree with Lasse.


_Added NotPlanned label._

_This comment was originally written by @jonaskello_


For anyone else arriving at this issue from searching it might be of interest to know that the quiver package has these methods:

quiver.strings
isBlank checks if a string is null, empty or made of whitespace characters.
isEmpty checks if a string is null or empty.

I'd argue there's still more cognitive load on the explicit expression vs a static String.isNullOrEmpty. There's a single mode of runtime failure for the static method (incorrect ! in front) whereas the full expression can fail on the incorrect negation, incorrect == vs != times 2, incorrect de morgan application.

It doesn't take much thinking but since it's so frequently used, any amount of infallible muscle memory is helpful.

I think the quiver solution is better than a static member on the String class because it doesn't require the String. prefix. Not being prefixed means that it should not be in dart:core, it should be something that you opt-in to including it in your top-level name space. A package is precisely that.

(looks up quiver)
SG

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rinick picture rinick  路  3Comments

ranquild picture ranquild  路  3Comments

DartBot picture DartBot  路  3Comments

DartBot picture DartBot  路  3Comments

matanlurey picture matanlurey  路  3Comments