Remove all redundant empty lines, be careful :)
The purpose of inner classes is to group classes that belong together, which makes your code more readable and maintainable.
We have access to private fields of inner class, so using getters or setters is redundant.
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); }
Don't complicate if-else construction. Detailed explanation.
If the logic of your code repeats - move it to the separate private method. Remember about DRY and KISS principles.
Redundant variables are confusing and make your code less clean and much more difficult to read. Not to mention they occupy stack memory.
You should never want to expose the object fields directly. They should be accessed through special methods (getters and setters).
Pay attention to the index you receive as an input, you can iterate only first or second half of the list, depending on the index value. That will boost your lists' performance.
This logic will be used in 2 methods: when we remove from list by index and by value. So let's create a method that will take a Node that needs to be removed and unlink it. As a result we will call it from both methods when Node is found.
Make sure that you add mutual links from both sides when you insert a new Node, and that you remove all the links when you remove a Node.