Giorgetti Alessandro
Ricci Gian Maria
Owner of SID s.r.l.
Blog: http://www.nablasoft.com/guardian
Email: guardian@nablasoft.com
MVP Team System
Blog: http://www.nablasoft.com/alkampfer [Eng]
http://blogs.ugidotnet.org/rgm [Ita]
Email: alkampfer@nablasoft.com
Refactoring Purposes
 Clean-up the code
 Improve readability
 Improve the ‘code maintenance’
 Implement a ‘Separation of Concerns’ in your
application
 Allow better testability
 Improve performances appling some ‘best practices’
Prerequisites to Refactoring
 Understand what the code does
 Understand what the code is supposed to do
(sometimes they do not match)
 Knowledge of what you are going to do (do not blindly
follow some practices or use tools without knowing
how the work and the impact they have).
You are altering (someone else’s) code, so always be
carefull and assure yourself to preserve the original
functionalities.
The Refactoring Pipeline
Refactoring can be a very complex operation, especially
depending on the legacy code you are working on.
Sometimes is wise to divide the task in multiple
iterations, and work on a single task a time. This way
you can test what you are doing and it’s easier to revert
back in case of errors.
Let’s see some common steps:
The Refactoring Pipeline
 Analyze the legacy code.
 Write down some objectives you want to accomplish
with refactoring (like: beautify code and improve
readability, separate business logic code from
presentation, improve performace, testability, etc…).
 Assign a priority to all these task and start an iteration
for any of them.
The Refactoring Pipeline
The Iteration Chain should be something like:
 Think what you are going to do (for example if you are
isolating business code from presentation code, define
a set of classes and interface and so on…).
 Write down some documentation about the changes
you are going to do.
 Implement your solution.
 Add some tests for the new component eventually
written.
 Verify that the whole system works.
Demo – Refactoring of an Image
Gallery
Our final goal will be:
- Structurate the application
- Less code bloating
- More classes with small methods in them
Demo - Refactoring an Image
Gallery
Defining the iterations:
 Step 0 - Analyze the code.
 Step 1 - Isolate the tasks, separate concerns, make the
methods smaller, improve readability and code
maintenace.
 Step 2- Define application layers and reduce couplings,
especially the ‘persistence coupling’; we end up with a
layered application usually: presentation, services,
repository layers.
 Step 3 – Apply an MVP/MVC/MVVM pattern and
move code to testable classes.
Demo - refactor to reach testability
 We start from a web form where all the logic is inside
the code behind
 We will move, step by step, to a custom and simple
implementation of MVC
 We add test for the logic of the page
 The goal is having a web page so simple that not a
simple if should remain in code behind.
Tips & Tricks
 Use a CDN service when possible to deliver library
services (AJAX, jQuery and so on)
(http://www.asp.net/ajax/cdn/)
 Consider using Javascript or jQuery for simple tasks to
avoid unneeded PostBacks
 Test…test…test…
Questions ?
We hope NOT, cause we are TIRED and we want to EAT
and DRINK!

Refactoring ASP.NET and beyond

  • 1.
  • 2.
    Owner of SIDs.r.l. Blog: http://www.nablasoft.com/guardian Email: guardian@nablasoft.com
  • 3.
    MVP Team System Blog:http://www.nablasoft.com/alkampfer [Eng] http://blogs.ugidotnet.org/rgm [Ita] Email: alkampfer@nablasoft.com
  • 4.
    Refactoring Purposes  Clean-upthe code  Improve readability  Improve the ‘code maintenance’  Implement a ‘Separation of Concerns’ in your application  Allow better testability  Improve performances appling some ‘best practices’
  • 5.
    Prerequisites to Refactoring Understand what the code does  Understand what the code is supposed to do (sometimes they do not match)  Knowledge of what you are going to do (do not blindly follow some practices or use tools without knowing how the work and the impact they have). You are altering (someone else’s) code, so always be carefull and assure yourself to preserve the original functionalities.
  • 6.
    The Refactoring Pipeline Refactoringcan be a very complex operation, especially depending on the legacy code you are working on. Sometimes is wise to divide the task in multiple iterations, and work on a single task a time. This way you can test what you are doing and it’s easier to revert back in case of errors. Let’s see some common steps:
  • 7.
    The Refactoring Pipeline Analyze the legacy code.  Write down some objectives you want to accomplish with refactoring (like: beautify code and improve readability, separate business logic code from presentation, improve performace, testability, etc…).  Assign a priority to all these task and start an iteration for any of them.
  • 8.
    The Refactoring Pipeline TheIteration Chain should be something like:  Think what you are going to do (for example if you are isolating business code from presentation code, define a set of classes and interface and so on…).  Write down some documentation about the changes you are going to do.  Implement your solution.  Add some tests for the new component eventually written.  Verify that the whole system works.
  • 9.
    Demo – Refactoringof an Image Gallery Our final goal will be: - Structurate the application - Less code bloating - More classes with small methods in them
  • 10.
    Demo - Refactoringan Image Gallery Defining the iterations:  Step 0 - Analyze the code.  Step 1 - Isolate the tasks, separate concerns, make the methods smaller, improve readability and code maintenace.  Step 2- Define application layers and reduce couplings, especially the ‘persistence coupling’; we end up with a layered application usually: presentation, services, repository layers.  Step 3 – Apply an MVP/MVC/MVVM pattern and move code to testable classes.
  • 11.
    Demo - refactorto reach testability  We start from a web form where all the logic is inside the code behind  We will move, step by step, to a custom and simple implementation of MVC  We add test for the logic of the page  The goal is having a web page so simple that not a simple if should remain in code behind.
  • 12.
    Tips & Tricks Use a CDN service when possible to deliver library services (AJAX, jQuery and so on) (http://www.asp.net/ajax/cdn/)  Consider using Javascript or jQuery for simple tasks to avoid unneeded PostBacks  Test…test…test…
  • 13.
    Questions ? We hopeNOT, cause we are TIRED and we want to EAT and DRINK!