I have two performance-critical functions like this:
insertExpensive(Holder* holder, Element* element, int index){
//............ do some complex thing 1
holder->ensureRange(index);//a little expensive
//............ do some complex thing 2
}
insertCheap(Holder* holder, Element* element, int index){
//............ do some complex thing 1
//............ do some complex thing 2
}
How to group 2 functions together to increase maintainability?
My poor solutions:
Solution 1.
insertExpensive(Holder* holder, Element* element, int index){
do1();
holder->ensureRange(index);//a little expensive
do2();
}
insertCheap(Holder* holder, Element* element, int index){
do1();
do2();
}
It would be ugly.
It also impractical if do2
want some local variables from do1
.
Solution 2.
insert(Holder* holder, Element* element, int index, bool check){
//............ do some complex thing 1
if(check)holder->ensureRange(index);//a little expensive
//............ do some complex thing 2
}
It costs a conditional checking for every call.
Solution 3. (draft)
template<bool check> insert(Holder* holder, Element* element, int index){
//............ do some complex thing 1 (Edit2 from do1());
bar<check>();
//............ do some complex thing 2 (Edit2 from do2());
}
template <>
inline void base_template<true>::bar() { holder->ensureRange(index); }
template <>
inline void base_template<false>::bar() { }
Overkill and unnecessary complexity?
Edit 1:
The priority of criteria for how good an approach is, are sorted as followed:-
1. Best performance
2. Less duplicate of code
3. Less total line of code
4. Easier to read for expert & beginner
Edit 2: edit the 3rd solution. Thank mvidelgauz and Wolf.