I have inherited a class method from another developer, which looks like below:
(Note: Class:X
means X
is a member of Class
in the OOP paradigm.)
Class:BigFunction()
{
ImportantValue = calculateImportantValue()
Iterate over Class:Foo from 1 to ImportantValue
Iterate over Class:Bar from 1 to ImportantValue
Iterate over Class:Baz from 1 to ImportantValue
# ... and so on ...
}
I am considering refactoring this to something like:
Class:WrapSeveralSmallFunctions()
{
ImportantValue = calculateImportantValue()
IterateFoo(ImportantValue)
IterateBar(ImportantValue)
IterateBaz(ImportantValue)
# ... and so on ...
}
Class:IterateFoo(ImportantValue)
{
Iterate over Foo from 1 to ImportantValue
}
Class:IterateBar(ImportantValue)
{
Iterate over Bar from 1 to ImportantValue
}
Class:IterateBaz(ImportantValue)
{
Iterate over Baz from 1 to ImportantValue
}
Per Single Responsibility Principle, the refactored code looks better to me.
However, I am also concerned about passing the ImportantValue
as an argument to every such "small" function. Is my concern valid? Is the proposed refactoring considered a good coding practice, or is there any better way of writing this code?