Design – How bad is it to have two methods with the same name but different signatures in two classes

class-designclass-diagramdesigndesign-patternssoftware

I have a design problem related to a public interface, the names of methods, and the understanding of my API and code.

I have two classes like this:

class A:
    ...
    function collision(self):
        ....

...

class B:
    ....
    function _collision(self, another_object, l, r, t, b):
        ....

The first class has one public method named collision, and the second has one private method called _collision. The two methods differs in argument type and number.

As an example let's say that _collision checks if the object is colliding with another object with certain conditions l, r, t, b (collide on the left side, right side, etc) and returns true or false. The public collision method, on the other hand, resolves all the collisions of the object with other objects.

The two methods have the same name because I think it's better to avoid overloading the design with different names for methods that do almost the same thing, but in distinct contexts and classes.

Is this clear enough to the reader or I should change the method's name?

Best Answer

These functions both have to do with collisions, but what they do is very different, so giving them names that more clearly describe what they actually do will make it easier for future developers to understand the difference without having to look at the logic inside.

The method that checks if the object is colliding with another object could be called checkForCollision.

The method that resolves collisions of the object with other objects could be called resolveCollisions or handleCollisions.

When two functions do almost the same thing is often the best time to give them more specific names to make their difference clear.

Related Topic