It’s Finally Fixed!

One of the biggest little frustrations I’ve seen in many codebases over the years has finally been fixed!

The Problem

As part of the Microsoft.IdentityModel.Tokens nuget package an internal extension method which worked on IEnumerable<T> and exposed IsNullOrEmpty was made a public method. Due to being public it meant anyone consuming the package could use it. As it is based off IEnumerable<T> it was relatively easily discoverable through IDE intellisense.

Due to this it was very easy for developers who were working on any IEnumerable<T> to use it instead of doing a specific targeted check themselves. Whenever I saw it being used it made me uncomfortable. It made me ask questions like “why?” and “is it doing what the developer actually meant?”. Often this was not the case. Many examples I have seen it would be used when checking if a string was null or empty. However in this case it would see the string an IEnumerable<char>. What is needed in nearly all of these scenarios was to use the string.IsNullOrWhiteSpace() method instead.

The Solution

The bug was finally raised and has been addressed in version 8 of the package.

By some of the comments on the bug I wasn’t the only one which felt this way.

As I said this is a breaking change so when you upgrade the package and you are using this method it will fail compilation. If your code was working on string instances then you will need to upgrade to use the native string methods for checking. If you are using it on other types or constructs then you will need to evaluate each situation as to what the code is trying to achieve and best way to do it.

Please do not just implement the method in your own code base and move on. Using Any() under the hood might not be what you are actually needing.

Conclusion

Nuget packages are awesome for adding functionality to your applications but be careful when using extension methods or when intellisense suggests a method usage as it may not be doing what you think it’s doing!

Until next time!