New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can we implement String.IsNullOrEmpty and String.IsNullOrWhiteSpace as part of the core libraries #11155
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. 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 == "" s == null || s.trim() == "" (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. |
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 |
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 |
(looks up quiver) |
You can create your extension to check the string is null or empty and the string is null or have space. Extension:
Implementation:
|
@lrhn what about nested properties? In this case extension methods (as mentioned by @rrifafauzikomara) do not resolve the issue - any null higher up the chain will throw an error:
Using |
I don't think static methods is the way most people are taking Dart code. With extension members, it's usually better to use those. |
but like this? |
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.
The text was updated successfully, but these errors were encountered: