• King (unregistered)

    This is worse than ternary, or?

  • I'm Sorry (unregistered) in reply to King

    Yes this is worse : No this isn't worse ;

  • bvs23bkv33 (unregistered)

    nice way to escape the pyramid of doom

  • P (unregistered)

    Whoever wrote the code clearly thought that decimal? means "I don't know if it is a decimal, it could be one or it could be anything" instead of a nullable decimal

    Or they didn't know what is even a decimal?, not to mention how to convert it to decimal, and fumbled to that solution. Roughly like:

    1. Returning decimal? directly gets IDE yelling at them because decimal? can't be implicitly cast to decimal. But they're taught when converting an "unknown" type to number type you need to parse it, so let's add that in
    2. IDE complains that first argument must be a string. Quickest way to get a string out of a decimal? is toString. So let's do that
    3. Now the code sometimes behave badly because nullable nulls give empty string on toString, which is not parsable. Or that the observed that decimal? can be null, and every enterprise coder is trained to handle that in advance, just in case. So let's do a null check. And because decimal? is clearly a type from outer space, let's use the most generic == null
    4. Profit

    TRWTF: nullable types are apparently aliens?

  • Little Bobby Tables (unregistered)

    TRWTF is of course calling a data type a "decimal".

    No, "decimal" does not mean "a thingy with numbers in that's got like a dot (or a comma if you're Yoorpeen) somewhere in it". "Decimal" means "expressed in base 10 notation". Yeah, we get it, computer scientists are appalling mathematicians nowadays, but at least try to get your nomenclature correct.

  • Null user (unregistered)

    Apparently he doesn't know about the null-coalescing operator (??) either.

    return value ?? 0;

  • Stella (unregistered)

    return comment ?? "";

  • TRWTF (unregistered)

    TRWTF is that .NET doesn't have BCL monadic types like Maybe, instead they introduced the half-cocked Nullable.

    And now .NET has immutable references, which still isn't quite as good as it could be.

  • zybex (unregistered) in reply to Null user

    That would not work because "value" is a "decimal?" while 0 is a "decimal".

  • P (unregistered) in reply to TRWTF

    You are TRWTF. You can chain methods on nullable types with ?., which is the same as monad chaining.

    And seriously, "monadic"? Are you on Haskell crack or something? Because nobody else would intentionally invoke that M word.

  • zybex (unregistered) in reply to TRWTF

    Nullable types are extremely useful. The notation with a question mark at the end "int?" is in fact syntatic sugar for the monad "Nullable<int>"

  • Brian (unregistered)

    Ugh, one of my personal WTF triggers is functions named "Validate". That's about as descriptive as "DoStuff". Are you testing that a value is valid? Are you coercing something into a valid value? Are you doing something completely unrelated to the validity of a value but calling it validation anyway? IMO, "Validate" is just too ambiguous of a word to be a good descriptor.

  • Scott (unregistered)

    I've most often seen this because people are afraid of exceptions, and want to return something they can use without null-checking.

    So their code has to check for zero instead. Sigh.

    The application I'm currently dismantling has this built into the bottom-most data layer, except that they set to min value for DateTime, int, long, decimal, etc.

    So a lot of code like: "if field is DateTime.MinValue then print empty string, else format field", because that's a lot easier than using the built-in .HasValue and .Value parts.

  • urgent (github)

    Computers work pretty much like flipping a book page and changing the letters. All you have is on or off.

    So floating point calculations, pretty soon you can hit the limits of the hardware.

    I really do wonder with quantum computers, if they are really quantum, and if they are really computers, what would happen if LIGO started to detect something just in the right way that the network of quantum computers detect LIGO, and they get this cosmic feedback loop going ... ... ...

    All because you didn't want to buy the cloud platform, boss.

  • (nodebb)

    The GetValueOrDefault() method and the null coalescing operator (??) are relatively recent additions to the language. Perhaps this was written before they were available? It's still a WTF...but maybe not quite as much.

  • WTFGuy (unregistered)

    I think P pretty well nailed the process behind this WTF. It's remarkable / terrifying how many devs seem to think that "correctness" begins and ends with "compiles with no errors (although maybe a warning or two)".

    God forbid we should have people that are actually skilled at (much less rigorously trained in) their chosen profession. It's not like public safety, the entire world economy, and a whole lot more depend on this stuff. Oh wait ...

  • urgent (github) in reply to WTFGuy

    Training can be undone by changing the standards. Standards often change because people get caught up in a fight that's much bigger than them between two rivals who have been so bad, we had to build entire multinational corporations to contain them.

    Being humble helps a lot. Walking away really is often the best choice. Don't let fear get you. Trust that a spoken tradition predates the Big Bang, and everything is taken for good cause, all the way down to the smallest particle.

    Aim for mutual realignment. Turn good into better, and know that life really is a series of unending orthogonal turns if you do it right. Ask any good MD. You really don't have to die.

    This makes your mind the most powerful thing that can be. There are no limits to the human mind. WE ARE EVERYWHERE NOW.

  • Harris M (unregistered) in reply to Little Bobby Tables

    It is referring to a decimal as in a decimal fraction. Meaning a fraction where the denominator is a power of 10

  • löchlein deluxe (unregistered)

    Programmers: give them a string and they'll hang themselves with it.

  • siciac (unregistered) in reply to Little Bobby Tables

    Nope. A decimal means it's using a denominator that's a (typically negative) power of 10 rather than a power of 2. Thus 1/5 or 1/100 can be represented exactly and dividing by powers of 10 (very common in finance but also in metric conversions) doesn't introduce any rounding error.

  • Anon (unregistered) in reply to zybex

    There is a big difference between nullable types in .NET and Maybe types in sane languages: (T?)? == T?, but Maybe<Maybe<T>> != Maybe<T>.

    Maybe means "add a new value to this type that's not used by anyone else". Nullable means "make sure this specific magic value is in the type".

    If you are writing generic code in C#, you either have to avoid using T?, or you have to tell the user that T can't itself be nullable.

  • P (unregistered) in reply to Brian

    You know what they say, running your program is the same as validating whether your program works correctly

  • Tyler (unregistered)

    There is another (potential) bug hidden here

    decimal returnValue = 0; Decimal.TryParse(value.ToString(), out returnValue);

    The assignment of 0 to returnValue is useless here, as Decimal.TryParse will ALWAYS overwrite the value of returnValue, even if the method returns false. out parameters HAVE to be written to by the method. In this case, 0 is what TryParse sets if it fails, but say you had the 'default' value set as 5 or something. That 5 would never survive the TryParse call!

Leave a comment on “Nullable Knowledge”

Log In or post as a guest

Replying to comment #:

« Return to Article