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