Joseph Nields
Joseph Nields

Reputation: 5661

Is there a convention for modifying the naming of identifiers that would otherwise be keywords in Java?

For example, you might do something like this:

public final class Library {
    public void checkOut(Book book) { /* banana banana banana */ }
    public void return_(Book book) { /* banana banana banana */ }
}

but is there an actual convention that Java programmers should follow?

Upvotes: 3

Views: 94

Answers (4)

Filip Zymek
Filip Zymek

Reputation: 2646

You should just use meaningful names. So that when another dev (or even non-technical person) will read your code he/she would easily understand what is going on.

a.b(c) is much shorter but member.checkOut(encyclopedia) is much much better to understand

Upvotes: 1

user177800
user177800

Reputation:

Semantics Are Important

return() should be checkIn() problem solved!

Also return is a a terrible word by itself, it is no better than something as ambiguous as type or flag or any of hundreds of other labels that I see every day that have no independent semantic and require lots of context for a specific meaning.

Upvotes: 7

Bill K
Bill K

Reputation: 62769

Jarrod has an excellent point, work around the problem.

The one horrible case is where some tool expects to see the bean pattern and reacts poorly if you use "isReturnable()" or getBookPageCount() where the page count is calculated instead of an attribute. In these cases I sometimes see "izReturnable()" which looks horrible, but it is what it is. the other can often be rephrased to something else like countPages(). In general the bean pattern just sucks and should be nuked from orbit and replaced with annotations.

Oh also, the other ugly case is a variable named "class" which is a great description of a variable containing a "class" object, many people use clazz--you just kinda get used to it.

But vs keyword I guess I'd say that the convention is just to use a more descriptive method/variable name.

Upvotes: 0

user4695271
user4695271

Reputation:

There are no conventions (as far as I know). Generally, when you need to use a keyword as an identifier or something else, there will be (almost always) another word that is most appropriate to change it for.

Upvotes: 0

Related Questions