3

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?

Joseph Nields
  • 5,527
  • 2
  • 32
  • 48
  • There are most certainly naming conventions! Check the GOOGLE page: https://google-styleguide.googlecode.com/svn/trunk/javaguide.html Naming conventions vary among developers, but generally try to not use names that would be confusing to another developer; naming something "return_" is not a good idea. – Evan Bechtol Mar 30 '15 at 20:05
  • [`clazz` is used for `class`](http://stackoverflow.com/q/2529974/781792). Beyond that, I'm guessing no, there's no convention. You should simply avoid using keywords as identifiers. – Tim S. Mar 30 '15 at 20:07
  • 1
    `klass` and `cls` are used the same amount as `clazz` and in both cases are about as unimaginative as you can get. –  Mar 30 '15 at 20:12

4 Answers4

7

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.

Community
  • 1
  • 1
1

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

Filip Zymek
  • 2,626
  • 4
  • 22
  • 31
0

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.

x80486
  • 6,627
  • 5
  • 52
  • 111
0

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.

Bill K
  • 62,186
  • 18
  • 105
  • 157