0% found this document useful (0 votes)
9 views9 pages

Refactoring and Clean Code Essentials

The document discusses the concept of refactoring in software development, emphasizing that it involves changing the internal structure of a system without altering its external behavior. It highlights the importance of clean code, its characteristics, and the benefits of refactoring, such as improved maintainability and bug detection. Additionally, it distinguishes between refactoring and rewriting, stating that refactoring is suitable for code that works mostly correctly, while rewriting is necessary when code fails to function.

Uploaded by

saleemayman7848
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

Refactoring and Clean Code Essentials

The document discusses the concept of refactoring in software development, emphasizing that it involves changing the internal structure of a system without altering its external behavior. It highlights the importance of clean code, its characteristics, and the benefits of refactoring, such as improved maintainability and bug detection. Additionally, it distinguishes between refactoring and rewriting, stating that refactoring is suitable for code that works mostly correctly, while rewriting is necessary when code fails to function.

Uploaded by

saleemayman7848
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

✅ WEEK 9 – MCQs (with Answers)

1. Refactoring is the process of changing a software system such that:


A. External behavior changes
B. Internal structure changes
C. Both internal and external behavior change
D. Only functionality is added
Answer: B

2. During refactoring, the external behavior of the system:


A. Is improved
B. Is removed
C. Does not change
D. Is redesigned
Answer: C

3. Refactoring is sometimes called:


A. Software maintenance
B. Improving the design after it has been written
C. Code rewriting
D. Debugging
Answer: B

4. The main goal of refactoring is to:


A. Add new features
B. Improve performance
C. Make code easier to maintain
D. Remove all bugs
Answer: C

5. Refactoring involves:
A. Adding new functionality
B. Editing and cleaning up code
C. Removing tests
D. Changing requirements
Answer: B

6. Clean code is:


A. Complex and optimized
B. Obvious for other programmers
C. Full of comments
D. Written quickly
Answer: B

7. Clean code does NOT contain:


A. Comments
B. Classes
C. Duplication
D. Functions
Answer: C

8. Clean code contains:


A. Many classes
B. Minimal number of classes and moving parts
C. No structure
D. Extra code
Answer: B

9. Which is a characteristic of clean code?


A. Fails tests
B. Is difficult to maintain
C. Passes all tests
D. Has duplicated logic
Answer: C

10. Clean code is easier and cheaper to:


A. Write
B. Debug
C. Maintain
D. Compile
Answer: C

11. Clean code becomes unclean when:


A. Tests pass
B. Requirements never change
C. Requirements change over time
D. Documentation improves
Answer: C

12. Technical debt refers to:


A. Money spent on software
B. Coding done for optimization
C. Coding you must do tomorrow due to shortcuts today
D. Bug fixing
Answer: C

13. Refactoring is NOT just cleaning up code because:


A. It changes functionality
B. It provides controlled techniques
C. It removes documentation
D. It adds features
Answer: B

14. Why is refactoring useful?


A. It avoids writing code
B. It accepts that first design is perfect
C. It helps design evolve as requirements change
D. It replaces testing
Answer: C

15. Refactoring improves software design by:


A. Increasing complexity
B. Causing design decay
C. Preventing design decay
D. Removing structure
Answer: C

16. One benefit of refactoring is that it:


A. Makes software harder to understand
B. Helps find bugs
C. Increases technical debt
D. Reduces testing
Answer: B

17. Refactoring promotes:


A. Shallow understanding of code
B. Deep understanding of code
C. No understanding of code
D. Only testing
Answer: B
18. Refactoring helps programmers work faster because:
A. It removes all code
B. A good design enables progress
C. It skips design
D. It avoids maintenance
Answer: B

19. Refactoring should be done when code:


A. Does not work at all
B. Works mostly correctly
C. Has no tests

D. Is new
Answer: B

20. If current code does not work, developers should:


A. Refactor
B. Optimize
C. Rewrite
D. Ignore it
Answer: C

✅ SHORT QUESTIONS WITH ANSWERS (Week 9)

Q1. What is refactoring?

Answer:
Refactoring is the process of changing a software system such that the external
behavior of the system does not change, while the internal structure of the system
is improved.

Q2. Why is refactoring sometimes called “improving the design after it has
been written”?

Answer:
Because refactoring improves the internal design of the code after it has already
been implemented, without changing its functionality.
Q3. What is the main goal of refactoring?

Answer:
The main goal of refactoring is to make code easier to maintain in the future.

Q4. Does refactoring add new functionality?

