So, I wrote this code, inspired by a Daily WTF post , which I wanted to make "more efficient":

public static string LastSixDigits(string number)
{
    return new List<string>(new string[] { number })
        .Where(n => !string.IsNullOrWhiteSpace(n) && n.Length >= 6)
        .DefaultIfEmpty(string.Empty)
        .First().Reverse().Take(6).Reverse()
        .Aggregate(string.Empty, (s, c) => s += c);
}

How it works:

  • Makes a list of the one argument passed
  • Takes only the elements of that list which are not whitespace and which have a satisfactory length
  • If no elements remain, pass the rest the empty string
  • Take the only element from the list, reverse it, take 6 characters, reverse those, and aggregate them into a string

Please, for the love of all that is holy...

Do not ever do this.

Not in production code.

Not even as a joke.

You think I'm kidding? I've seen crap like this in production code. Extensions like ReSharper make it super easy to abuse LINQ.

Never do it. It's a trap.

Thank you.

Sincerely, Everyone ever

Previous Post