Remove all redundant empty lines, be careful :)
Your code should be easy to read. Move all hardcoded values to constant fields and give them informative names.
Bad example:
public class Shelf {
private Object[] items;
public Shelf() {
items = new Object[6];
}
}
Good example:
public class Shelf {
private static final int MAX_ITEMS_NUMBER = 6;
private Object[] items;
public Shelf() {
items = new Object[MAX_ITEMS_NUMBER];
}
}
Don't complicate if-else construction. Detailed explanation.
If the logic of your code repeats - move it to a separate private method. For example, the index checks for the get()
, set()
, and remove()
methods should be moved to a separate method.
There is even a software design principle called DRY that urges you not to repeat yourself.
Do not use abstract words like string
or array
as variable names. Do not use one-letter names.
By the name of the variable, it should be clear what it is responsible for. The name of the method should make it clear what it does.
Bad example:
private void grow() {
if (size == elementData.length) {
// grow logic
}
}
Good example:
private void grow() {
// grow logic
}
Another good example:
private void growIfArrayFull() {
if (size == elementData.length) {
// grow logic
}
}
In the first example, we have grow()
with if
inside.
This means that sometimes the array grows and sometimes it doesn't. When a developer who reads the code sees a call to the grow()
method,
he thinks that the array will be increased. Only if he reads the body of the grow()
method, he will understand that the array does not always grow.
The method name says nothing about this conditional logic.
Redundant variables are confusing and make your code less clean and much more difficult to read. Not to mention they occupy stack memory.
Use System.arraycopy() to move your array elements.
According to one of the important principles called the Single Responsibility principle, any software component must have only one responsibility, that is, to be in charge of doing just one concrete thing.
If you throw an Exception, you should always add an informative message so that it would be clear from the stack trace what exactly went wrong.