Seven Easy Data Validation Checks

If you work with data, you know how crucial it is to validate it before modeling. As critical as it is, data validation is often overlooked during a project as it’s often perceived as less…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Understanding Code Refactoring

Hi All ,

Today we will look the overview of Refactoring and what is the scope of activities that constitutes refactoring . We will also take a look at what activities are not part of refactoring process.

What is Refactoring : Refactoring is the process of modifying the internal structure of code without changing its external behavior. It is a technique that helps developers to improve the design of existing code and make it easier to understand and maintain. It involves reorganizing and restructuring the code without altering its functionality.

Sometimes a programmer may have come to you and explained that they don’t like the design of something and that “we’re gonna need to do a whole bunch of refactoring” to make it right. Oh Oh. This doesn’t sound good. And it doesn’t sound like refactoring either….

Refactoring, as originally defined by Martin Fowler and Kent Beck, is

A change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior… It is a disciplined way to clean up code that minimizes the chances of introducing bugs.

Refactoring is done to fill in short-cuts, eliminate duplication and dead code, and to make the design and logic clear. To make better and clearer use of the programming language. To take advantage of information that you have now but that the programmer didn’t have then — or that they didn’t take advantage of then. Always to simplify the code and to make it easier to understand. Always to make it easier and safer to change in the future.

Fixing any bugs that you find along the way is not refactoring. Optimization is not refactoring. Tightening up error handling and adding defensive code is not refactoring. Making the code more testable is not refactoring — although this may happen as the result of refactoring. All of these are good things to do. But they aren’t refactoring.

Refactoring is simple. Protect yourself from making mistakes by first writing tests where you can. Make structural changes to the code in small, independent and safe steps, and test the code after each of these steps to ensure that you haven’t changed the behavior — it still works the same, just looks different. Refactoring patterns and refactoring tools in modern IDEs make refactoring easy, safe and cheap.

Refactoring isn’t and end in itself

Refactoring is supposed to be a practice that supports making changes to code. You refactor code before making changes, so that you can confirm your understanding of the code and make it easier and safer to put your change in. Regression test your refactoring work. Then make your fix or changes. Test again. And afterwards maybe refactor some more of the code to make the intent of the changes clearer. And test everything again. Refactor, then change. Or change, then refactor.

You don’t decide to refactor, you refactor because you want to do something else, and refactoring helps you do that other thing.

The scope of your refactoring work should be driven by the change or fix that you need to make — what do you need to do to make the change safer and cleaner? In other words: Don’t refactor for the sake of refactoring. Don’t refactor code that you aren’t changing or preparing to change.

Scratch Refactoring to Understand

Don’t bother reviewing and testing all of these changes. The point is to move fast — this is a quick and dirty prototype to give you a view into the code and how it works. Learn from it and throw it away.

Scratch refactoring also lets you test out different refactoring approaches and learn more about refactoring techniques. Michael Feathers recommends that you keep notes during this on anything that wasn’t obvious or that was especially useful, so that you can come back and do a proper job later — in small, disciplined steps, with tests.

What about “Large Scale” Refactoring?

You can get a big return in understandability and maintainability from making simple and obvious refactoring changes: eliminating duplication, changing variable and method names to be more meaningful, extracting methods to make code easier to understand and more reusable, simplifying conditional logic, replacing a magic number with a named constant, moving common code together.

There is a big difference between minor, inline refactoring like this, and more fundamental design restructuring — what Martin Fowler refers to as “Big Refactoring”. Big, expensive changes that carry a lot of technical risk. This isn’t cleaning up code and improving the design while you are working: this is fundamental redesign.

In the meantime you have to keep working with the old code and new code together, making the code harder to follow and harder to change, more brittle and buggy — the opposite of what refactoring is supposed to achieve. Sometimes this can go on forever — the transition work never gets completed because most of the benefits are realized early, or because the consultant who came up with the idea left to go on to something else, or the budget got cut, and you’re stuck maintaining a Frankensystem.

This is Refactoring — That Isn’t

Mixing this kind of heavy project work up with the discipline of refactoring-as-you-go is wrong. They are fundamentally different kinds of work, with very different costs and risks. It muddies up what people think refactoring is, and how refactoring should be done.

