Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a good naming convention for methods that take conditional action?

Let's say I have a method, Foo(). There are only certain times when Foo() is appropriate, as determined by the method ShouldFooNow(). However, there are many times when the program must consider if Foo() is appropriate at this time. So instead of writing:

if ShouldFooNow():    Foo() 

everywhere, I just make that into one function:

def __name():     if ShouldFooNow():        Foo() 

What would be a good name for this method? I'm having a hard time coming up with a good convention. IfNecessaryFoo() is awkward, particularly if Foo() has a longer name. DoFooIfShould()? Even more awkward.

What would be a better name style?

like image 455
Nick Heiner Avatar asked Jul 01 '10 17:07

Nick Heiner


People also ask

What should be the naming convention for methods?

Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized. Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters.

What naming convention is used for most variables?

For variables, the Java naming convention is to always start with a lowercase letter and then capitalize the first letter of every subsequent word. Variables in Java are not allowed to contain white space, so variables made from compound words are to be written with a lower camel case syntax.

What is naming convention and example?

Naming-convention definitionA collection of rules followed by a set of names which allow users to deduce useful information, based on the names' character sequence and knowledge of the rules followed; such as Manhattan's East-West streets being called "Streets" and its North-South streets being called "Avenues". noun.

What makes a good function name?

A function name should clearly indicate what the function does. You don't have to scroll around, open the function source code to understand how it works. Also, the name should describe only one concept: one-to-one relation. In the end, you probably don't want to communicate like these guys!


1 Answers

I think you're pretty close. Put the action/intent at the head of the method name, for easier alphabetic searching. If I were writing something like that, I'd consider

FooIfNecessary() FooIfRequired() 

Say, for instance,

ElevatePermissionsIfNecessary() 
like image 150
Michael Petrotta Avatar answered Sep 20 '22 01:09

Michael Petrotta