Hello there freaks & geeks. Here comes my question again, thanks for helping!.
Lets suppose i have an object Foo, which method foo() does about 100 lines.
foo()
{
…
qwert
yuiop
asdfg
zxcvb
nmhjk
…
}
If a developer wants to add some code to foo(), it can be easily done with inheritance, composing or a Decorator Pattern.
But in the case he wants to modify “one line” in the middle of the code (changing a property value, invoking a method before something…), whats the best way to do it?
foo2()
{
…
qwert
yuiop
ASDFG
zxcvb
nmhjk
…
}
The goal is to let the original developer to define a behavior without worriying about what others will modify/change.
The new developer should be able to change the object/method/property without retyping the code.
Inserting breaks or predefining steps with callbacks/listeners or dividing the code into smaller pieces is not the desired option, cause it involves the first developer, which is sleeping. 😛
What about a commit environment where the changes made to the object need a commit to apply?
foo() does normal, then foo2 just change the desired properties and make commit.
Any other alternatives?
Thanks!
Retyping the lines of code is not the issue, because the chances are it will be just a copy&paste. You need to eliminate code duplication. So make you function overrideable (so the next developer can inherit from it or extend it), and factor out all the common code into smaller separate functions.
If you are using .Net then you could also consider refactoring the function to accept a delegate or lambda as one of its parameters, and that delegate/lambda gets executed at the critical spot (this may be your best option). You may also be able to achieve your aim with partial classes – you write some of the code of the class, the other developer writes the rest, although this may be a messy solution for your problem.
This is likely to also be a very messy and inefficient solution.