4

For example, I have a Selenium WebDriver and as part of my initialization I would like to check if I am able to instantiate it and start up browser.

I can't come up with a proper name. Is there any common-agreed practice, like naming methods with side effects to be verbs? Should it be one of these options, or something else?

def driver_works(): ...

if driver_works():
    pass
def does_driver_work(): ...

if does_driver_work():
    pass
def is_driver_working(): ...

if driver_is_working():
    pass
def driver_is_working(): ...

if is_driver_working():
    pass

2 Answers 2

6

Fluent names are preferred. These allow the code to be read while ignoring types and structure. Prefer names that focus on abstraction:

if driver_is_working():

Though some still cling to prefixes that signal type:

if is_driver_working():

We do not prefer names that force us to mangle English grammar:

if does_driver_work():

We do not prefer names that make assertions that may not be true unless they throw exceptions:

if driver_works():

And by we I hopefully mean all developers. But I likely only mean developers who have to read your code. Developers who write code seem to prefer all sorts of ungodly things

4
  • 1
    And just for the completeness, by "we" you mean who?
    – toriningen
    Jun 19, 2017 at 14:54
  • @toriningen note edit. Jun 19, 2017 at 15:35
  • I would like to add - though not worth an answer - that just adding words because it sounds better really doesn't help. "if the_driver_is_working():" does not offer much extra, even if it might be more grammatically correct. Jun 19, 2017 at 15:54
  • 1
    @ErdrikIronrose True. We don't like needless fluff. Prefer Ernest Hemingway's style to H.P. Lovecraft's. Jun 19, 2017 at 16:01
2

There's more we like.

  • We like active form for functions/methods that do something: Rather PopulateList() than ListPopulation(). For this kind of action performing functions it may help to think "VerbNoun()".

  • If it is about obtaining a return value, lots of us like the name to start with "Get". This is a bit language/culture dependent but you can hardly go wrong with this. For storing values, "Set" or "Put" is common.

  • For a status like in your example, commonly used words are "Active" and "Enabled". Other words are acceptable if they convey the status but we do encourage using prefixes like Is, Has or Can. For your driver example you could use IsOperational, IsAvailable, IsReadyToRumble or CanDrive.

  • We do not like using underscores if there is no need for them. You want to save those for separating domains. Like Class1_DoYourThing and Class2_DoYourThing. If you would write Class1_Do_Your_Thing it may not be obvious what parts belong together. This is particularly an issue with event handlers for different classes.

If you follow these guidelines you will make us very happy.

1
  • I like most of this (+1) but the underscore thing is a Python thing. I know toriningen didn't use a Python tag but that code looks suspiciously like Python. Jun 20, 2017 at 5:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.