How to break long method name into more than one line?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Mar 11 20:30:08 EDT 2012


On Sun, 11 Mar 2012 11:53:45 -0700, Herman wrote:

> I am trying to stick to the rule described in the TDD book that, each
> test method name consists of the method name to be tested, inputs and
> the expected outputs.

*The* TDD book? There's only one? Surely not.

That rule sounds utterly impractical. I can't think of anything to 
recommend it. Like any other function, method or class, tests should have 
meaningful names, but reading the name alone should not necessarily tell 
you *everything* about the function.

We have "len", not "len_sequence_or_mapping_int", and similarly it is 
perfectly reasonable to have "test_len_empty" rather than 
"test_len_emptylist_emptystr_emptyunicode_emptydict_emptyset_emptytuple_zero".

I expect that naming rule was invented by either people who have heard of 
test driven development, but never actually done it, or by people so 
anally-retentive that if they make seven short car trips over an hour, 
they check the tyre pressure, oil and water seven times because "the 
manual says to check before *every* trip".

No offence.

My advice is to moderate the naming convention of your tests with a good 
dose of common sense and aim for names which are readable rather than 
names that contain everything including the kitchen sink. Imagine you are 
in a technical meeting with some of your fellow programmers, and need to 
ask for help with a failing test. Imagine saying the name of the test 
aloud in a sentence. Does it add clarity to the discussion, or obfuscate 
it?

People's short term memory can only hold so much (allegedly "seven plus 
or minus two"), and if the name itself hits that limit, you leave nothing 
left for the rest of the sentence.

http://en.wikipedia.org/wiki/The_Magical_Number_Seven,_Plus_or_Minus_Two

Names should be practically, rather than conforming to some naming rule 
that hurts readability and obfuscates the tests.


> It takes up a lot of space and my company has a
> rule of limiting 79 characters (or 80) per line. I found that def
> abcdeef\
> dddaaa(self):
>     pass
> 
> does not work, but
> def \
> abcsajfoijfiawifoiwejfoi(self):
>     pass
> 
> works. Is this the only way to do it?

Yes. You can't split tokens over multiple lines, or put any whitespace 
between them.


-- 
Steven



More information about the Python-list mailing list