Skip to main content

When Do I Refactor?

When do I know it's time to refactor code?


Refactoring is different for every person but for me I follow these guidelines:
- If I don't know / understand the code, I don't refactor.  Period!
- Is the code broken?  If not then I generally don't refactor
- Can the code be made much simpler?  If the amount of code can be reduced by 2x and made cleaner / simpler then I may refactor.
- Can the existing code handle the new feature / bug fix?  If not then I may refactor.
- I will not refactor the code if I think I have a better way.  Better my be for me but not other developers.  I leave working code working.

Tests:
I never assume that tests for existing code:
-  Has complete code coverage (I've never seen this yet).  I've seen 100% coverage but not all combinations of logic or possible side effects of API changes, etc.
- That the tests are even correct.  The tests only test what the developer wanted to test, not what could / should happen.  Tests are never free from bugs just like the code that is being tested.  If there are x% of bugs for every 1,000 lines of code then the tests will also contain basically the same rate of bugs.

For these reasons, I never rely on tests for catch bugs when I refactoring code.  I need to fully understand the logic and intension of the code before I refactor.  Otherwise I'll just create additional bugs and duplicate failed logic in the new code.

For me, I try to make sure that my code is as solid, understandable and as simple as possible.  If code is not understandable then it's at rick of being churned by refactoring.

Comments

Popular posts from this blog

Still Life

 Life is never still, at least I don't think you would want it to be.  That's why so many of us (i.e., retired care free people) travel.  Keep seeing new things and places.   Well the Hurricane so far inland was a new thing and one I would have rather forgone but life is what happens.  Life keeps moving. Except a still life is forever and captures a slice of time frozen in a painting.  This is from a wooden bowl of pears onboard our ship during dinner one nice.  Again, just thought it looked nice. Enjoy.

A Trip Abroad

 Just back from our trip abroad as Asheville continues to recover from the hurricane.  This trip has been planned for a long time and we almost didn't make it because our dog sitting business will be closed for a while. But make it we did and now we are back. We love Amsterdam.  What a fun place to visit.  So many things to too and do there.  The weather was not the best but it did not have an impact on our visit.  Just bundle up. Got some quick painting in while on the boat (i.e., Viking) as we moved from port to port.  This painting is of a garden at a heritage site of windmills.  I thought it looked nice. Nice time on the trip to de-stress from what was happing back home.  We were fine back home but not everything is well with many others.

So THIS is My Style?

 If I play around long enough my style will appear.  I'm guessing that this is kind of it.  I'll keep working on other techniques in watercolor but for now this appears to be my style. I do like it and others appear (to my face) to also like this.  Not every one of my paintings is a success.  About 1/3 so far, but when they do I am rather pleased that anything good comes out of it at all. I do love color.  Color is happy and outdoors is full of color, be it the west or back east with the greens.  Color color color. Also doing some painting on hot press paper and see how that goes. Later......