Jake Wharton has published an article titled Just Say mNo to Hungarian Notation and if you agree with what he says, here's a little tip to help you remember to say mNo by making use of the Search Structurally feature of Studio.

So let's start off with a very simple app with The Disease in there.

Basic app

So let's start off by finding the offending name. Use the Find Action shortcut (Shift-Command-A for Mac, Ctrl-Shift-A for Linux/Windows) and type in "Search Structurally"

Search Structurally

This will bring up the Search Template. Hit the button to Copy Template and select "all fields of the class". The template should now look like this:

Search Template

Hit the button Edit variables and select the FieldName variable from the list. In the Text/regexp fields enter (m|s)[A-Z].*

This regular expression will match any string that starts with a lower case 'm' or 's' followed by a capital letter. Feel free to experiment and change this to suit. This was just a quick hack and worked for me, but might not for you.

Edit Variables

Once you've hit ok you can click on Find and see what the search finds. There will probably be many more results than you expect as it looks through a lot of code by default - generated code, some API code. Try experimenting with the Scope before you hit Find to see if you can make it just search your code.

The result should show something like this:

Running the search

I've highlighed the deviant in our example code.

That's all well and good, but it's a pain to have to run that every so often to find any slip-ups you've made. So let's make it better. Bring up the Preferences of Studio, go to Inspections -> General and look for Structural Search Inspection.


Tick the box and then click on the little plus sign. From the pop-up menu click on Add Search template...

Adding the template

The familiar Structural Search template window pops up with the last structural search you did. Hit OK and give the template a name.

Naming it

By default this will show in Studio as a warning, but we want to eradicate The Disease, so we're going to change it so that it's a error. So click on the Severity drop down and make the change.

Changing the severity

Hit OK and have a look what it has done to the code.

The finished product

And would you look at that? The red wiggly line of doom has appeared over our variable. If we hover our mouse over it we are told the error of our ways.

Studio also has Replace Structurally. I tried - and failed - to get it to turn the error into a quickfix, i.e. remove the 'm' or 's' and lowercase the first letter of what's left. Perhaps someone who knows a little more than me can sort that out. But at least this is a start.

Darren @ Æ


comments powered by Disqus