Question

In my web app framework, each page can have a precondition that needs to be satisfied before it can be displayed to the user. For example, if user 1 and user 2 are playing a back-and-forth role-playing game, user 2 needs to wait for user 1 to finish his turn before he can take his turn. Otherwise, the user is displayed a waiting page.

This is implemented with a predicate:

def precondition(self):
    return user_1.completed_turn

The simplest name for this API is precondition, but this leads to code like if precondition(): ..., which is not really obvious. Seems to me like it is more accurate to call it precondition_is_met(), but not sure about that either.

Is there a best practice for naming methods like this?

Was it helpful?

Solution

The precondition seems to be part of the public API. Therefore, the naming should primarily reflect its usage by a consumer of your API. How your code handles that internally is another question all together.

I assume a user will provide a Page object that implements this precondition method. In that case, implementing a method precondition feels much more natural, declarative, and DWIMmy that the verbose (to the point of being Javaesque) precondition_is_met. Notice that the method is this precondition, and invoking it determines whether it was met.

If I am mistaken and the function is solely used in your internals, the longer name seems to be justified, although you could perhaps elide the is and use precondition_met or precondition_fulfilled.

Licensed under: CC-BY-SA with attribution
scroll top