Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming and overloading

Tags:

c#

.net

I have two method overloads:

bool HasRight(RightType rightType, string cityId);
bool HasRight(RightType rightType, string cityAlias);

Of course it won't compile as methods signatues are the same. What is the best way to solve naming problem in this case?

like image 489
SiberianGuy Avatar asked Jun 30 '26 03:06

SiberianGuy


2 Answers

Use different method names, don't misuse overloading for semantically-disconnected things.

For example:

bool HasRightById(RightType rightType, string cityId);
bool HasRightByAlias(RightType rightType, string cityAlias);
like image 146
Ondrej Tucny Avatar answered Jul 01 '26 18:07

Ondrej Tucny


Would you immediately be able to distinguish between an id and an alias? How about simply:

bool HasRight(RightType rightType, string cityIdOrAlias)

If it isn't clear cut, something like "if it starts with :, for example :nyc, then it is an id, else it is assumed to be an alias". Other options:

  • suffixed names:

    bool HasRightById(RightType rightType, string cityId)
    bool HasRightByAlias(RightType rightType, string cityAlias)
    
  • take both and demand exactly one inside the method:

    bool HasRight(RightType rightType, string cityId, string cityAlias)
    
like image 22
Marc Gravell Avatar answered Jul 01 '26 18:07

Marc Gravell