Skip to content

Checking your reading list…

Retry verification

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Clean Code Practices: Naming, Classes, Functions, and Testing
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.

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.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

Report an issue with this article
testingsoftware designrefactoringClean Codenaming conventionsSOLID

Reader feedback

Was this useful?

Useful0

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion