Wednesday 10 January 2024

The Code That Should Not Be

Pieces of code apparently written by engineers (not software engineers).

HashSet<int> h = new HashSet<int>();

foreach(int i in numbers)
    if(!h.Contains(i))
        h.Add(i);

The following is happening inside some loop where the null check a does not change its value:

if(a)
{
    return null;
}

// do something...

x.field = a ? "" : something(a);

In other words, x.field is never set if a is true. It seems they wrote a loop that returns null when any of the items fails the null check. And the loop should fill some "report" or so. I prefer collecting some error messages in a string or StringBuilder and if it's length is non-zero, throwing some exception. But I'm no engineer, yet.

No comments:

Post a Comment