@jasonplackey
jplackey
LinkedIn
Email
The Hundred-Line C# Statement
Tuesday, March 17, 2020

If you add a breakpoint and your entire screen turns red, you may want to rethink things.

Below is some C# code that I recently inherited. I made the code unreadable deliberately to protect the innocent (and the guilty). This particular instance is a huge statement that calls a constructor that takes a params (variable-length) array of objects. Instead of simply using multiple statements to create and insert multiple objects into a list, the statement says something like:

return new MyObject (
    new SomeOtherObject(SomeFunction(foo)),

    SomeBooleanReturningFunction(bar)
        ? new SomeOtherObject(SomeOtherFunction(baz))
        : new YetAnotherObject(...),

    .
    .
    .
);

Combine the params array with a bunch of nested constructors, ternary operators, and inline logic, and you have what you see here: A 100+ line single statement. Try to breakpoint any single line within the chunk, and you breakpoint the entire statement.

A 100+ line statement is not clever. Please... keep it simple!

Blog engine and all content © 2024 Jason Plackey