Lecture 11 - Refactoring Flashcards

(21 cards)

1
Q

What is refactoring?

A

Refactoring is the process of modifying software so that:
- The external behaviour of the code is not modified
- The internal structure is improved to make it easier to understand and cheaper to modify without changing its observable behavior

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does refactoring not do?

A

Refactoring does not fix bugs, but it may help find bugs by scrutinizing code. It may also reduce the further introduction of bugs by cleaning-up code
- Refactoring does not add new functionality to the system, but mit will ease the further adding of new functionality.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why would we want to “improve the design after it has been written”?

A

As real software systems are subject to requirements that change over their lifetimes
- The code is modified. The integrity of the structure according to the original design fades away (remember “design entropy”)
REFER TO SLIDES FOR EXAMPLE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What happens when new code is introduce before refactoring is done?

A

With more new codes added to the system, problems (such as “code smells” or “anti-patterns”) might be introduced unintentionally that can negatively impact the quality of the software
- Sluggish performance
- Hard to remove code (because they are entangled/tightly coupled)
- Hard to add new code (because of too much hard-coding)

A common anti-pattern seen in legacy systems is the “Big Ball of
Mud

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Can refactoring be done during development?

A

Yes, the same way it can be done for a finished piece of software it can be done during initial development as well

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to do Refactoring?

A

Each refactoring is implemented as a small behavior-preserving transformation.
Behavior-preservation is achieved through pre- and post-transformation testing.
Refactoring process:
- test
- refactor
- test

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the understanding of “Two hats”?

A

When using refactoring during development, you divide your time between:
- Adding functionality
- Refactoring
When adding functionality, you should not be changing existing code
- You write tests for the new functionality
- You add the new functionality
- You measure progress by getting the new tests to work
—-
THE IDEA IS:
Frequently you start off trying to add new functionality
► You realize that this would be a lot easier if the code had a different structure
► You swap hats
► You refactor until the code has the structure you want
► You swap hats again, and add the new functionality
► Now you think the new functionality is hard to understand, so you swap hats and…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why refactor?

A

Improves design
- Without refactoring, the design of the software will decay
- Changes are often made to meet short-term goals – Often, people who maintain the software are not the original software developers who write the code!
- The maintainers might not be aware of the intended overall design
- It becomes harder to understand the design from the code itself

Every time this happens, the effects add up:
- If it is hard to see the design in the code, it will be hard to preserve it
- This will cause the design to decay more rapidly
- Eventually the original design may fade away completely

Helps you find bugs
- Helping you focus on the code, you might figure out bugs/issues

It makes you program faster
- Better design, improved readability, fewer bugs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is an important goal of refactoring?

A

To remove duplicate code:
This won’t make the system any faster, but it will make it easier to maintain
The more code there is, the more there is to understand. . . or misunderstand

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What else can refactoring be implemented into

A

Other than code, it can be implemented into software architecture:
- Architectural refactoring improve the design of an existing software, which changes the structure but not the functionality.
- The goal is to improve a set of quality attributes such as performance, scalability, extensibility, testability, and robustness.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the refactoring techniques?

A

Inline Class
Extract Class
Extract Subclass
Replace Delegation with Inheritance
Replace Inheritance with Delegation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is Inline Class?

A

Inline class is used when a class does almost nothing and isn’t responsible for anything, and no additional responsibilities are planned for it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

When and why use Inline Class?

A

Solution: Move all features from the class to another one
Why: Eliminating needless classes frees up operating memory on the computer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is Extract Class?

A

Extract Class is used when one class does the work of two.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

When and why use Extract Class

A

Solution: Instead, create a new class and place the fields and methods responsible for the relevant functionality in it.
Why: Classes always start out clear and easy to understand. They do their job and mind their own business as it were, without butting into the work of other classes. But as the program expands, a method is added and then a field… and eventually, some classes are performing more responsibilities than ever envisioned.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is Extract Subclass?

A

Extract Subclass is used when a class has features that are used only in certain cases.

17
Q

When and why use Extract Subclass?

A

Solution: Create a subclass and use it in these cases.
Why: Your main class has methods and fields for implementing a certain rare use case for the class. While the case is rare, the class is responsible for it and it would be wrong to move all the associated fields and methods to an entirely separate class. But they could be moved to a subclass.

18
Q

What is Replace delegation with inheritance?

A

Replace delegation with inheritance is used when a class contains many simple methods that delegate to all methods of another class.

19
Q

When and why to use Replace delegation with inheritance?

A

Solution: Make the class a delegate inheritance, which makes the delegating methods unnecessary.
Why: Delegation is a more flexible approach than inheritance, since it allows changing how delegation is implemented and placing other classes there as well. Nonetheless, delegation stops being beneficial if you delegate actions to only one class and all of its public methods. In such a case, if you replace delegation with inheritance, you cleanse the class of a large number of delegating methods and spare yourself from needing to create them for each new delegate class method. Reduces code length. All these delegating methods are no longer necessary.

20
Q

What is Replace Delegation with Inheritance?

A

Replace Inheritance with Delegation is used when you have a subclass that uses only a portion of the methods of its superclass

21
Q

When and why use Replace Delegation with Inheritance?

A

Solution: Create a field and put a superclass object in it, delegate methods to the superclass object, and get rid of inheritance.
Why: Opposite to Replace Delegation with Inheritance, sometimes you want your class to be more flexible and reduce coupling.