Would anybody share their refactoring tips, what worked and what didn’t?
Would anybody share their refactoring tips, what worked and what didn’t?
I'm dealing with my code that I started developing 15 years ago, with learning Delphi as I came across a problem. As self-employed, the focus was on sell-able features not on manageable code. So, combination of developing as fast as possible and being a beginner, I still have a lot of global variables, no classes, lots of boiler-plate code and other beginners mistakes.
In past few years I'm refactoring and making code easier to manage. Now I have multiple common units: GlobalSettings, GlobalVars, Utils, Utils2, CommonCode... and spend quite some time tracking down the appropriate methods, either is (‘X’ is just example) GetXName, GetXNo, GetXID, GetXSeqID, GetXName, GetXNameFromID, GetXIDFromName... and 100s of other similar names all working on different ‘X’ arrays.
The old style of coding was awful, but one benefit was that almost every single feature was in just a few long methods. So, I just had to browse through couple of methods and I understood the code and what is where.
Now it became much easier to reuse refactored methods, but much harder to track down which methods I already have, do they really do what I need now and so on... a new kind of mess :)
So, any tips how you manage refactored code and how I can avoid over-refactoring methods into ‘oblivion’?
I'm dealing with my code that I started developing 15 years ago, with learning Delphi as I came across a problem. As self-employed, the focus was on sell-able features not on manageable code. So, combination of developing as fast as possible and being a beginner, I still have a lot of global variables, no classes, lots of boiler-plate code and other beginners mistakes.
In past few years I'm refactoring and making code easier to manage. Now I have multiple common units: GlobalSettings, GlobalVars, Utils, Utils2, CommonCode... and spend quite some time tracking down the appropriate methods, either is (‘X’ is just example) GetXName, GetXNo, GetXID, GetXSeqID, GetXName, GetXNameFromID, GetXIDFromName... and 100s of other similar names all working on different ‘X’ arrays.
The old style of coding was awful, but one benefit was that almost every single feature was in just a few long methods. So, I just had to browse through couple of methods and I understood the code and what is where.
Now it became much easier to reuse refactored methods, but much harder to track down which methods I already have, do they really do what I need now and so on... a new kind of mess :)
So, any tips how you manage refactored code and how I can avoid over-refactoring methods into ‘oblivion’?
As part of those refactoring patterns, it is adviced to get rid of global variables and encapsulate them in ready to inject classes.
ReplyDeletePaul TOTH I absolutely had lots of those - all necessary code in one Button OnClick event and then called from many other buttons :)
ReplyDeleteAlmost all gone now.
A. Bouchez Thanks, interesting power point! If I had to start a completely new project now, I might look into mORMot.
One of the learning curves of refactoring for me was, as it is noted again and again in above comments, to go step by step. For example I was accessing registry settings on FormCreate, FormShow, OnClick... FormClose... and other control events and it was a mess. Then finally I put all registry access into one method that read all the settings for ALL the forms at once and set the check/enabled properties. Well, this worked good until I started optimizing startup time and stopped creating all the forms at the beginning - this caused errors in my new settings method, since not all forms were created, yet. So, back to the drawing board and customize so that each Form can control accessing registry as it can.
So, refactoring doesn't necessary mean do it once good and right and forget about it - no, you will come back at some point :)
Mike Torrettinni Yes, refactoring is an iterative process, by definition. My slides show some good working paths about it.
ReplyDelete