New CodeRush Plugin – Sort Format Tokens

Some programmers feel that they have some sort of very minor OCD. Despite no real rational reason, they feel that certain things should be a certain way. The world seems out of place, slightly of kilter, slightly more irritating or distracting when these things are not quite as they would like them to be.

Of course when I say ‘Some programmers’, I certainly include myself in that group.

Today’s plugin is kind of an OCD plugin. It’s just my personal attempt to scratch a little itch. I’d be amazed if this helps anyone else at all. I’m just putting it out there as an example of just how flexible CodeRush can be.

When I set out to create it, I had no other purpose in mind, than the appeasement of that part of my brain, which objected every time I didn’t adjust my code to this specific pattern.

That said, the plugin does have some benefits as a natural consequence of the way it works.

Sort Format Tokens

This plugin operates on a subset of the String.Format calls.

Consider the following call:

SortFormatTokensExample1

It’s simple and it does exactly what you need it to. However over time you might feel the need to add more elements to the message:

SortFormatTokensExample2

Something about this statement irks me. Something just doesn’t sit right. So I find myself rewriting it

SortFormatTokensExample3

This new code operates exactly the same as the old code. There isn’t even a performance benefit here.

However my particular brand of very minor OCD feels better and I’m able to scan through the code and comprehend it quicker.

The Problem

Changing one statement into the other is a manual task. There is the risk of typos and logic errors, and of course it takes time to achieve.

Not very much time I guess, but every moment that I spend thinking about this sort of thing, is less time spent on solving business problems and adding value.

Therefore I did what I usually do when I spot a coding task I’d prefer not to have to do manually, which has an predictable pattern to it….

I wrote a plugin Open-mouthed smile

The Solution –> Sort String Tokens for CodeRush

This plugin does exactly what it says on the tin.

It rewrites the a String.Format call so that…..

  • The string tokens are (where appropriate) in numerical order.
  • The arguments that follow the string are in the same matching order.

Simply place your caret within the first argument of a String.Format call and choose 'Sort Format Tokens’ from the Code subsection of your CodeRush\Refactor Smart tag menu.

SmartTagMenuSortFormatTokens

Bonus Features

Beyond the original spec of “Sort my string tokens so that they look nice”, SortFormatTokens is also careful to correctly interpret several corner case scenarios.

  • It deals correctly with sequences of tokens with missing items.
    Gaps in the sequence are removed.  “{1} – {3} – {6}”  becomes “{0} – {1} – {2}”.
  • It deals correctly with string tokens that have formatting information.
    Formatting information is retained within the renumbered token. “{1:00} – {0} – {2}” becomes “{0:00} – {1} – {2}”
  • It deals correctly with duplicate usage of a token within a string.
    The 2nd and further instances of any given token are renumbered the same way as the first instance. “{1} – {0} – {1}” becomes “{0} – {1} – {0}”.
  • It also removes any unreferenced arguments from the call, optimizing your call to be faster in the process.

Where can I get this wonderful plugin from?

As usual this plugin is available from the Visual Studio Gallery and from GitHub.

The source code is available on GitHub

Free DevExpress Products - Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We'll be happy to follow-up.
No Comments

Please login or register to post comments.