Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it wrong to use numbers in Java method names?

Sometime ago, I remember being told not to use numbers in Java method names. Recently, I had a colleague ask me why and, for the life of me, I could not remember.

According to Sun (and now Oracle) the general naming convention for method names is:

Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.

Code Conventions of Java

This doesn't specifically say that numbers can't be used, although by omission you can see that it's not advised.

Consider the situatiuon (that my colleague has) where you want to perform some logic based on a specific year, for instance, a new policy that takes affect in 2011, and so your application must act on the information and process it based on it's year. Common sense could tell you that you could call the method:

boolean isSessionPost2011(int id) {}

Is it acceptable to use numbers in method names (despite the wording of the standard)? If not, why?

Edit: "This doesn't specifically say that numbers can't be used, although by omission you can see that it's not advised." Perhaps I worded this incorrectly. The standard says 'Methods should be verbs'. I read this to say that considering a number is not a verb, then method names should not use numbers.

like image 737
Cyntech Avatar asked Feb 11 '11 01:02

Cyntech


People also ask

Can we use numbers in Java method name?

Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized. This doesn't specifically say that numbers can't be used, although by omission you can see that it's not advised.

Are numbers allowed in Java class names?

Java class names cannot begin with a number. if there are multiple words in the class name like "MyClassName" each word should begin with a capital letter. eg- "MyClassName".

What is a valid method name in Java?

By convention, method names begin with a lowercase letter. The method names are verbs or verbs followed by adjectives or nouns. Each subsequent word starts with an uppercase character.

Can we use underscore in Java method name?

Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters. Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed.


2 Answers

The standard Java class library is full of classes and methods with numbers in it, like Graphics2D.

like image 134
EboMike Avatar answered Sep 18 '22 17:09

EboMike


The method seems ... overly specific.

Couldn't you instead use:

boolean isSessionAfter(int id, Date date)

?

That way the next time you have a policy applied to anything after a particular date, you don't need to copy-paste the old method and change the number - you just call it with a different date.

like image 41
Anon. Avatar answered Sep 17 '22 17:09

Anon.