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. Inner class should be at the end of the class.
We have access to private fields of the inner class, so using getters or setters is redundant.
Writing proper variable names can be a highly valuable investment in 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.
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 names. 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 a separate private method.
Remember about DRY and KISS principles.
In this context, consider creating the findNodeByIndex
method to avoid code duplication. Also, think about creating the checkIndex
method to verify the index is valid.
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 the 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 the 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.