Juri Strumpflohner
Juri Strumpflohner Juri is a full stack developer and tech lead with a special passion for the web and frontend development. He creates online videos for Egghead.io, writes articles on his blog and for tech magazines, speaks at conferences and holds training workshops. Juri is also a recognized Google Developer Expert in Web Technologies

FriendlyBool Extender What?

1 min read

From now and then it happens that I have to databind some entities onto the UI, containing boolean datatypes like "Married" or "Editable" and so on. Printing out "true" or "false" is not really user friendly, so normally what is being done is to have some kind of if clauses like
if (Person.Married == true)
{
return "Yes";
}
else
{
return "No";
}
Obviously this repeats as more people have to do that. So why not create a nice C# extender on boolean types :)

public static class FriendlyBoolAnswerExtender
{

public static string ToFriendlyBoolAnswer(this bool booleanAnswer)
{
if (booleanAnswer)
return "Yes";
else
return "No";
}

}
...and the usage
true.ToFriendlyBoolAnswer();
someObject.Married.ToFriendlyBoolAnswer();
Nothing special actually, but still nice to have and the code gets a bit more elegant and readable :). Of course strings are not returned hardcoded but refactored out into resource files.
Questions? Thoughts? Hit me up on Twitter
comments powered by Disqus