Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming convention for interface / class / mockClass in Java?

Tags:

java

I'm creating a mock class for a Lexer object, and I think I may need to do some refactoring. I have two options:

  1. Create an interface Lexer, and rename the current Lexer to something like RealLexer. Have MockLexer implement Lexer, and method calls take anything of type Lexer. I dislike that my precious Lexer class is now renamed to something that has no meaning if you don't know that there's a mock class.
  2. Create an interface LexerInterface (which I already dislike, since it has Interface in its name), but allowing myself to keep the current Lexer the way it is. MockLexer then implements LexerInterface. Another downside is that method calls take LexerInterface as params.

Both options smell bad to me, so I figured I'd let standards decide for me. Has anyone had experience with this?

like image 471
munchybunch Avatar asked Oct 25 '11 03:10

munchybunch


People also ask

What is the naming convention for interface in Java?

Interface names should be capitalized like class names. 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.

How do you name an interface class?

Naming InterfacesInterfaces should be in the title case with the first letter of each separate word capitalized. In some cases, interfaces can be nouns as well when they present a family of classes e.g. List and Map .

Which is the correct naming convention for naming a Java file?

Java uses CamelCase as a practice for writing names of methods, variables, classes, packages, and constants.

Should interface name start with I in Java?

There are two main naming conventions for interfaces in Java: no prefix or suffix for interfaces, ~Impl suffix for implementations, ~I prefix for interfaces.


2 Answers

I'd definitely vote for using Lexer as your interface name. How about adding some information about how or why your implementation does its thing as part of the name? E.g.:

  • StringParsingLexer
  • TokenizingLexer
  • SingleThreadedLexer
  • {ThirdPartyLibraryName}DelegatingLexer

Also, do you really need to be explicitly constructing a MockLexer? Using a framework like Mockito can make your testing considerably easier and faster; You can get started as easily as:

Lexer mockLexer = Mockito.mock(Lexer.class);

Mockito.when(mockLexer.doFoo()).thenReturn("bar");
like image 148
millhouse Avatar answered Sep 19 '22 18:09

millhouse


My recommendation, as I stated in the comments, is to use Lexer for your interface and DefaultLexer for the default implementation. This pattern is used quite frequently and as such is very understandable to anyone who will be maintaining your code. As for the mock object, it would also be understandable to name this something like MockLexer.

As an example of a naming convention that Java uses:

javax.swing.table.TableModel is an interface
javax.swing.table.AbstractTableModel is an abstract class implementing TableModel
javax.swing.table.DefaultTableModel is an implementation of AbstractTableModel.

There is however, no recommendation in the Java Codding Conventions outside of using capital letters, nouns, etc.

like image 42
Jonathan Spooner Avatar answered Sep 20 '22 18:09

Jonathan Spooner