Clean Code Practices: Naming, Classes, Functions, and Testing
This article explains why maintaining clean, well‑structured code is essential for productivity and team collaboration, covering naming conventions, class design principles, function responsibilities, testing strategies, and tools like SonarLint, while providing concrete Java code examples for each concept.
After years of development, the author emphasizes that clean code is crucial for team efficiency, reducing bugs, and avoiding overtime; unclean code leads to poor extensibility, crashes, increased costs, and even company failure.
1. Why keep code clean? Unclean code accumulates technical debt, lowers productivity, and increases maintenance costs.
1.1 Start clean from the beginning – Refactor immediately; avoid postponing improvements. Example code snippet:
later equal never1.2 How to write clean code? High readability, no duplication, and adherence to design principles such as Single Responsibility, Open‑Closed, Liskov Substitution, Dependency Inversion, Interface Segregation, Demeter, and Composite Reuse.
2. Naming – Good names improve readability. Bad example:
public interface Animal { void abc(); }Improved version:
public interface Animal { void cry(); }Other naming issues include inconsistency, redundancy, and unclear intent; recommended prefixes like getXxx() for single objects and listXxx() for collections.
3. Classes – Should follow Single Responsibility, Open‑Closed, and high cohesion. Example of a class violating Single Responsibility:
public abstract class Sql {
public abstract void insert();
public abstract void countInsert();
}Refactor by extracting responsibilities into separate classes such as CountSql .
4. Functions – Must do one thing, have meaningful names, clean parameters, and clear return values. Example of a large function:
public String upload() {
// validate image (80 lines)
// compress image (50 lines)
// return status (5 lines)
return "0";
}Refactored version splits logic into check() , compress() , and returns the result.
Additional guidelines: avoid boolean flags, output parameters, and combine commands with queries; use exceptions instead of error codes.
5. Testing – Emphasizes Test‑Driven Development (TDD) and the FIRST principles (Fast, Independent, Repeatable, Self‑validating, Timely). Example of a unit test using the given‑when‑then pattern:
@Test
public void shouldReturnItemNameInUpperCase() {
// Given
Item mockedItem = new Item("it1", "Item 1", "This is item 1", 2000, true);
when(itemRepository.findById("it1")).thenReturn(mockedItem);
// When
String result = itemService.getItemNameUpperCase("it1");
// Then
verify(itemRepository, times(1)).findById("it1");
assertThat(result, is("ITEM 1"));
}Tools like SonarLint help detect code smells, duplicate code, potential NPEs, and enforce quality metrics.
References include the book "Clean Code" and various online articles.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.