Long test method names

Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch

Writing good code is hard. For lots of reasons. One reason is the tension you try to balance when choosing identifiers. On the one hand you want your identifiers to be longer to give you enough space to make them expressive. But on the other hand you want your identifiers to be shorter so they don't balloon into monsters when combined into expressions. For example:
// too short
bool is_leap_year(int y)
{
    return y % 4 == 0 && y % 100 != 0 || y % 400 == 0;
}

// too long
bool is_leap_year(int intYearValue)
{
    return intYearValue % 4 == 0 && intYearValue % 100 != 0 || intYearValue % 400 == 0;
}

// just right, said Goldilocks
bool is_leap_year(int year)
{
    return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}
However, the names of your test methods are not subject to this design tension. The names of your test methods should never be part of a larger expression. So loosen up! Feel free to use long test method names. Names that reflect the specification not the implementation.
// not like this
static void test_leap_year()
{
    assert(!is_leap_year(1999));
}

// like this
static void years_not_divisible_by_4_are_not_leap_years()
{
    assert(!is_leap_year(1999));
    assert(!is_leap_year(2001));
}


2 comments:

  1. I_just_wish_there_was_a_way_to_ditch_underscores_and_use_regular_spaces_instead_like_in_ruby

    ReplyDelete
  2. There is. See the CATCH unit test framework by Phil Nash

    ReplyDelete