SpecFlow: My preference for the step definition style

When using SpecFlow’s dialog to generate step definition skeletons, the default style is “Regular expressions in attributes”:

That produces results like this:

That looks very messy to me. The regular expression text can be read easily, but then the PascalCase method underneath looks like it is too much. I see redudant code. I see dead code. 🙂

I personally like the “Method name – underscores” style better:

That looks a lot cleaner to me. Yes, I am a little biased after having spent several years in Ruby-land, but hey, I had adopted this style in C# before that. While this is not a common format for method names in C#, I don’t really care, as nobody should be calling this methods in the common way anyway, so I rather follow a style that makes the code read better, without junk around that doesn’t add anything to my understanding of the code.

Note: by the way, I do rename those p0 and P0 references!!

Advertisements

,

  1. #1 by darrencauthon on July 14, 2017 - 12:09 pm

    Another option is to do this:

    [Given(“I have entered (.*) into the calculator”)]
    public void x(int value)
    {
    // …
    }

    The methods that come out of the method do not matter, so you can call them whatever you want. I like to call them by a single character, just to limit their access to my attention as much as possible.

    I can see why some people like the snake case, but doing so is yet-another translation to a system that’s already a little shaky. I mean, gosh, it’s one thing to bind text to text, but now we’re binding text to code magically. Get one character off, and it looks right but isn’t right.

    The same could be said for the strings, but at least then we’re binding strings to strings. It’s a bit less magical. But that said… I used SpecFlow before they had all of these helpers to jump from steps to step definitions, so perhaps it’s not so much of a problem now.

    • #2 by claudiolassala on August 31, 2017 - 1:27 pm

      That’s definitely another way to look at it. Thanks for the input!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: