Fundamentals 13 min read

Clean Code Practices: Naming, Classes, Functions, and Testing

This article explains why clean code matters for productivity and project health, and provides practical guidelines on naming, class design, function design, SOLID principles, testing strategies, and tooling to help developers write maintainable, readable software.

Architecture Digest
Architecture Digest
Architecture Digest
Clean Code Practices: Naming, Classes, Functions, and Testing

Clean code is essential for project success, reducing bugs, maintenance cost, and overtime; the article outlines four main chapters—naming, classes, functions, and testing—to help developers write tidy code.

1. Why Keep Code Clean?

Unclean code grows over time, lowering productivity and causing issues such as poor extensibility, crashes, overtime, and increased company costs, even risking failure.

2. Naming

Good names improve readability and reduce understanding cost. Bad naming examples include meaningless names and inconsistent prefixes. Refactor examples:

public interface Animal { void abc(); }

public interface Animal { void cry(); }

Avoid redundant words and follow conventions like using getXxx() for single objects and listXxx() for collections.

3. Classes

Classes should follow Single Responsibility, Open‑Closed, and high cohesion. Example of a class with multiple responsibilities:

public abstract class Sql { void insert(); void countInsert(); }

Refactor into separate classes:

public abstract class CountSql { void countInsert(); }

3.1 Single Responsibility

Each class should have one reason to change, making it smaller and easier to maintain.

3.2 Open‑Closed Principle

Code should be open for extension but closed for modification. Example of violating OCP:

public abstract class Sql { void insert(); void update(); void delete(); }

Refactor by extracting specific operations into subclasses.

4. Functions

Functions should do one thing, have descriptive names, clean parameters, no side effects, and clear return values. Example of a long function:

public class PicService { public String upload() { /* validation 80 lines */ /* compression 50 lines */ /* return 5 lines */ } }

Refactor into smaller steps:

public String upload() { check(); compress(); return "0"; }

Avoid boolean flags, output parameters, and use exceptions instead of error codes.

5. Testing

Testing validates code quality; adopt TDD, FIRST principles, and given‑when‑then pattern. Example test:

@Test public void shouldReturnItemNameInUpperCase() { // Given Item mocked = new Item("it1", "Item 1", ...); when(itemRepository.findById("it1")).thenReturn(mocked); // When String result = itemService.getItemNameUpperCase("it1"); // Then verify(itemRepository, times(1)).findById("it1"); assertThat(result, is("ITEM 1")); }

Tools like SonarLint can automatically detect code smells, duplicated code, and potential bugs.

Testingsoftware designrefactoringclean codenaming-conventionsSOLID
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.