Fundamentals 17 min read

How to Write High‑Quality Code: From Learning to Refactoring

This article explores how developers can master code quality by adopting a learning mindset, studying exemplary code, applying core programming principles, analyzing real‑world examples such as RocketMQ file‑warming, and finally refactoring code to improve maintainability and performance.

21CTO
21CTO
21CTO
How to Write High‑Quality Code: From Learning to Refactoring

1. Introduction

In daily work many developers complain about code quality, assuming that only their own code is good and everything else is garbage that must be refactored. The article asks what constitutes good code—readability, extensibility, high cohesion, low coupling, programming paradigms, and design principles—while noting that these criteria are hard to measure.

2. Learned Code

Rather than listing rules, the author stresses understanding underlying principles and execution patterns. Learning proceeds through four stages: learning, imitation, thinking, and innovation. Developing a learning consciousness, choosing high‑quality examples, and recognizing the historical context of conventions (e.g., the 80‑character line limit) are highlighted.

3. Imitated Code

Imitation is not blind copying; it means quickly finding excellent solutions when problems arise and applying them. The author describes cognitive traps that can stall progress and presents a visual diagram of the learning process.

4. Designed Code

Programming thinking involves four steps: decomposition, pattern recognition, abstraction, and algorithm design. Core coding principles—maintainability, optionally performance—are discussed, along with classic design principles such as the Open/Closed principle, KISS, composition, dependency inversion, and single responsibility.

5. Refactored Code

Refactoring improves software quality, performance, and extensibility by reorganizing code without changing its behavior. The article presents a case study from the RocketMQ project, showing how file‑warming writes zeros to a mapped file to avoid page‑faults and improve throughput.

代码出处:org.apache.rocketmq.store.logfile.DefaultMappedFile#warmMappedFile

The case raises three questions: the impact of page‑fault interrupts, the reason for a 4 KB loop and zero‑filling, and why a Thread.sleep(0) is used to “prevent GC”. Answers explain page‑fault costs, 4 KB alignment for storage devices, and using safepoints to trigger periodic garbage collection.

6. Summary

The article concludes that mastering code quality requires a learning mindset, critical thinking, and practical experience with real code. It stresses balancing human‑centric maintainability with machine‑centric performance, and suggests that thoughtful refactoring, when driven by business needs, yields the greatest long‑term benefits.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Software Engineeringcode qualityrefactoringlearningprogramming principles
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

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.