Remove all redundant empty lines, be careful :)
- Example:
public class Shelf { private Object[] items; public Shelf() { items = new Object[]{"book", "photo", "phone"}; } }
Your code should be easy to read. Let's 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]; } }
- Refactored code:
public class Shelf { private static final int MAX_ITEMS_NUMBER = 6; private Object[] items; public Shelf() { items = new Object[MAX_ITEMS_NUMBER]; } }
We will soon learn how these collections work.
If the logic of your code repeats - move it to the separate private method. There is even a software design principle called DRY that urges not to repeat yourself.
Writing proper variable names can be a highly valuable investment to the quality of your code.
Not only you and your teammates understand your code better, but it can also improve code sustainability in the future later on.
When you go back to the same code base and re-read it over again, you should understand what is going on.
Do not use abstract words like string
or array
as variable name. Do not use one-letter names. The name of the method should make it clear what it does.
- Bad example:
String[] arr = new String[]{"Alex", "Bob", "Alice"}; for (String s : arr) { System.out.println(s); }
- Refactored code:
String[] usernames = new String[]{"Alex", "Bob", "Alice"}; for (String username : usernames) { System.out.println(username); }