Answer:
No, the goal of refactoring is not to add new functionality.

Q5. What is meant by clean code?

Answer:
Clean code is code that is obvious for other programmers and easy to understand.

Q6. Write any two characteristics of clean code.

Answer:

 Clean code doesn’t contain duplication.


 Clean code passes all tests.

Q7. Why is clean code easier and cheaper to maintain?

Answer:
Because clean code has a better structure, minimal duplication, and fewer moving
parts, which reduces maintenance effort.

Q8. At what point does clean code become unclean?

Answer:
Clean code becomes unclean when requirements change over time and time or
money constraints force developers to take shortcuts.

Q9. What is technical debt?

Answer:
Technical debt is the coding you must do tomorrow because you took a shortcut in
order to deliver the software today.
Q10. Is refactoring just cleaning up code?

Answer:
No, refactoring goes further by providing a technique for cleaning up code in a
more efficient and controlled manner.

Q11. Why is refactoring useful?

Answer:
Refactoring is useful because it acknowledges that it is difficult to get a design
right the first time and allows the design to evolve as requirements change.

Q12. How does refactoring improve software design?

Answer:
Refactoring improves software design by preventing design decay and by making
small, incremental improvements.

Q13. Write any two benefits of refactoring.

Answer:

 Refactoring makes software easier to understand.


 Refactoring helps find bugs.

Q14. How does refactoring help in finding bugs?

Answer:
Refactoring promotes a deep understanding of the code, which helps programmers
find and anticipate bugs.

Q15. Why does refactoring help programmers work faster?

Answer:
Because a good design enables progress and allows programmers to implement
changes more efficiently.
Q16. When should refactoring not be done?

Answer:
Refactoring should not be done when the current code does not work correctly; in
that case, rewriting is preferred.

Q17. What is the difference between refactoring and rewriting?

Answer:
Refactoring improves internal structure without changing behavior, while rewriting
replaces the existing code entirely

✅ LONG QUESTIONS WITH ANSWERS (Week 9)

Q1. Define refactoring and explain its goals.

Answer:
Refactoring is the process of changing a software system such that the external
behavior of the system does not change while the internal structure is improved. It
is often called “improving the design after it has been written.” The goals of
refactoring are to clean up existing code, improve design quality, and make the
code easier and cheaper to maintain in the future. Refactoring does not aim to add
new functionality.

Q2. Explain the concept of clean code and its characteristics.

Answer:
Clean code is code that is obvious and easy to understand for other programmers. It
does not contain duplication, contains a minimal number of classes and other
moving parts, passes all tests, and is easier and cheaper to maintain. Clean code
improves readability, reduces errors, and supports long-term maintenance.

Q3. Discuss how clean code becomes unclean and explain technical debt.

Answer:
Clean code becomes unclean when requirements change over time, making the
design harder to update, and when time or money constraints force developers to
take shortcuts. These shortcuts lead to technical debt, which is the coding work that
must be done later because compromises were made to deliver software quickly.
Technical debt increases maintenance cost and reduces code quality.
Q4. Why is refactoring useful in software development?

Answer:
Refactoring is useful because it recognizes that it is difficult to create a perfect
design in the first attempt. As requirements change, the design must also evolve.
Refactoring provides techniques for evolving the design in small, incremental
steps. It improves the software design and prevents design decay caused by
continuous changes.

Q5. Explain the benefits of refactoring.

Answer:
Refactoring makes software easier to understand by improving structure and
eliminating duplicated code. It helps programmers find bugs by promoting a deep
understanding of the code. Refactoring also helps programmers work faster
because a good design enables smooth progress and easier modifications.

Q6. Explain refactoring with a simple example.

Answer:
In the given example, duplicated code for sending data is removed by restructuring
the logic. First, conditional duplication is reduced, and then a common factor is
introduced. This improves readability and structure while keeping the external
behavior unchanged. The final version is simpler and more maintainable.

Q7. Compare refactoring and rewriting.

Answer:
Refactoring should be applied when the existing code works mostly correctly and
only needs structural improvement. If the code does not work correctly, refactoring
should not be performed; instead, the code should be rewritten. Refactoring
preserves behavior, while rewriting replaces the system.

Q8. Explain why refactoring improves long-term software maintenance.

Answer:
Refactoring improves long-term maintenance by keeping the design clean and
preventing design decay. By making small incremental improvements, refactoring
ensures that the software remains understandable, adaptable, and easier to modify
as requirements evolve.

You might also like