Share your code publicly, it's amazing!

Before you continue to read :

Read my latest blog before you continue, as this is a part two follow up to that blog.

I shared the blog on X, Mastodon and also internally on my work Slack channels and I've had lots of great feedback. I thought it would be useful to pull all the comments together so people can see everything in one place. 

The feedback

The first bit of feedback was from Liam on mastodon :

 

The oringal code (for reference) using the Ternary Operator

if (decimal.TryParse(Product.Price?.Replace("GBP", string.Empty), out decimal price))
{
    var cleanedProductPrice = price % 1 == 0 ? price.ToString("0") : price.ToString("0.00");

    return $"{Product.Name} {(string.IsNullOrWhiteSpace(Product.Price) ? "" : " - £" + cleanedProductPrice)}";
}
return string.Empty;

@owaincodes in this example, if you do ? price.ToString("C0") : price.ToString("C2"), you'll get a nice culture-appropriate formatting for the price, so you can reuse the code for €, $, ¥ etc without specifying the symbol. The C matches your application's culture config

This is a nice way of using String Formatting or to be more specific the Currency Format Specifier , I shared this thought internally on the Gibe slack and another suggestion from Tristian was to use .ToString("N0") and .ToString("N2")

This uses the Numeric Format Specifier - there are so many other options so it's worth checking it out.

Anders made a few more code suggestions.

@owaincodes I think the example may be simplified a bit more:

  1. the ternary can be placed into the ToString call to avoid writing it twice
  2. if parsing the price is successful you know that it can't be empty

So something like this:

if (decimal.TryParse(Product.Price?.Replace("GBP", string.Empty), out decimal price))
{
    return $"{Product.Name} {price.ToString(price % 1 == 0 ? "0" : "0.00")}";
}
return string.Empty;

@owaincodes or possibly even a double ternary 😎

return decimal.TryParse(Product.Price?.Replace("GBP", string.Empty), out decimal price)
    ? $"{Product.Name} {price.ToString(price % 1 == 0 ? "0" : "0.00")}"
    : string.Empty;

As you can see, you can really go down a rabbit hole with this.

Terrence added a reply to the above.

@owaincodes Though one thing I will say, as a bit of a counterpoint, is short hand notation can be a double-edged sword sometimes. It's all too easy to start simple, but sometimes end up with multiple Ternarys ( ? x : y), Null Coalescing (??) operators & methods chained together. To the point that 'one liner', though more succint, is actually harder for a human to read & follow than the long-hand version was. Always important to remember human readability element to code as well as elegance...

Which I totally agree with, there is a fine line between reducing number of lines of code and making it easy to read.

So find something that works for you and your team and agree a standard because as you can see, there are numerous ways to do the same thing.

What I love about all of this is, that "simple" bit of code I shared in my previous blog became a big conversation about how different people do different things. Never be scared to share even "simple" bits of code because you never know what you might learn from it.

Published on : 09 April 2024