12 Refactoring Rules of Thumb

12 Refactoring Rules of Thumb

It’s easy to refactor messy code once you follow a few simple guidelines. Are you curious about why these heuristics are useful? Then read the additional information included after the list.

I’ll start by providing you exactly what you came for: the rules of thumb.

Are you curious about why these heuristics are useful? Then read the additional information included after the list.


12 Heuristics for Beginners to Become Refactoring Experts

Let’s dive headfirst into the matter. This list is a mix of heuristics I always have in the back of my mind when I venture into a refactoring session.

  1. Is your class’ list of parameters starting to grow too big? Then it’s likely doing too much. Its responsibilities are unclear and perhaps painful to test and debug. It’s a prime candidate for refactoring.
  2. Do you have methods inside classes that only use one of the class dependencies? You better pull that method into a class of its own — even if that class will only consist of a single method.
  3. Is your method doing two different things depending on a boolean parameter value? Why not just create two different methods with clear responsibilities?
  4. Is your method branching over a value? Say, for instance, you’re checking the type of an object and perform different operations based on its type. This is a great time to turn your if-else or switch into a dictionary or map.
  5. Do you use if-else or switches extensively throughout your code? Try using polymorphism instead and apply battle-tested design patterns like Strategy, Mediator, etc.
  6. Does your class constructor or method take a “magic” number or string? That is an arbitrary value representing some intrinsic business value. Replace the magic with enums.

computer-science clean-code programming software-development software-engineering visual studio code

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Software Developer vs Software Engineer — Differences: Bogus or Real?

In this article, see if there are any differences between software developers and software engineers. What you’re about to read mostly revolves around my personal thoughts, deductions, and offbeat imagination. If you have different sentiments, add them in the comment section, and let’s dispute! So, today’s topic…

Offshore Software Development - Best Practices

To make the most out of the benefits of offshore software development, you should understand the crucial factors that affect offshore development.

Interested in Learning to Program? 13 Reasons to Start Now

Software development is something that is gaining popularity at lightning speed with the development of technology. The demand for regular developers is high compared to most other mainstream professions. But, what are the other reasons for learning to code?

Developer or Engineer? Does It Make a Difference?

To summarise the main differences between the software developer and engineer: A developer executes. ... So the software developer is mainly focused on developing code that is a part of software development cycle. An engineer designs and plans applying the principles of engineering to software development.

Development Containers in Education with Visual Studio Code

Development Containers in Education with Visual Studio Code