VibeKoding / Ensiklopedia ยท Fondasi KuatEnsiklopedia ยท Fondasi Kuat / An Introduction to Code Quality and RefactoringAn Introduction to Code Quality and Refactoring
VK

An Introduction to Code Quality and RefactoringAn Introduction to Code Quality and Refactoring

๐Ÿ“š Ensiklopedia ยท Fondasi KuatEnsiklopedia ยท Fondasi Kuat ๐ŸŒ Dual Bahasa (ID / EN) โšก VibeKoding Native

Ensiklopedia VibeKoding: An Introduction to Code Quality and Refactoring.Ensiklopedia VibeKoding: An Introduction to Code Quality and Refactoring.

๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

Is it enough that your code just works? You may have written code like this: the feature works, but two weeks later even you can't understand it. Or someone on the team left behind a pile of "code that only God and they can understand." This chapter helps you understand what good code is, how to identify bad code, and how to safely improve it.Is it enough that your code just works? You may have written code like this: the feature works, but two weeks later even you can't understand it. Or someone on the team left behind a pile of "code that only God and they can understand." This chapter helps you understand what good code is, how to identify bad code, and how to safely improve it.

What will you learn in this article?What will you learn in this article?

ChapterContentCore Concepts
Chapter 1Code SmellsIdentifying common problems
Chapter 2Refactoring TechniquesSafely improving code
Chapter 3Code ReviewQuality assurance in team collaboration
Chapter 4Quality MetricsMeasuring code health with data

After reading this chapter, you will be able to identify code problems, refactor safely, and continuously improve code quality through team collaboration.After reading this chapter, you will be able to identify code problems, refactor safely, and continuously improve code quality through team collaboration.

------

0. The Big Picture: The Lifecycle of Code0. The Big Picture: The Lifecycle of Code

In software development, there is an often-overlooked fact: code is read far more times than it is written.In software development, there is an often-overlooked fact: code is read far more times than it is written.

A piece of code, from creation to retirement, roughly goes through this journey:A piece of code, from creation to retirement, roughly goes through this journey:

๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

- Writing Phase: A developer writes the first implementation. The feature works, tests pass. - Review Phase: Team members read the code and suggest improvements. - Maintenance Phase: Fixing bugs, adding features, adapting to new requirements โ€” this phase accounts for over 80% of the code's lifecycle. - Refactoring Phase: When code becomes hard to maintain, the internal structure needs to be improved without changing external behavior. - Retirement Phase: Technology evolves, and old code is replaced by new solutions.- Writing Phase: A developer writes the first implementation. The feature works, tests pass. - Review Phase: Team members read the code and suggest improvements. - Maintenance Phase: Fixing bugs, adding features, adapting to new requirements โ€” this phase accounts for over 80% of the code's lifecycle. - Refactoring Phase: When code becomes hard to maintain, the internal structure needs to be improved without changing external behavior. - Retirement Phase: Technology evolves, and old code is replaced by new solutions.

Martin Fowler once said in Refactoring: "Any fool can write code that a computer can understand. Good programmers write code that humans can understand."Martin Fowler once said in Refactoring: "Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

------

1. Code Smells: Identifying Common Problems1. Code Smells: Identifying Common Problems

1.1 Overview of Code Smells1.1 Overview of Code Smells

The concept of "Code Smell" was proposed by Kent Beck. It refers to characteristics in code that are not bugs but suggest deeper design problems. It's like a strange odor in a room โ€” it won't make you sick immediately, but it signals that something needs cleaning.The concept of "Code Smell" was proposed by Kent Beck. It refers to characteristics in code that are not bugs but suggest deeper design problems. It's like a strange odor in a room โ€” it won't make you sick immediately, but it signals that something needs cleaning.

Use the interactive component below to identify some of the most common code smells:Use the interactive component below to identify some of the most common code smells:

1.2 Common Code Smells Checklist1.2 Common Code Smells Checklist

Code SmellSymptomHarm
Long MethodFunction exceeds 50 linesHard to understand, test, and reuse
Magic NumbersWriting 86400000 directly in codeUnclear meaning, easy to miss when modifying
Duplicated CodeSimilar logic in multiple placesMust sync changes everywhere, easy to miss
Deep NestingMore than 3 levels of if/forLogic like a maze, hard to follow
Long Parameter ListFunction has more than 4 parametersDifficult to call, easy to mix up order
God ClassOne class/module does too muchUnclear responsibilities, change one thing and everything breaks
๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

Code smells are not "errors" โ€” they are "signals." They tell you: the design here may need improvement. Not all smells need to be fixed immediately, but you need the ability to recognize them.Code smells are not "errors" โ€” they are "signals." They tell you: the design here may need improvement. Not all smells need to be fixed immediately, but you need the ability to recognize them.

------

2. Refactoring Techniques: Safely Improving Code2. Refactoring Techniques: Safely Improving Code

