Good And Bad Practices After 997 Confusing Lines Of Java
By: Živković Miloš
Tags: Code Review, Pull Request, Clean Code, Refactoring, Java

Good And Bad Practices After 997 Confusing Lines Of Java

Photo by Ricardo Gomez Angel on Unsplash

Doing code reviews is hard, even more, if you have more experience than others. After merging over 100 PRs, and written over 900 lines of code, I know what makes a good review.

I’ll reveal some of the most common mistakes, and how to avoid them. Code reviews are crucial to avoid a lot of errors.

Refactoring code can lead to a lot of issues. Those are of the highest priority as they can break the existing flows. I’ll break down what to look for in refactoring PRs.

Java Streams

Java streams have become the de facto standard in dealing with arrays, lists, and other enumerable. Using for loop or a lot of temporary variables can be refactored using Java streams. Streams provide a robust interface that can be used for mapping, filtering, and reducing.

Using Optional over Null

Optional prevents a lot of null pointer exceptions. Using it is crucial where service can provide null as a result. Optional has a similar interface for mapping and filtering. Mapping is really handy to manipulate classes and data.

Optional doesn’t scratch every itch but can provide readable code. Sometimes you’ll need to use null. What comes as a great method of Optional is ifPresent which will execute function only if optional is present.

Existing Code Improvements

Improving the existing code or changing it poses a lot of risk for the previous features. That’s the first thing developer should have in mind. Changing code requires changes in tests, so we are sure everything works as expected.

Checking if code is actually doing all that is required is great while reviewing. Contacting team members to clarify on PR can also help a lot.

Refactoring

Refactoring is the process of changing a software system in such a way that it does not alter the external behavior of the code yet improves its internal structure. Martin Fowler

By improving and updating code, we can improve the quality and maintenance in the long run. Refactoring PRs spread knowledge across the team. These PRs spark discussion and new ideas.

Refactoring doesn’t have a goal to just reduce the number of lines. It needs to reveal the intent. Propose a new way of thinking. One should think of code duplication. Do we have similar logic somewhere else?

Check if the code belongs where it is at the moment. Do we need to recalculate the cart in the user facade or in the cart facade? Placing code where it belongs increases code maintainability.

Maintainance > Quick Fix

Someone will get my code, and that can be me. Think about it when you weld some code to just work. Take a step back and think of other solutions. Consult your team members. Somebody probably worked in that problematic area.

Reducing Operational Costs

Does that log need to be in the database? Check again. Maybe you can spare some memory by posting it directly to the customer. Reducing costs can increase your value in the team.

Duplicated code can also lead to operational costs. One can lose a lot of time to find scattered validation logic. Smart developers would place it in one place and apply where needed.

Thank you for reading!