• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using open-close princle when fixing prod bug

 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the open close priciple: Software entities (classes, modules, functions etc) should be open for extension, but closed for modification.

Suppose I have a prod bug that needs to be fixed ASAP. I locate where the bug is and want to put a fix to it but I don't want to (1) make a lot of modification to the code and (2) encapsulate the modification in one location. What is the best way to achieve this?

Class ServiceClient{
...
...

//this the a very long method. The bug causes by two line
//within this method
public void run() throws ApplicationException{

...
Line 100 // Noted: the bug is caused by these two lines
Line 101
...

}
}
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joe Nguyen:
According to the open close priciple: Software entities (classes, modules, functions etc) should be open for extension, but closed for modification.



Two important things to notice:

- the principle is talking about *extension*, not bug fixing

- it says that entities should be designed a way that you can extend them without modifying them. When you need to extend a class that isn't designed for that kind of extension, you are simply out of luck - you need to modify it.

Often the best way to modify it in that case is to modify it in a way that it better conforms to the OCP, that is, that the next time you need to extend it in a similar way, you don't need to modify it again.

That might require more modification than is comfortable without having an extensive test rig to confirm that you didn't break something. On the other hand, without making those changes the system is likely to become unmaintainable not too far in the future...
 
Joe Nguyen
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since it is a prod issue and the code is being by many clients, making a lot of changes is not an option at the moment. Besides, the more changes being made, the more errors may be introduced and the longer QA will take to approve the code.

I was thinking to extract these two lines into a new method, modify this new method to fix the bug (making sure there aren't any side effects to the member variables and parameter of the method), and invoke this method from the run() method.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Refactoring a long method into shorter methods might be a worthwhile exercise, but maybe not while in a rush to fix a bug. And not without good regression tests to make you confident you're not breaking anything.

A great thing to do for bugs is to write a test that fails now and will work when the bug is fixed. That forces you to really know what the proper result should be. If you have a long method that does a lot of other stuff this could be pretty hard. If you have a short method that does something simple and returns a single value it's a lot easier.

So there's a reason to fix in place and a reason to extract the defective lines. Guess I'll have to let you choose.
reply
    Bookmark Topic Watch Topic
  • New Topic