2.1 Overview of Refactoring2.1 Overview of Refactoring

The definition of Refactoring is precise: improving the internal structure of code without changing its external behavior.The definition of Refactoring is precise: improving the internal structure of code without changing its external behavior.

The key phrase is "without changing external behavior." Refactoring is not rewriting, not adding features, not fixing bugs. It is the "organizing and tidying up" of code internals.The key phrase is "without changing external behavior." Refactoring is not rewriting, not adding features, not fixing bugs. It is the "organizing and tidying up" of code internals.

Use the component below to compare the before and after of common refactoring techniques:Use the component below to compare the before and after of common refactoring techniques:

2.2 Common Refactoring Techniques2.2 Common Refactoring Techniques

Extract FunctionExtract Function

This is the most commonly used refactoring technique. When a piece of code can be summarized with a meaningful name, it should be extracted into a function.This is the most commonly used refactoring technique. When a piece of code can be summarized with a meaningful name, it should be extracted into a function.

javascript
// Before refactoring function printReport(data) { // Calculate total price let total = 0 for (const item of data.items) { total += item.price * item.qty } // Print... } // After refactoring function calculateTotal(items) { return items.reduce((sum, item) => sum + item.price * item.qty, 0) } function printReport(data) { const total = calculateTotal(data.items) // Print... }

RenameRename

Good naming is the cheapest and most effective documentation. When you need to write a comment to explain what a variable or function means, its name is not good enough.Good naming is the cheapest and most effective documentation. When you need to write a comment to explain what a variable or function means, its name is not good enough.

javascript
// Before refactoring const d = new Date() - startTime // Elapsed time const arr = users.filter(u => u.a) // Active users // After refactoring const elapsedMs = new Date() - startTime const activeUsers = users.filter(user => user.isActive)

Replace Nested Conditional with Guard ClausesReplace Nested Conditional with Guard Clauses

javascript
// Before refactoring function getPayAmount(employee) { if (employee.isSeparated) { return { amount: 0 } } else { if (employee.isRetired) { return { amount: employee.pension } } else { return { amount: employee.salary } } } } // After refactoring function getPayAmount(employee) { if (employee.isSeparated) return { amount: 0 } if (employee.isRetired) return { amount: employee.pension } return { amount: employee.salary } }
๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

The biggest risk of refactoring is "introducing bugs while making changes." So the prerequisite for refactoring is having test coverage. Run tests after each small refactoring step to ensure behavior hasn't changed. For code without tests, add tests first before refactoring.The biggest risk of refactoring is "introducing bugs while making changes." So the prerequisite for refactoring is having test coverage. Run tests after each small refactoring step to ensure behavior hasn't changed. For code without tests, add tests first before refactoring.

------

3. Code Review: Quality Assurance in Team Collaboration3. Code Review: Quality Assurance in Team Collaboration

3.1 Motivation for Coding Review3.1 Motivation for Coding Review

Code Review is one of the most effective quality assurance methods in a team. Its value goes beyond finding bugs:Code Review is one of the most effective quality assurance methods in a team. Its value goes beyond finding bugs:

3.2 What to Review3.2 What to Review

DimensionFocus
CorrectnessIs the logic correct? Are edge cases handled?
ReadabilityAre names clear? Is the structure easy to understand?
SecurityAre there injection risks? Is sensitive data exposed?
PerformanceAre there obvious performance issues? N+1 queries?
TestingAre there corresponding tests? Do they cover critical paths?

3.3 Review Etiquette3.3 Review Etiquette

Good code review is a discussion about code, not criticism of people:Good code review is a discussion about code, not criticism of people:

------

4. Code Quality Metrics4. Code Quality Metrics

4.1 Cyclomatic Complexity4.1 Cyclomatic Complexity

Cyclomatic Complexity measures the number of independent paths in code. Each if, for, case, &&, || increases complexity.Cyclomatic Complexity measures the number of independent paths in code. Each if, for, case, &&, || increases complexity.

ComplexityRatingRecommendation
1-10SimpleEasy to understand and test
11-20ModerateConsider splitting
21-50ComplexMust refactor
50+UnmaintainableUrgent refactoring needed

4.2 Code Coverage4.2 Code Coverage

Code coverage measures what proportion of code is executed by tests. Common metrics:Code coverage measures what proportion of code is executed by tests. Common metrics:

๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

80% coverage does not mean good code quality. Coverage only tells you "which code hasn't been tested," not "whether the tests are meaningful." A test that only asserts expect(true).toBe(true) can increase coverage but is completely worthless.80% coverage does not mean good code quality. Coverage only tells you "which code hasn't been tested," not "whether the tests are meaningful." A test that only asserts expect(true).toBe(true) can increase coverage but is completely worthless.

4.3 Practical Tools4.3 Practical Tools

