DEV Community

Leonora Der
Leonora Der

Posted on

Do you have nice examples of refactoring code?

I am looking for code samples that are good examples of how to refactor code. I would be the happiest if you could provide the code both before and after the refactorings. I am interested in any language, but Java and Python is preferred.

Why? I am planning to make a meetup on the topic and I would need a good base for it.

(I know that there are a lot of stuff out there like refactoring.guru, etc, but I am interested in real-life code samples! :) Also, I have a big bunch of code from our codebase, but I hope you might have even more interesting samples! )

Any help is appreciated! :)

Top comments (8)

Collapse
 
chuck_ha profile image
Chuck Ha

I just wrote a post on refactoring some Go code to use interfaces. I outline the steps used to refactor this particular pattern: dev.to/chuck_ha/interfaces-in-go-3onc

I would also suggest reading sandimetz.com/99bottles/. It's a fantastic book that refactors code throughout the book and explains how to get there.

Collapse
 
vga profile image
Victor Gallet

This kata is a famous one :

github.com/emilybache/GildedRose-R...

Also, you can take a look at this one :

github.com/sandromancuso/trip-serv...

Java one is also explained in two blog articles here :

codurance.com/2011/07/16/testing-l...

Collapse
 
joshcheek profile image
Josh Cheek

Someone at work asked me to refactor their 99 bottles code. I recorded the refactoring so they could see the process and how I think about it. Before is here and after is here. It's a pretty good demonstration of how I refactor.

Collapse
 
okolbay profile image
andrew

is it about safe refactoring (with ide)
or refactoring to design patterns? if its the latter, I recommend going through Clean Code by Rob Martin and
Refactoring: Improving the Design of Existing Code by Kent Beck and Martin Fowler

Collapse
 
mteheran profile image
Miguel Teheran

(C#) I just made one that I found in StackOverflow. I implemented the code in a WebApi and it is working fine :D

stackoverflow.com/questions/333111...

Collapse
 
bohdanstupak1 profile image
Bohdan Stupak

Here's some C# refactoring exercise
c-sharpcorner.com/Forums/refactori...

Collapse
 
maryhale132 profile image
Mary Hales

I'd be happy to help! I have some Java and Python examples of code refactorings that I've worked on in the past. I'd be happy to share them with you. Also, if you have troubles with the tasks in college, click for more info on resources that can assist you. Would you like me to share a specific type of refactoring, such as simplifying a complex algorithm or improving code readability? Also, I'd love to hear more about your meetup plans, what's the target audience and what kind of refactoring topics do you plan to cover?

Collapse
 
jess profile image
Jess Lee