Refactoring in software engineering


















Is that renaming worth the effort? Good code should communicate what it is doing clearly, and variable names are key to clear code. Never be afraid to change the names to things to improve clarity. With good find and replace tools, it is usually not difficult. Strong typing and testing will highlight anything you miss. Remember any fool can write code that a computer can understand, good programmers write code that humans can understand. As I look at amountOf, I can see that it uses information from the rental, but does not use information from the customer.

This method is thus on the wrong object, it should be moved to the rental. To move a method you first copy the code over to rental, adjust it to fit in its new home and compile. The next step is to find every reference to the old method, and adjusting the reference to use the new method. In this case this step is easy as we just created the method and it is in only one place. In general, however, you need to do a find across all the classes that might be using that method.

When I've made the change the next thing is to remove the old method. The compiler should then tell me if I missed anything. There is certainly some more I would like to do to Rental. The next thing that strikes me is that thisAmount is now pretty redundant. It is set to the result of each. Thus I can eliminate thisAmount by replacing a temp with a query. I like to get rid of temporary variables like thus as much as possible.

Temps are often a problem in that they cause a lot of parameters to get passed around when they don't need to. You can easily lose track of what they are there for. They are particularly insidious in long methods. Of course there is a small performance price to pay, here the charge is now calculated twice. But it is easy to optimize that in the rental class, and you can optimize much more effectively when the code is properly refactored.

The next step is to do a similar thing for the frequent renter points. Again the rules vary with the tape, although there is less variation than with the charging. But it seems reasonable to put the responsibility on the rental. First we need to extract a method from the frequent renter points part of the code highlighted below. Again we look at the use of locally scoped variables.

Again it uses each , which can be passed in as a parameter. The other temp used is frequentRenterPoints. In this case frequentRenterPoints does have a value beforehand. The body of the extracted method doesn't read the value, however, so we don't need to pass it in as a parameter as long as we use an appending assignment. I did the extraction, compiled and tested, and then did a move.

With refactoring small steps are the best, that way less tends to go wrong. As I suggested before, temporary variables can be a problem.

They are only useful within their own routine, and thus they encourage long complex routines. In this case we have two temporary variables, both of which are being used to get a total from the rentals attached to the customer. Both the ascii and html versions will require these totals.

I like to replace temps with queries. Queries are accessible to any method in the class, and thus encourage a cleaner design without long complex methods. I began by replacing totalAmount with a charge method on customer. After compiling and testing that refactoring, I then did the same for frequentRenterPoints.

It is worth stopping and thinking a bit about this refactoring. Most refactoring reduce the amount of code, but this one increases it. That's because Java requires a lot of statements to set up a summing loop. Even a simple summing loop with one line of code per element needs six lines of support around it.

As Java develops and builds up its ability to handle block closures in the style of Smalltalk, I expect that overhead to decrease, probably to the single line that such an expression would take in Smalltalk. The other concern with this refactoring lies in performance. The old code executed the while loop once, the new code executes it three times. If the while loop takes time, this might significantly impair performance.

Many programmers would not do this refactoring simply for this reason. Easy Normal Medium Hard Expert. Writing code in comment? Please use ide. Load Comments. What's New. Most popular in Software Engineering. More related articles in Software Engineering. We use cookies to ensure you have the best browsing experience on our website.

There are several best practices and recommendations regarding code refactoring. One of the smartest ways to approach it is to apply the Agile method and do it one step at a time, followed by testing.

This is why so many developers utilizing Agile methodology are big proponents of code refactoring. Breaking down the refactoring process into manageable chunks and performing timely testing before moving on to other updates always results in a higher quality application and a better overall development experience. Yes, it will take. Think about your overall goal. Do you just want to change the variable names to improve readability? Or do you want to do a full-on cleanup?

What are the best ways for you to optimize the code within a reasonable timeframe? The most important outcome of refactoring is that not only is the code cleaner but that it actually works. The last thing you want to do when refactoring is mess something up in the process and create bugs or problems that affect the functionality of the product.

This is why testing throughout the refactoring process is imperative. Changes in classification done during refactoring can cause old tests to fail. In addition, new tests may have to be created for outdated legacy software systems. Both in-depth and regression testing should be performed as part of a refactoring effort. This will ensure that the functionality of the solution was not affected in any way. Development teams using the Agile method for both programming and testing will most likely already be on the same page involving refactoring.

Accept the fact that you will never be percent satisfied. You have to start thinking about refactoring as an ongoing maintenance project. Just like you have to clean and organize your home throughout the week, you will need to clean and organize your code on many different occasions.

Automating some or all of the refactoring processes is becoming more and more popular with developers. There are many shortcuts and tools to make refactoring less painful. You should refactor the code before adding any updates or new features in your existing code. Most Common Code Refactoring Techniques There are many approaches and techniques to refactor the code. Red-Green Refactoring Red-Green is the most popular and widely used code refactoring technique in the Agile software development process.

Developers take initiative for the refactoring into the test-driven development cycle and it is performed into the three district steps. You stop and check what needs to be developed. Refactor: In the final and third steps, you focus on improving and enhancing your code keeping your test green. So basically this technique has two distinct parts: The first part involves writing code that adds a new function to your system and the second part is all about refactoring the code that does this function.

Refactoring By Abstraction This technique is mostly used by developers when there is a need to do a large amount of refactoring. Mainly we use this technique to reduce the redundancy duplication in our code. This involves class inheritances, hierarchy, creating new classes and interfaces, extraction, replacing inheritance with the delegation , and vice versa.

Pull-Up method: It pulls code parts into a superclass and helps in the elimination of code duplication. Push-Down method: It takes the code part from a superclass and moves it down into the subclasses. P ull up the constructor body, extract subclass, extract superclass, collapse hierarchy, form template method, extract interface, replace inheritance with the delegation, replace delegation with Inheritance, push down-field all these are the other examples. Basically, in this technique , we build the abstraction layer for those parts of the system that needs to be refactored and the counterpart that is eventually going to replace it.

Two common examples are given below… Encapsulated field: We force the code to access the field with getter and setter methods. Generalize type: We create more general types to allow code sharing, replace type-checking code with the state, replace conditional with polymorphism, etc.

Composing Method During the development phase of an application a lot of times we write long methods in our program. These l ong methods make your code extremely hard to understand and hard to change.



0コメント

  • 1000 / 1000