2007-06-14

Underscores are terible, but_I_still_use_them

I must admit, I get the creeps when I look at some code and see unterscores used all over the place. It's so old school to me. And it has been my (lame) excuse for a while for not digging deeper into the Ruby language.

But there is one place where underscores make sense: Naming your unit test methods! It is just good to write somthing like

When_user_enters_search_term_longer_than_three_characters_Presenter_fetches_matching_products

Unfortunately it is a PITA to type such as thing. Today I came accros a really good idea from Jean-Paul Boodhoo while watching a dnrTV screencast. He has build himself a Visual studio macro that just picks up the current selection and replaces every space with an underscore.

As I was sitting on the train while watching, there was no way of downloading this macro (and I'm not sure if it can be downloaded), so I had to role my own and here it is:

Sub MakeCompileableTestName()
    DTE.Find.FindWhat = " "
    DTE.Find.ReplaceWith = "_"
    DTE.Find.Target =
vsFindTarget.vsFindTargetCurrentDocumentSelection
    DTE.Find.MatchCase = False
    DTE.Find.MatchWholeWord = False
    DTE.Find.MatchInHiddenText = False
    DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
    DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResultsNone
    DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
    DTE.Find.Execute()
End Sub

It seems to work nicely and I have assigned it to the Ctrl-Shift-Insert Keyboard shortcut.

1 comment:

Andrei Pamula said...

I tend to disagree:
http://thought-tracker.blogspot.com/2005/10/conveying-intent-in-tests.html

Matz said in an interview/mailing-list that snake_case is easier for the non-native-english speakers.

Smalltalk is more "funkier" with argument discriminators:

even_strings = aCollection select :[i| i is_even] then_collect: [even| even as_string]

IMO it makes the code more readable, more like "prose".



Now for the fun part, homework:
write a ruby module which enable objects to understand both PascalCase and snake_case.

Ex:
require 'pp'
AssertEqual([1,2,3].PrettyPrint, [1,2,3].pretty_print)

Whos.amung.us?

whos online

Ralf"s shared items