It's like magic! Three secrets to dramatically improve your code (for beginners)
Hello, this is John! This time, I will explain the technique of "refactoring" in an easy-to-understand way even for beginners. Refactoring is like magic, it dramatically improves the code you write!
What is refactoring?
Refactoring, simply put, isOrganizing the look and structure of your code to make it easier to read and maintain" However, the program's behavior itself will not change. It's like giving your room a thorough cleaning. You rearrange the furniture and throw away unnecessary items to make the room easier to use, but the function of the room (a place to sleep and eat) remains the same, right?
As mentioned in the original article, refactoring used to be a very difficult task. But now, thanks to useful tools (IDEs, software for writing programs), anyone can do it easily. It's like having a magic wand!
Tip #1: Extract Method
If I were asked to choose only one refactoring, I would choose "Extract Method" without hesitation! This isTechniques for breaking down long, complex code into smaller, more understandable partsIt's like dividing a long recipe into individual steps.
The reason method extraction is important is that it prevents your code from becoming a "big ball of mud" - ugly and hard to work with, just like long, complex code that's hard to understand and hard to fix.
The trick to extracting a method isEach method (a block of program) should be about 10 to 15 lines.And give each method a name that clearly explains what it does. Code with lots of small, well-named methods looks like a beautiful, tidy garden!
Tip #2: Rename Variable/Method/Class
In the world of programming, it is often said that "naming is the hardest thing." Many "names" appear in a program, such as variables (boxes for storing data), methods (blocks of processing), and classes (blueprints).Bad names make your code hard to read.Hm.
For example, suppose you have a variable named "RecNo". You can imagine that this is some kind of number, but you don't know what it is, right? However, if you name it "RecordNumber", it becomes obvious at a glance.
Don't hesitate to change the name! If you think "this name is a little hard to understand," you should change it immediately. With modern IDEs (programming software), you can change the name of the entire code automatically by changing it once, so it's very easy. Prioritize clarity even if the name is a little longer.
Trick #3: Extract Variable
When writing a program, it's easy to get in a hurry and write a complex formula in one line. For example,
If CalculateInterestRate(GatherAllInputs()) > 0.6 { ... }
Can you tell at a glance what this does? You'll probably have to think about it a bit to figure it out.
That's where variable extraction comes in! Rewrite this code as follows:
const AllTheInputs = GatherAllInputs(); const CustomerInterestRate = CalculateInterestRate(AllTheInputs); const CustomerInterestRateIsHighEnough = CustomerInterestRate > 0.6; If CustomerInterestRateIsHighEnough { ... }
So, isn't this a lot easier to read? By giving each variable a meaningful name, you can understand at a glance what the code does. It also makes debugging (the process of finding bugs in a program) easier.
Some people may think, "It's a pain to write so much," butWriting clear code is never a waste of timeRather, it is a very important investment that will benefit you and your team members in the future.
Summary: Refactoring is the key to great code!
This time, I have explained three secrets that can dramatically improve your code: "Extract Method", "Rename" and "Extract Variable". Using these techniques, your code should become cleaner and easier to use.
Modern IDEs (programming software) are designed to make refactoring easy, so make sure you use these tools to help you write the best code possible!
I myself have found writing code much more enjoyable since I started paying attention to refactoring. In particular, when the code becomes neater after extracting a method, it feels as refreshing as completing a puzzle! I encourage everyone to give refactoring a try.
This article is based on the following original articles and is summarized from the author's perspective:
The three refactorings every developer needs most