ToolPurpose
ESLintJavaScript/TypeScript static analysis
PrettierCode formatting, consistent style
SonarQubeComprehensive code quality platform
HuskyGit hooks, automatic checks before commits

------

5. AI-Powered: Using LLMs to Improve Code Quality5. AI-Powered: Using LLMs to Improve Code Quality

LLMs are already very practical in the code quality domain โ€” they can serve as your "24/7 online code reviewer."LLMs are already very practical in the code quality domain โ€” they can serve as your "24/7 online code reviewer."

5.1 Identifying Code Smells5.1 Identifying Code Smells

> Prompt:> Prompt:

> ```> ```

> Please review the following code and identify code smells, including but not limited to:> Please review the following code and identify code smells, including but not limited to:

> long methods, magic numbers, duplicated code, deep nesting, long parameter lists.> long methods, magic numbers, duplicated code, deep nesting, long parameter lists.

> For each issue, provide the specific location, description, and improvement suggestions.> For each issue, provide the specific location, description, and improvement suggestions.

>>

> [Paste your code]> [Paste your code]

> ```> ```

5.2 Automated Refactoring5.2 Automated Refactoring

> Prompt:> Prompt:

> ```> ```

> Please refactor the following code with these requirements:> Please refactor the following code with these requirements:

> 1. Do not change external behavior> 1. Do not change external behavior

> 2. Use techniques like extract function, guard clauses to replace nesting> 2. Use techniques like extract function, guard clauses to replace nesting

> 3. Improve naming, eliminate magic numbers> 3. Improve naming, eliminate magic numbers

> 4. Explain the reasoning behind each refactoring step> 4. Explain the reasoning behind each refactoring step

>>

> [Paste your code]> [Paste your code]

> ```> ```

5.3 Simulated Code Review5.3 Simulated Code Review

> Prompt:> Prompt:

> ```> ```

> Please review this code from the perspective of a senior developer, providing feedback on these dimensions:> Please review this code from the perspective of a senior developer, providing feedback on these dimensions:

> - Correctness: Are there logic bugs? Are edge cases handled?> - Correctness: Are there logic bugs? Are edge cases handled?

> - Readability: Are names clear? Is the structure easy to understand?> - Readability: Are names clear? Is the structure easy to understand?

> - Performance: Are there obvious performance issues?> - Performance: Are there obvious performance issues?

> - Security: Are there injection or data leakage risks?> - Security: Are there injection or data leakage risks?

> Use a "suggestion" tone rather than "command," and provide improvement plans.> Use a "suggestion" tone rather than "command," and provide improvement plans.

>>

> [Paste your code]> [Paste your code]

> ```> ```

๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

You need to verify AI's refactoring suggestions yourself โ€” run tests to confirm behavior hasn't changed. Treat AI as a "colleague who gives suggestions," not an "authority to trust unconditionally."You need to verify AI's refactoring suggestions yourself โ€” run tests to confirm behavior hasn't changed. Treat AI as a "colleague who gives suggestions," not an "authority to trust unconditionally."

------

6. Summary6. Summary

Looking back, we've gone from identifying problems to solving them, building a complete code quality improvement system:Looking back, we've gone from identifying problems to solving them, building a complete code quality improvement system:

  1. Identify: Learn to smell code smells, know where improvements are neededIdentify: Learn to smell code smells, know where improvements are needed
  2. Refactor: Master safe refactoring techniques, improve in small steps under test protectionRefactor: Master safe refactoring techniques, improve in small steps under test protection
  3. Collaborate: Use code review to let the team collectively safeguard code qualityCollaborate: Use code review to let the team collectively safeguard code quality
  4. Measure: Track code health with objective metricsMeasure: Track code health with objective metrics
  5. ๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

    Code quality is not a one-time effort but a continuous habit. It's like keeping a room tidy โ€” you don't wait until it's a mess to do a deep clean; you tidy up a little every day. The Boy Scout Rule says it well: leave the code cleaner than you found it.Code quality is not a one-time effort but a continuous habit. It's like keeping a room tidy โ€” you don't wait until it's a mess to do a deep clean; you tidy up a little every day. The Boy Scout Rule says it well: leave the code cleaner than you found it.

    ------

    Further ReadingFurther Reading

    • Classic Book: Martin Fowler's Refactoring: Improving the Design of Existing Code is the bible of this field.Classic Book: Martin Fowler's Refactoring: Improving the Design of Existing Code is the bible of this field.
    • Clean Code: Robert C. Martin's Clean Code provides many practical coding principles.Clean Code: Robert C. Martin's Clean Code provides many practical coding principles.
    • Practical Tools: Try configuring ESLint + Prettier + Husky in your project to experience automated code quality assurance.Practical Tools: Try configuring ESLint + Prettier + Husky in your project to experience automated code quality assurance.
    • Code Review: Google's Code Review guidelines are the industry gold standard and worth studying.Code Review: Google's Code Review guidelines are the industry gold standard and worth studying.