7

C# is getting so fucking obfuscated with these null check inceptions. Found the following in my company's code base. Why did it take me and 3 other devs an hour to figure out how to write this if statement into a flowchart?

if(!string.IsNullOrEmpty(a?.Id ?? b[0]?.Id))...😫😫😫

FYI: We figured it and also found some bugs with logic, but can you? I'll post our flowchart if ranters are interested.

So to add to the madness:
if(!string.IsNullOrEmpty(a?.Id ?? (b?.Any() ? b[0].Id : null)))...🤯🤯🤯

Comments
  • 0
    @irene they also forgot to do a count check. Both null and empty "b" would crash it.
  • 1
    Fixed: if(!string.IsNullOrEmpty(a?.Id ?? (b?.Any() ? b[0]?.Id : null)))...
  • 1
    @nint3ndope add a try/check for nullpointer exceptions ;)
  • 14
    That is not the error of C# but of the developer trying to squeeze everything into a line.

    Just because you can you do not need to and probably should not.

    In cases like this I usually break out parts into methods with descriptive names for clarity.

    The short form is best used in simple short expressions.

    I have done the same kind of single line madness in c#, C and even plain old line oriented basic myself and cursed myself when I have to fix something.
  • 9
    Also, in C# 8 they will add the option of on a per file or project basis have non nullable reference types so a simple variable by default cannot be null and you have to explicitly declare it as
    MyObect? obj;

    To be able to assign null to it.

    I cannot even begin to guess how many errors would have been avoided had that existed 10 years ago in C#
  • 2
    Kotlin ftw
  • 2
    Not language fault. This code is bad and its author should feel bad. Personally, I would never let it pass review.
  • 3
    In the interests of always leaving code cleaner than you found it, I hope you refactored this.
  • 0
    At first I thought this was a rant about using a and b as variable names.
  • 0
    Pffffft. Implement the maybe monad and be done with it peasants.
  • 0
    This is just gross. First off the variable names are complete crap, how the fuck are you supposed to know what any of that stuff is? Second why are they trying to check 1,000 different things in one if statement? First thing to do is give the variables meaningful names, next thing to do is break those checks out into separate functions. Third thing to do is go find whoever wrote this and give them a good verbal lashing for their failure to uphold clean coding standards.
  • 0
    @nint3ndope I’d make a strong argument that your ‘fixed’ code there is not fixed at all. What happens when someone else has to come back to that a few months or years from now or even when you have to come back to it? It might be functional but it’s not good code. It needs refactored to be good and if you view software as a form of art with readability being key to good code this code is bad. That code is barely comprehensible, you can’t just glance at it and tell what it does, you first have to figure out what each variable is, then try to make sense of the mass of null checks. Name those variables with descriptive names, then break the checks out into separate functions. Otherwise all the work that went into troubleshooting this has been for naught.
  • 2
    @Voxera totally agree. My "fix" was just poking fun of the null check inception madness. Syntax is so dirty. I obviously refactored it into cleaner, more readable code.
  • 0
    @jespersh personally I hate the syntax but I understood it or at least the intent immediately. It took us almost an hour to create a flow chart for that if statement. I showed this to a js dev and they literally said wtf and gave up without trying.
  • 0
    Null is a bug for higher level languages. This is why I opted for a type systems that discourages the use of null
  • 0
    I think C# should do what Python did. Break changes but keep the legacy version for backwards compatibility. Or we can all just switch to a language available for the CLR that forbids null for code written in the language and guard against it at all costs
  • 3
    @MoonOwl They are doing that in C# 8.

    It is a setting you have to manually activate, like javascripts strict mode.

    But it will make null the exception.

    And I very much look forward to it.

    Easy null checks like the ?. or ?? helped a lot in reducing noice but it also makes for much les readable code if used to much.

    Using extra line breaks can improve readability and for repeatable syntax where every line is similar that can be enough.

    But if every part is slightly different its all to easy to forget a ? somewhere.

    Even the person that “invented” null does not think it was a good idea.

    Sir Anthony Richard Hoare:
    My billion dollar mistake
    https://en.m.wikipedia.org/wiki/...
  • 1
    "Flowchart"
    "OOP"

    Excuse me what the fuck?
  • 0
    @Voxera Sadly we just updated code base from .NET Framework 4.6.0 to 4.7.1/.net standard 2.0. Due to the nature of the software and 3rd party applications that use our libraries, I don't envision them going to C#8 for a 3 or 4 years after it's official release.
  • 1
    @Gregozor2121 you can do a flowchart or at least map out all the possiblities of that particular if statement. I don't do this for everything but thought it would be a fun to try and do given that small if statement packs so much into just a few characters.
  • 1
    @nint3ndope
    I know. Nearly 99% of my comments and life is a joke tho.
  • 0
    @jespersh if b = null then FirstOrDefault() will throw a null exception. You will have to null check b.
  • 0
    @nint3ndope I know the feeling. We are just migrating to net core but it will take a lot if time. Some small parts will get there faster but the bulk will not.
Add Comment