Tidying up user data for display

Today I was tidying up a quick WPF application i had built for a client. It simply renders "today's schedule" in a way that is meaningful for them and allows them to view it on multiple screens and print as necessary.

Development was quick as it only consumed several Google Calendar feeds and based upon my test data worked well.

However today seeing used in real life made me rethink how i rendered the data, in my test data I entered items how I would enter them, this is important, I entered them using sentence casing for paragraphs, title casing for titles etc. However due to the way some of the users worked everything was being entered in CAPS :(

Functionally the app works fine, however for me, and I'm no designer, it looked odd. So I opened the solution again and started to look at ways of improving this. My initial thoughts were to apply some sort of styling, think text-transform:capitalize; if you were using CSS, but alas XAML doesn't have this. I then thought I could implement a custom formatter that I could use in the XAML upon databinding, I could have done this, but I chose not too. Although it makes sense to do so, I started thinking about how I will probably end up using the data access / domain code in this project later on in another GUI where I will no doubt have the same problem.

As a result I wanted to "fix" this data at the domain level in C#. As soon as you hit the code you know there are many ways of achieving this, you could go down the route of RegEx replacing, iterating through the string looking for .'s or whitespace if you want title case etc and then do some replacing. You could even just specify everything is lowercase, but for me none of these fully fitted what I wanted / effort level I wanted to put in for an issue that only I really had.

What I really wanted was a ToTitleCase or ToSentanceCase that already exists the framework, I didn't want to go grab extension methods which I'm almost certain there will be many of. A quick bit of poking around led me to this gem.
I refer to MSDN the ToTitleCase method "Converts the specified string to titlecase." Great, exactly what I wanted. It's easy to use too:

// Defines the string with mixed casing.
      string myString = "wAr aNd pEaCe";

      // Creates a TextInfo based on the British culture.
      TextInfo myTI = new CultureInfo("en-GB",false).TextInfo;

      // Changes a string to titlecase.
      Console.WriteLine( "\"{0}\" to titlecase: {1}", myString, myTI.ToTitleCase( myString ) );

Fantastic, build, run, enjoy.....
Well kinda.... This is one method you really do need to read the remarks for on the MSDN page,
this method does not currently provide proper casing to convert a word that is entirely uppercase, such as an acronym.
the ToTitleCase method provides an arbitrary casing behavior which is not necessarily linguistically correct. A linguistically correct solution would require additional rules, and the current algorithm is somewhat simpler and faster. We reserve the right to make this API slower in the future.

This actually meant that in my case, when people added descriptions entirely in uppercase the method did nothing. Bit of a shame, I made the conscious decision that Titles I would use ToTitleCase in the hope to improve titles where people enter one with all lower case or mix use, but if they use entirely uppercase then I am unfortunate. However for descriptions I decided to lowercase the string and then use ToTitleCase. Now this isn't sentence casing but it does look better than all caps. This is a compromise, I was able to improve the app without spending too much time on it.

ToTitleCase is one of those hidden gems, (just like using XmlConvert.ToString with a DateTime will give you the DateTime in RFC 3339 which is fantastic for use with Google Calendar API's etc... but that's a blog post for another day ... ), which can save you time and provide quick wins, it's also culture sensitive which can really help you out if you have a globalised project, just be clear on what it does and what it doesn't do.



Popular posts from this blog

WebUSB - An unexpected update...

Can you use BuildRoot with Windows Subsystem for Linux......

DotNet CLI , private NuGet feeds and Linux...