0% found this document useful (0 votes)
29 views

Week 11 SREE Refactoring + Design Patterns

This document provides an introduction and overview of code refactoring and design patterns. It defines refactoring as improving the internal structure of code without changing its external behavior. Examples of refactoring operations like "Extract Method" and "Introduce Parameter" are given. Common design patterns like Factory, Strategy, and Adapter are also explained with definitions and implementations. The key points are that refactoring improves code quality without affecting functionality, and design patterns provide reusable solutions to common programming problems.

Uploaded by

Hunain
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Week 11 SREE Refactoring + Design Patterns

This document provides an introduction and overview of code refactoring and design patterns. It defines refactoring as improving the internal structure of code without changing its external behavior. Examples of refactoring operations like "Extract Method" and "Introduce Parameter" are given. Common design patterns like Factory, Strategy, and Adapter are also explained with definitions and implementations. The key points are that refactoring improves code quality without affecting functionality, and design patterns provide reusable solutions to common programming problems.

Uploaded by

Hunain
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

Week-11: Refactoring Introduction

Course Code: SESE4183


Refactoring
• Definition. Refactoring consists of improving the internal structure of
an existing program's source code, while preserving its external
behavior.
• Examples of refactoring operations include actions like "Extract Method"
and "Introduce Parameter.”
• "Extract Method" involves taking a section of code and moving it into a new
method, making the code more modular and easier to understand.
• "Introduce Parameter" refers to adding a parameter to a method or function,
allowing more flexibility and reusability.
Refactoring
• Refactoring is the process of changing a software system in
such a way that it does not alter the function of the code yet
improves its internal structure.
• When carried out manually, refactoring is applied directly to
the source code and is generally a labor-intensive, ad hoc, and
potentially error-prone (tending to make or cause errors)
process.
Refactoring
• Refactoring or Code Refactoring is defined as systematic
process of improving existing computer code, without adding
new functionality or changing external behavior of the code.
• It is intended to change the implementation, definition, structure
of code without changing functionality of software.
Where is refactoring used?

• Code refactoring is a process used in the DevOps software


development approach that involves editing and cleaning up
previously written software code without changing the function
of the code at all.
• The basic purpose of code refactoring is to make the code
more efficient and maintainable.
Does refactoring improve performance?

• Refactoring restructures your code into a more simplified or


efficient form in a disciplined way.
• Refactoring software code improves its internal structure
without changing external functionality for more efficient
execution, faster download speed, and easier updates.
Can refactoring help in agile?

Code Refactoring is a practice where you alter the internal


structure of software without changing its behavior. ... As the
software evolves, refactoring lets you modify it constantly in
response to inevitable change.
When should refactoring be done in agile?

• If the business needs a new feature, then refactoring


should only be done on those parts of the system that are
required to enable that feature.
• In other words, don't refactor the whole user interface,
just refactor the parts that relate to the specific business
request.
Usefulness of Refactoring
• The basic purpose of code refactoring is to make the code
more efficient and maintainable.
• This is key in reducing technical cost since it's much better to
clean up the code now than pay for costly errors later.
• Code refactoring, which improves readability, makes the QA
and debugging process go much more smoothly.
Martin Fowler, the father of the code smell notion,
defines refactoring as “the process of changing a software
system to improve its internal structure without altering its
external behavior”.
Refactoring is an iterative process that works by transforming
functions and rethinking algorithms.
What would not be considered 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.
What should you look for when
refactoring code?
1. Get rid of switch statements.
2. Make your conditionals descriptive.
3. Use guard clauses (simplify the logic) to avoid nested if
statements.
4. Avoid code duplication.
5. Functions should only do one thing.
Code Refactoring Techniques in Software Engineering

• Improving or updating the code without changing the software’s


