Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good examples when teaching refactoring?

Tags:

refactoring

I'm running a refactoring code dojo for some coworkers who asked how refactoring and patterns go together, and I need a sample code base. Anyone know of a good starting point that isn't to horrible they can't make heads or tails of the code, but can rewrite their way to something useful?

like image 688
Ball Avatar asked Jul 27 '09 19:07

Ball


7 Answers

I would actually suggesting refactoring some of your and your coworkers' code.

There are always places that an existing codebase can be refactored, and the familiarity with the existing code will help make it feel more like a useful thing and less like an exercise. Find something in your company's code to use as an example, if possible.

like image 83
Reed Copsey Avatar answered Sep 19 '22 18:09

Reed Copsey


Here are some codes, both the original and the refactored version, so you can prepare your kata or simply compare the results once the refactoring is performed:

  1. My books have both shorter examples and a longer, actually a book long example. Code is free to download.
    VB Code Examples
    C# Code Examples

  2. A nice example from Refactoring Workbook

  3. There are a lot of examples on the internet of simple games like Tic-Tac-Toe or Snake that have a lot of smells but are simple enough to start with refactoring.

like image 44
Danijel Arsenovski Avatar answered Sep 17 '22 18:09

Danijel Arsenovski


The first chapter in Martin Fowler "Refactoring" is a good starting point to refactoring. I understood most of the concepts when one of my teachers at school used this example.

like image 43
Michaël Larouche Avatar answered Sep 17 '22 18:09

Michaël Larouche


What is the general knowledge level of your coworkers?

Something basic as code duplication should be easy to wrap their heads around. Two pieces of (nearly) identical code that can be refactored into a reusable method, class, whatever. Using a (past) example from your own codebase would be good.

like image 30
Thorarin Avatar answered Sep 18 '22 18:09

Thorarin


I would recommend you to develop a simple example project for a specific requirement.

Then you add one more requirement and make changes to the existing classes . You keep on doing this and show them how you are finding it difficult to make each change when the code is not designed properly. This will make them realize easily because, this is what those ppl will be doing in their day to day work. Make them realize that , if patterns and principles are not followed from beginning, how are they going to end up in mess at the end.

When they realize that,then you start from scratch or refactor the existing messed up code .Now add a requirement and make them realize that it is easy to make a change in the refactored code, so that you need to test only a few classes. One change would not affect others and so on.

You could use the computer ,keyboard and printer class as an example. Add requirements like, you will be wanting the computer to read from mouse , then one more requirement can be like your computer would want to save it in hard disk than printing. Finally your refactored code should be like, your computer class should depend on abstract input device class and output device class. And your keyboard class should inherit from Inputdevice class.

like image 32
Rockstart Avatar answered Sep 20 '22 18:09

Rockstart


Clean Code: A Handbook of Agile Software Craftsmanship by Robert C. Martin considers refactoring.

like image 41
dalazx Avatar answered Sep 21 '22 18:09

dalazx


I'm loving Refactoring Guru examples. In there you can find design patterns examples too.

like image 32
Resul Rzaeeff Avatar answered Sep 18 '22 18:09

Resul Rzaeeff