Cyntech Cyntech - 3 days ago 4
Java Question

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.

Answer

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

Comments