functionality or external behavior of the application is known as code
refactoring.
• It reduces the technical cost and makes the code more efficient and
maintainable.
• If you don’t pay attention to the code refactoring process earlier, you
will pay for errors in your code later.
• So don’t ignore cleaning up the code.
Code Refactoring Techniques
• In a software development process, different developers have different code
writing styles.
• They make changes, maintain the code, extend the code, and most of the
time they leave the code without continuous refactoring.
• Un-refactored code tends to code rot: a lot of confusion and clutter in code
such as duplicate code, unhealthy dependencies between classes or
packages, bad allocation of class responsibilities, too many responsibilities
per method or class, etc.
• To avoid all these issues continuous refactoring is important.
Refactoring Key Points
1. You need to perform code refactoring in small steps. Make tiny
changes in your program, each of the small changes makes your code
slightly better and leaves the application in a working state.
2. Do not create any new features or functionality during the refactoring
process. You should refactor the code before adding any updates or
new features in your existing code.
3. Refactoring process can affect the testing outcomes so it’s good to get
your QA and testing team involved in the refactoring process.
4. You need to accept that you won’t be fully satisfied with your code.
Your refactored code will be outdated in near future and you’ll have to
refactor it again.
Methods
1. Red-Green Refactoring
2. Refactoring By Abstraction
3. Composing Method
4. Simplifying Methods
5. Moving Features Between Objects
6. Preparatory Refactoring
7. User Interface Refactoring
Desig
n
Patter
ns
Design Pattern
• Definition. A design pattern in software engineering is a general,
reusable solution to a commonly occurring problem within a given
context in software design.
• Design patterns represent the best practices used by experienced
object-oriented software developers.
• Design patterns are solutions to general problems that software
developers faced during software development.
• It's not a finished design that can be directly transformed into
code; rather, it's a template or guideline for how to solve a problem
that can be used in many different situations.
Factory Pattern
• Factory pattern is one of the most used design patterns in Java.
• This type of design pattern comes under creational pattern as
this pattern provides one of the best ways to create an object.
• In Factory pattern, we create object without exposing the
creation logic to the client and refer to newly created object
using a common interface.
Factory Pattern
Implementation
• We're going to create a Shape interface and concrete
classes (CIRCLE / RECTANGLE / SQUARE)
implementing the Shape interface.
• A factory class ShapeFactory is defined as a next step.
• FactoryPatternDemo, our demo class will
use ShapeFactory to get a Shape object.
• It will pass information (CIRCLE / RECTANGLE / SQUARE)
to ShapeFactory to get the type of object it needs.
Factory Pattern
Factory Pattern
Factory Pattern
Factory Pattern
Factory Pattern
Factory Pattern
Strategy Pattern
• In Strategy pattern, a class behavior or its algorithm can be
changed at run time. This type of design pattern comes under
behavior pattern.
• In Strategy pattern,
• we create objects which represent various strategies and
• a context object whose behavior varies as per its strategy object.
• The strategy object changes the executing algorithm of the context
object.
Strategy Pattern
• Implementation
• We are going to create a Strategy interface defining an action
and concrete strategy classes implementing
the Strategy interface.
• Context is a class which uses a Strategy.
• StrategyPatternDemo, our demo class, will use Context and
strategy objects to demonstrate change in Context behavior
based on strategy it deploys or uses.
Strategy Pattern
Strategy Pattern
Strategy Pattern
Strategy Pattern
Adapter Pattern
• Adapter pattern works as a bridge between two incompatible interfaces.
• This type of design pattern comes under structural pattern as this pattern
combines the capability of two independent interfaces.
• This pattern involves a single class which is responsible to join functionalities
of independent or incompatible interfaces.
• A real life example could be a case of card reader which acts as an adapter
between memory card and a laptop. You plugin the memory card into card
reader and card reader into the laptop so that memory card can be read via
laptop.
• We are demonstrating use of Adapter pattern via following example in which
an audio player device can play mp3 files only and wants to use an advanced
audio player capable of playing vlc and mp4 files.
Adapter Pattern
Implementation
• We have a MediaPlayer interface and a concrete class AudioPlayer implementing
the MediaPlayer interface. AudioPlayer can play mp3 format audio files by default.
• We are having another interface AdvancedMediaPlayer and concrete classes VlcPlayer
and Mp4Player implementing the AdvancedMediaPlayer interface. These classes can play
vlc and mp4 format files.
• We want to make AudioPlayer to play other formats as well. To attain this, we have
created an adapter class MediaAdapter which implements the MediaPlayer interface and
uses AdvancedMediaPlayer objects to play the required format.
• AudioPlayer uses the adapter class MediaAdapter passing it the desired audio type without
knowing the actual class which can play the desired format.
• AdapterPatternDemo, our demo class will use AudioPlayer class to play various formats.
Adapter Pattern
Adapter Pattern
Adapter Pattern
Adapter Pattern
Adapter Pattern

You might also like