Refactoring can and should be folded in to how you write and maintain code — a part of the everyday discipline of development, like writing tests and reviewing code. It should be done quietly, continuously and implicitly. It becomes part of the cost of doing work, folded in to estimates and risk assessments. Done properly, it doesn’t need to be explained or justified.

Refactoring that takes a few minutes or an hour or two as part of a change is just part of the job. Refactoring that can take several days or longer is not refactoring; it is rewriting or redesigning. If you have to set aside explicit blocks of time (or an entire sprint!) to refactor code, if you have to get permission or make a business case for code cleanup, then you aren’t refactoring — even if you are using refactoring techniques and tools, you’re doing something else.

Sometimes redesigning and rewriting code is the right thing to do. But be honest and clear. Don’t hide this under the name of refactoring.

Technical debt is the accumulation of technical problems and shortcuts that a software system incurs over time. These problems can include poor design, inadequate testing, and outdated technologies, among others. Technical debt can make it difficult to maintain and evolve a software system, leading to increased costs and reduced quality.

Refactoring can be an effective way to address technical debt by improving the internal structure of the code and making it easier to understand and maintain. By refactoring the code, developers can identify and fix design problems, eliminate unnecessary complexity, and improve the overall quality of the code.

However, it is important to note that refactoring alone may not be sufficient to completely eliminate technical debt. In some cases, it may be necessary to implement more significant changes or even rewrite parts of the code to fully address the technical debt.

It is also important to proactively manage technical debt by regularly reviewing and improving the design and quality of the code. This can help to prevent technical debt from accumulating in the first place and can make it easier to maintain and evolve the software system over time.

There are several benefits to refactoring code. It can help to improve the readability and maintainability of the code, making it easier for other developers to understand and work with. It can also help to identify and fix issues in the code, such as bugs, performance problems, and security vulnerabilities.

However, refactoring is not the same as debugging or fixing bugs. It is a separate process that is focused on improving the overall design of the code, rather than fixing specific issues.

It is also important to note that refactoring should not be done haphazardly. It is important to have a clear plan and to thoroughly test the code after refactoring to ensure that the changes have not introduced any new problems or broken any existing functionality.

There are several techniques that can be used as part of a refactoring process to address technical debt. Some common techniques include:

It is important to carefully plan and test any refactoring efforts to ensure that the changes do not introduce new problems or break any existing functionality. Properly managing technical debt requires a combination of refactoring and proactive code management to keep the system maintainable and up-to-date.

Here are a few additional considerations when it comes to refactoring and technical debt:

By following these best practices, developers can effectively use refactoring to address technical debt and improve the maintainability and performance of a software system.

We hope you liked this post on Refactoring process and details of its benefits and scope for Application Developers .

Happy Learning … Happy Coding …..

Other Interesting Articles:

AWS Learning : Journey towards Limitless Opportunities in Cloud .

No-cost ways to learn AWS Cloud over the holidays

Understanding 𝗖𝗢𝗥𝗦-𝗖𝗿𝗼𝘀𝘀-𝗢𝗿𝗶𝗴𝗶𝗻 𝗥𝗲𝘀𝗼𝘂𝗿𝗰𝗲 𝗦𝗵𝗮𝗿𝗶𝗻𝗴

Linux Commands for Cloud Learning

Java Programming Principles : Law of Demeter

Java : Understanding The Golden Ration Phi

Add a comment

Related posts:

INSPIRATION

Inspiration is a feeling of enthusiasm you get from someone or something, that gives you new and creative ideas. “The process of being mentally stimulated to do or feel something, especially to do…

Baby Water Mat Inflatable Cushion Infant Toddler Water Play Mat for Children Early Education Developing Baby Toy Summer Toys

Baby Water Mat Inflatable Cushion Infant Toddler Water Play Mat for Children Early Education…. Baby Water Mat Inflatable Cushion Infant Toddler Water Play Mat for Children Early Education Developing Baby Toy Summer Toys.

Fall Into These Glitter Sweet Slumber Shrugs!

When it comes to fashion, glitter shrugs are having a major moment. From celebrities to street-style stars, everyone is wearing them. Not only are they fashionable, but they are also perfect for…