Erik Zaadi

The tales of a developer with passion for dad jokes

Readability VS Uber Cool One Liners VS Performance

Given that you need to do a MD5 hash in C#, which approach would you take?

A not so readable but cool one liner:

Or

A more readable and performance wise approach?

[Update:] Alex proposed the following, verbose, but as an extension:

1
2
3
4
5
6
7
8
public static string CalculateMd5Hash(this String input)
{
    return System.Security.Cryptography.MD5
        .Create()
        .ComputeHash(System.Text.Encoding.Unicode.GetBytes(input))
        .Aggregate(new StringBuilder(), (a, b) => a.Append(b.ToString("X2")), a => a)
        .ToString();
}

What do you think?

Share on: