Functions should not have identical implementations

There are rules such as this Java static code analysis: All branches in a conditional structure should not have exactly the same implementation that will find identical branches in control structures.

In light of just having been made aware of a bug (written by yours truly :upside_down_face: ) where three functions have the same implementation due to non diligent use of copy paste, I thought it would be a good idea to make a post about this.

It is likely a bug if functions have exactly the same implementation. Maybe if the function body consists of only one function call there could be an argument to allow duplicates from a readability standpoint.

Functions whose bodies are non-trivial that differ at most by the name of local variables, including parameters are candidates for non-conformance for this rule.

Maybe itā€™s worth considering this rule on a per-class basis. Member functions of different classes could conceivably have the same implementation, especially in terms of using member functions/variables that are not public. However, if functions on the same class are more likely to be subject to this rule.

Hello @torgeir.skogen,

Thanks for your idea. However we already have a rule:

That could be interesting why this rule wasnā€™t triggered in your case. Or are you talking about anything else?

Regards,
Margarita

Iā€™m sorry but I canā€™t seem to find the code I fixed that relates to this suggestion.

Iā€™m not sure how I didnā€™t find this rule, but for what itā€™s worth, I brought this up as a general topic that relates to many programming languages. Iā€™m personally working in C++ most of the time. Maybe part of the confusion is that in some languages thereā€™s a rule called Methods should not have identical implmentations, while in some others, thereā€™s Functions should have identical implementations. It appears that C++ only has the alternative about methods. So maybe I searched for something and specified ā€œfunctionsā€ for C++, and didnā€™t find the specific rule, then proceeded to assume that it didnā€™t exist for any programming languages.

Either way, though, I suspect that this rule didnā€™t trigger in my case because I write a lot of functions in C++ that arenā€™t member functions. As far as I know, that is the preferred lingo about this in the context of C++, instead of methods. So maybe the rule for C++ only checks for identical member functions of the same class, while free functions are out of scope for that specific rule? Which I guess would go to show that thereā€™s a difference between methods and functions. I noticed that for python specifically, the rule is called Functions and methods.... Which certainly implies thereā€™s a difference.

Thanks for the reply. As far as there is no reproducer I canā€™t investigate why Java rule wasnā€™t triggered.

If you think your feedback about methods and functions in C++ is still relevant, you can open another thread and add c++ tag. So the proper language specialist will answer you. For this thread, I think we can close it.

Regards,
Margarita