Is it enough for methods to be distinguished just by argument name (not type)?
up vote
33
down vote
favorite
Is it enough for methods to be distinguished just by argument name (not type) or is it better to name it more explicitly?
For example T Find<T>(int id)
vs T FindById<T>(int id)
.
Is there any good reason to name it more explicitly (i.e. adding ById
) vs keeping just argument name?
One reason I can think of is when signatures of the methods are the same but they have a different meaning.
FindByFirstName(string name)
and FindByLastName(string name)
c# naming coding-standards conventions method-overloading
|
show 1 more comment
up vote
33
down vote
favorite
Is it enough for methods to be distinguished just by argument name (not type) or is it better to name it more explicitly?
For example T Find<T>(int id)
vs T FindById<T>(int id)
.
Is there any good reason to name it more explicitly (i.e. adding ById
) vs keeping just argument name?
One reason I can think of is when signatures of the methods are the same but they have a different meaning.
FindByFirstName(string name)
and FindByLastName(string name)
c# naming coding-standards conventions method-overloading
4
So when you overload Find to includeT Find<T>(string name)
or(int size)
how do you plan to resolve the inevitable problems?
– UKMonkey
2 days ago
2
@UKMonkey what inevitable problems?
– Konrad
2 days ago
3
in the first case: if multiple entries have the same name then you would have to change the function signature; which means people will likely get confused with what it's meant to return; In the latter case, the argument is the same - and thus an illegal overload. You either start naming the function with "byX" or make an object for the argument so that you can have the equivalent of overload with same argument. Either works well for different situations.
– UKMonkey
2 days ago
2
@UKMonkey you can post an answer with some code examples if you want
– Konrad
2 days ago
2
Ids should probably be an opaqueID
object and not just anint
. In that way get compile-time checking that you do not use an id for an int or viceversa in some part of your code. And with that you can havefind(int value)
andfind(ID id)
.
– Bakuriu
2 days ago
|
show 1 more comment
up vote
33
down vote
favorite
up vote
33
down vote
favorite
Is it enough for methods to be distinguished just by argument name (not type) or is it better to name it more explicitly?
For example T Find<T>(int id)
vs T FindById<T>(int id)
.
Is there any good reason to name it more explicitly (i.e. adding ById
) vs keeping just argument name?
One reason I can think of is when signatures of the methods are the same but they have a different meaning.
FindByFirstName(string name)
and FindByLastName(string name)
c# naming coding-standards conventions method-overloading
Is it enough for methods to be distinguished just by argument name (not type) or is it better to name it more explicitly?
For example T Find<T>(int id)
vs T FindById<T>(int id)
.
Is there any good reason to name it more explicitly (i.e. adding ById
) vs keeping just argument name?
One reason I can think of is when signatures of the methods are the same but they have a different meaning.
FindByFirstName(string name)
and FindByLastName(string name)
c# naming coding-standards conventions method-overloading
c# naming coding-standards conventions method-overloading
edited yesterday
Deduplicator
5,00931836
5,00931836
asked 2 days ago
Konrad
488618
488618
4
So when you overload Find to includeT Find<T>(string name)
or(int size)
how do you plan to resolve the inevitable problems?
– UKMonkey
2 days ago
2
@UKMonkey what inevitable problems?
– Konrad
2 days ago
3
in the first case: if multiple entries have the same name then you would have to change the function signature; which means people will likely get confused with what it's meant to return; In the latter case, the argument is the same - and thus an illegal overload. You either start naming the function with "byX" or make an object for the argument so that you can have the equivalent of overload with same argument. Either works well for different situations.
– UKMonkey
2 days ago
2
@UKMonkey you can post an answer with some code examples if you want
– Konrad
2 days ago
2
Ids should probably be an opaqueID
object and not just anint
. In that way get compile-time checking that you do not use an id for an int or viceversa in some part of your code. And with that you can havefind(int value)
andfind(ID id)
.
– Bakuriu
2 days ago
|
show 1 more comment
4
So when you overload Find to includeT Find<T>(string name)
or(int size)
how do you plan to resolve the inevitable problems?
– UKMonkey
2 days ago
2
@UKMonkey what inevitable problems?
– Konrad
2 days ago
3
in the first case: if multiple entries have the same name then you would have to change the function signature; which means people will likely get confused with what it's meant to return; In the latter case, the argument is the same - and thus an illegal overload. You either start naming the function with "byX" or make an object for the argument so that you can have the equivalent of overload with same argument. Either works well for different situations.
– UKMonkey
2 days ago
2
@UKMonkey you can post an answer with some code examples if you want
– Konrad
2 days ago
2
Ids should probably be an opaqueID
object and not just anint
. In that way get compile-time checking that you do not use an id for an int or viceversa in some part of your code. And with that you can havefind(int value)
andfind(ID id)
.
– Bakuriu
2 days ago
4
4
So when you overload Find to include
T Find<T>(string name)
or (int size)
how do you plan to resolve the inevitable problems?– UKMonkey
2 days ago
So when you overload Find to include
T Find<T>(string name)
or (int size)
how do you plan to resolve the inevitable problems?– UKMonkey
2 days ago
2
2
@UKMonkey what inevitable problems?
– Konrad
2 days ago
@UKMonkey what inevitable problems?
– Konrad
2 days ago
3
3
in the first case: if multiple entries have the same name then you would have to change the function signature; which means people will likely get confused with what it's meant to return; In the latter case, the argument is the same - and thus an illegal overload. You either start naming the function with "byX" or make an object for the argument so that you can have the equivalent of overload with same argument. Either works well for different situations.
– UKMonkey
2 days ago
in the first case: if multiple entries have the same name then you would have to change the function signature; which means people will likely get confused with what it's meant to return; In the latter case, the argument is the same - and thus an illegal overload. You either start naming the function with "byX" or make an object for the argument so that you can have the equivalent of overload with same argument. Either works well for different situations.
– UKMonkey
2 days ago
2
2
@UKMonkey you can post an answer with some code examples if you want
– Konrad
2 days ago
@UKMonkey you can post an answer with some code examples if you want
– Konrad
2 days ago
2
2
Ids should probably be an opaque
ID
object and not just an int
. In that way get compile-time checking that you do not use an id for an int or viceversa in some part of your code. And with that you can have find(int value)
and find(ID id)
.– Bakuriu
2 days ago
Ids should probably be an opaque
ID
object and not just an int
. In that way get compile-time checking that you do not use an id for an int or viceversa in some part of your code. And with that you can have find(int value)
and find(ID id)
.– Bakuriu
2 days ago
|
show 1 more comment
3 Answers
3
active
oldest
votes
up vote
64
down vote
accepted
Sure there is a good reason to name it more explicitly.
It's not primarily be the method definition that should be self-explanatory, but the method use. And while findById(string id)
and find(string id)
are both self-explanatory, there is a huge difference between findById("BOB")
and find("BOB")
. In the former case you know that the random literal is, in fact, an Id. In the latter case you're not sure - it might actually be a given name or something else entirely.
9
Except in the 99% of other cases where you have a variable or property name is a point of reference:findById(id)
vsfind(id)
. You can go either way on this.
– Greg Burghardt
2 days ago
15
@GregBurghardt: A particular value is not necessarily named the same way for a method and its caller. For example, considerdouble Divide(int numerator, int denominator)
being used in a method:double murdersPerCapita = Divide(murderCount, citizenCount)
. You can't rely on two methods using the same variable name as there are plenty of cases where that is not the case (or when it is the case, it's bad naming)
– Flater
2 days ago
1
@Flater: Given the code in the question (finding stuff from some sort of persistent storage) I imagine you might call this method with a "murderedCitizenId" or "citizenId" ... I really don't think the argument or variable names are ambiguous here. And honestly I could go either way on this. It's actually a very opinionated question.
– Greg Burghardt
2 days ago
4
@GregBurghardt: You cannot distill a global rule from a single example. OP's question is in general, not specific to the example given. Yes, there are some cases where using the same name makes sense, but there are also cases where it doesn't. Hence this answer,, it's best to aim for consistency even if it's not necessary in a subset of use cases.
– Flater
2 days ago
1
Names parameters resolve the confusion after the confusion has already existed, as you need to look at the method definition, while explicitly named methods entirely avoid the confusion by having the name present in the method call. Also, you can't have two methods with the same name and argument type but different argument name, which means you're going to need explicit names in certain cases anyway.
– Darkhogg
2 days ago
|
show 3 more comments
up vote
32
down vote
There are reasons to prefer FindById
(versus a compelling one in favor of Find
: Brevity).
Future-proofing: If you start with
Find(int)
, and later have to add other methods (FindByName(string)
,FindByLegacyId(int)
,FindByCustomerId(int)
,FindByOrderId(int)
, etc), people like me tend to spend ages until they finally find theFind(int)
method because they are looking forFindById(int)
. Not really a problem if you can and will changeFind(int)
toFindById(int)
once it becomes necessary, but that is something that will maybe happen in the future - future proofing is all about these maybes.Make things easier to read.
Find
is perfectly fine if the call looks likerecord = Find(customerId);
YetFindById
is slightly easier for reading if it'srecord = FindById(AFunction());
orrecord = FindById(AFunction() * A_MAGIC_NUMBER + AnotherFunction());
-
although you could argue that you have bigger problems if you reach that point.Consistency. You can consistently apply the
FindByX(int)
/FindByY(int)
pattern everywhere, but that won't work forFind(int X)
/Find(int Y)
.
Personally, I prefer a different approach:
// Use Id<Customer> or CustomerID, depending on local coding standards
CustomerRecord Find(Id<Customer> id)
Related: Strongly typing ID values in C#
The above is the answer to the question, the below is clarification and additional information based on feedback provided by comments.
The advantages of a simple Find
all boil down to the initially mentioned Brevity, which is valued highly by some, and not so by others:
- KISS.
Find
is simple and straightforward, and alongsideoperator
it's one of the 2 most expected function names in this context. (Some popular alternatives beingget
,lookup
, orfetch
, depending on context). Some schools of thought oppose unconditionally applying the KISS principle. - As a rule of thumb, if you have a function name that is a single well-known word which accurately describes what the function does, you need a very compelling argument to prefer a function name made up from multiple words, even if the latter is slightly better at describing what the function does. See: Length vs NumberOfElements. What is and what isn't a "very compelling argument" is subjective.
- Some schools of thought aim to avoid redundancy whereever possible. If we look at
FindById(int id)
, we can easily remove redundancy by changing it toFind(int id)
. It's worth mentioning that some schools of thought disagree and do embrace redundancy under various circumstances.
Regarding the suggested alternative Find(Id<Customer> id)
, there was an upvoted comment that heavily criticizes it. I'm including the response in this answer because similar criticism did appear in the linked question as well, meaning they are shared by multiple readers.
Concern 1: It's an abuse of generics. A CustomerId and an OrderID are not the same thing, but the implementation ends up being similar. A common approach to similar implementations is metaprogramming. Using generics for metaprogramming is their purpose, not an abuse. There is value in a discussion about either exposing or hiding the generic, something which will ultimately come down to local coding standards.
Concern 2: It doesn't stop simple mistakes./It's a solution in search of a problem The linked version absolutely does stop plenty of mistakes, with the most common one being the wrong argument order inDoSomething(int customerId, int orderId, int productId)
. If it's used in green-field projects there is no need to expose the underlying implementation, thus preventing even more possible mistakes. Strongly typed Ids also resolve the issue the OP asked us about.
Concern 3: It really just obscures code. It's hard to tell that an id is held inint aVariable
. It is easy to tell what kind of Id is held inId<Customer> aVariable
. That's because the latter is less obscure. Knowing what a variable represents is always more important than knowing how it does so, because unlike the "how", the "what" is useful on it's own.
Concern 4: These Ids are no strong types, just wrappers.std::string
is just a wrapper aroundchar*
. If a class wraps something, it does not follow that the class isn't a strong type. Using a class to wrap the implementation detail of an integer id serves the principle of making interfaces easy to use correctly, hard to use incorrectly, by removing unnecessary and harmful functionality like bitwise operations. It also provides encapsulation, which makes it a lot easier to account for new requirements such as larger IDs, leading zeros, or alphanumeric IDs. On top of that, implementingCustomerId
as a class supports the single responsibility principle.
Concern 5: It's over engineered. Here's a minimal version, although I do recommend addingoperator==
andoperator!=
as well, since I find they are easier to read thanEquals
.
.
public struct Id<T>: {
private readonly int _value ;
public Id(int value) { _value = value; }
public static explicit operator int(Id<T> id) { return id._value; }
}
8
I'd like to add that I can't really think of any downside to naming a methodFindByID
instead ofFind
, there's been many times where I got annoyed with an API that had vague methods likeFind
, but I've never said "FindByID
is too verbose, I really wish the API would have been more vague" (assuming that your method isn't 50 characters long or something, don't doFindByCustomerIDInUnitedStatesOnATuesdayWhenThePrinterIsOutOfInkAndWeRanOutOfCoffee
).
– jrh
2 days ago
1
Also... I know that parameter overloading is a language feature, but IMO it should be used responsibly, functions that make use of it should be rather similar (and more importantly, throw similar exceptions). I guess I would prefer that overloaded methods just allow me to be more or less specific (also yes, I know that default parameters accomplish most of this), it's a judgement call but I'd recommend erring on the side of making a new method instead of making yet another overload, partially because browsing and referring to the documentation for an overloaded function is harder.
– jrh
2 days ago
10
Why is brevity "extremely compelling"? Will you die younger if you type those extra four letters?
– Eric Lippert
2 days ago
4
@EricLippert: Each of the last few C# versions added a few features that don't add any new functionality, just improved code brevity. I imagine those were deemed a priority for the same reason.
– BlueRaja - Danny Pflughoeft
2 days ago
2
@jrh I did not mean to imply that the very short names are acceptable; I generally do not appreciate abbreviated symbols. I meant that I actually do have to deal withFindByCustomerIDInUnitedStatesOnATuesdayWhenThePrinterIsOutOfInkAndWhen...
often on my current project, as there are a lot of those. It's awful. Just offering up an anecdote where someone actually has to deal with that. I agree with your statements though. As long as you don't go crazy with it (like on my current project), sane maintainers are happy to have descriptive self-documenting symbols.
– Aaron
2 days ago
|
show 5 more comments
up vote
9
down vote
Another way of thinking about this is to use the type safety of the language.
You can implement a method such as:
Find(FirstName name);
Where FirstName is a simple object that wraps a string which contains the first name and means there can be no confusion as to what the method is doing, nor in the arguments with which it is called.
New contributor
4
Not sure what your answer is to the OPs question. Do you recommend to use a name like "Find" by relying on the type of the argument? Or do you recommend to use such names only when there is an explicit type for the argument(s), and use a more explicit name like "FindById" elsewhere? Or do you recommend to introduce explicit types to make a name like "Find" more feasible?
– Doc Brown
2 days ago
2
@DocBrown I think the latter, and I like it. It's actually similar to Peter's answer, iiuc. The rationale as I understand it is two-fold: (1) It's clear from the argument type what the function does; (2) You cannot make mistakes likestring name = "Smith"; findById(name);
.which is possible if you use non-descript general types.
– Peter A. Schneider
2 days ago
5
While in general I am a fan of compile time type safety, be careful with wrapper types. Introducing wrapper classes for the sake of type safety can at times severely complicate your API if done in excess. e.g., the whole "artist formerly known as int" problem that winapi has; in the long run I would say most people just look at the endlessDWORD
LPCSTR
etc. clones and think "it's an int / string / etc.", it gets to the point where you spend more time propping up your tools than you do actually designing code.
– jrh
2 days ago
1
@jrh True. My litmus test for introducing "nominal" types (differing only in name) is when common functions/methods don't make any sense in our use case, e.g.int
s are often summed, multiplied, etc. which is meaningless for IDs; so I'd makeID
distinct fromint
. This can simplify an API, by narrowing down what we can do given a value (e.g. if we have anID
, it will only work withfind
, not e.g.age
orhighscore
). Conversely, if we find ourselves converting a lot, or writing the same function/method (e.g.find
) for multiple types, that's a sign that our distinctions are too fine
– Warbo
2 days ago
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
64
down vote
accepted
Sure there is a good reason to name it more explicitly.
It's not primarily be the method definition that should be self-explanatory, but the method use. And while findById(string id)
and find(string id)
are both self-explanatory, there is a huge difference between findById("BOB")
and find("BOB")
. In the former case you know that the random literal is, in fact, an Id. In the latter case you're not sure - it might actually be a given name or something else entirely.
9
Except in the 99% of other cases where you have a variable or property name is a point of reference:findById(id)
vsfind(id)
. You can go either way on this.
– Greg Burghardt
2 days ago
15
@GregBurghardt: A particular value is not necessarily named the same way for a method and its caller. For example, considerdouble Divide(int numerator, int denominator)
being used in a method:double murdersPerCapita = Divide(murderCount, citizenCount)
. You can't rely on two methods using the same variable name as there are plenty of cases where that is not the case (or when it is the case, it's bad naming)
– Flater
2 days ago
1
@Flater: Given the code in the question (finding stuff from some sort of persistent storage) I imagine you might call this method with a "murderedCitizenId" or "citizenId" ... I really don't think the argument or variable names are ambiguous here. And honestly I could go either way on this. It's actually a very opinionated question.
– Greg Burghardt
2 days ago
4
@GregBurghardt: You cannot distill a global rule from a single example. OP's question is in general, not specific to the example given. Yes, there are some cases where using the same name makes sense, but there are also cases where it doesn't. Hence this answer,, it's best to aim for consistency even if it's not necessary in a subset of use cases.
– Flater
2 days ago
1
Names parameters resolve the confusion after the confusion has already existed, as you need to look at the method definition, while explicitly named methods entirely avoid the confusion by having the name present in the method call. Also, you can't have two methods with the same name and argument type but different argument name, which means you're going to need explicit names in certain cases anyway.
– Darkhogg
2 days ago
|
show 3 more comments
up vote
64
down vote
accepted
Sure there is a good reason to name it more explicitly.
It's not primarily be the method definition that should be self-explanatory, but the method use. And while findById(string id)
and find(string id)
are both self-explanatory, there is a huge difference between findById("BOB")
and find("BOB")
. In the former case you know that the random literal is, in fact, an Id. In the latter case you're not sure - it might actually be a given name or something else entirely.
9
Except in the 99% of other cases where you have a variable or property name is a point of reference:findById(id)
vsfind(id)
. You can go either way on this.
– Greg Burghardt
2 days ago
15
@GregBurghardt: A particular value is not necessarily named the same way for a method and its caller. For example, considerdouble Divide(int numerator, int denominator)
being used in a method:double murdersPerCapita = Divide(murderCount, citizenCount)
. You can't rely on two methods using the same variable name as there are plenty of cases where that is not the case (or when it is the case, it's bad naming)
– Flater
2 days ago
1
@Flater: Given the code in the question (finding stuff from some sort of persistent storage) I imagine you might call this method with a "murderedCitizenId" or "citizenId" ... I really don't think the argument or variable names are ambiguous here. And honestly I could go either way on this. It's actually a very opinionated question.
– Greg Burghardt
2 days ago
4
@GregBurghardt: You cannot distill a global rule from a single example. OP's question is in general, not specific to the example given. Yes, there are some cases where using the same name makes sense, but there are also cases where it doesn't. Hence this answer,, it's best to aim for consistency even if it's not necessary in a subset of use cases.
– Flater
2 days ago
1
Names parameters resolve the confusion after the confusion has already existed, as you need to look at the method definition, while explicitly named methods entirely avoid the confusion by having the name present in the method call. Also, you can't have two methods with the same name and argument type but different argument name, which means you're going to need explicit names in certain cases anyway.
– Darkhogg
2 days ago
|
show 3 more comments
up vote
64
down vote
accepted
up vote
64
down vote
accepted
Sure there is a good reason to name it more explicitly.
It's not primarily be the method definition that should be self-explanatory, but the method use. And while findById(string id)
and find(string id)
are both self-explanatory, there is a huge difference between findById("BOB")
and find("BOB")
. In the former case you know that the random literal is, in fact, an Id. In the latter case you're not sure - it might actually be a given name or something else entirely.
Sure there is a good reason to name it more explicitly.
It's not primarily be the method definition that should be self-explanatory, but the method use. And while findById(string id)
and find(string id)
are both self-explanatory, there is a huge difference between findById("BOB")
and find("BOB")
. In the former case you know that the random literal is, in fact, an Id. In the latter case you're not sure - it might actually be a given name or something else entirely.
edited 2 days ago
Doc Brown
129k22235374
129k22235374
answered 2 days ago
Kilian Foth
87.9k33239264
87.9k33239264
9
Except in the 99% of other cases where you have a variable or property name is a point of reference:findById(id)
vsfind(id)
. You can go either way on this.
– Greg Burghardt
2 days ago
15
@GregBurghardt: A particular value is not necessarily named the same way for a method and its caller. For example, considerdouble Divide(int numerator, int denominator)
being used in a method:double murdersPerCapita = Divide(murderCount, citizenCount)
. You can't rely on two methods using the same variable name as there are plenty of cases where that is not the case (or when it is the case, it's bad naming)
– Flater
2 days ago
1
@Flater: Given the code in the question (finding stuff from some sort of persistent storage) I imagine you might call this method with a "murderedCitizenId" or "citizenId" ... I really don't think the argument or variable names are ambiguous here. And honestly I could go either way on this. It's actually a very opinionated question.
– Greg Burghardt
2 days ago
4
@GregBurghardt: You cannot distill a global rule from a single example. OP's question is in general, not specific to the example given. Yes, there are some cases where using the same name makes sense, but there are also cases where it doesn't. Hence this answer,, it's best to aim for consistency even if it's not necessary in a subset of use cases.
– Flater
2 days ago
1
Names parameters resolve the confusion after the confusion has already existed, as you need to look at the method definition, while explicitly named methods entirely avoid the confusion by having the name present in the method call. Also, you can't have two methods with the same name and argument type but different argument name, which means you're going to need explicit names in certain cases anyway.
– Darkhogg
2 days ago
|
show 3 more comments
9
Except in the 99% of other cases where you have a variable or property name is a point of reference:findById(id)
vsfind(id)
. You can go either way on this.
– Greg Burghardt
2 days ago
15
@GregBurghardt: A particular value is not necessarily named the same way for a method and its caller. For example, considerdouble Divide(int numerator, int denominator)
being used in a method:double murdersPerCapita = Divide(murderCount, citizenCount)
. You can't rely on two methods using the same variable name as there are plenty of cases where that is not the case (or when it is the case, it's bad naming)
– Flater
2 days ago
1
@Flater: Given the code in the question (finding stuff from some sort of persistent storage) I imagine you might call this method with a "murderedCitizenId" or "citizenId" ... I really don't think the argument or variable names are ambiguous here. And honestly I could go either way on this. It's actually a very opinionated question.
– Greg Burghardt
2 days ago
4
@GregBurghardt: You cannot distill a global rule from a single example. OP's question is in general, not specific to the example given. Yes, there are some cases where using the same name makes sense, but there are also cases where it doesn't. Hence this answer,, it's best to aim for consistency even if it's not necessary in a subset of use cases.
– Flater
2 days ago
1
Names parameters resolve the confusion after the confusion has already existed, as you need to look at the method definition, while explicitly named methods entirely avoid the confusion by having the name present in the method call. Also, you can't have two methods with the same name and argument type but different argument name, which means you're going to need explicit names in certain cases anyway.
– Darkhogg
2 days ago
9
9
Except in the 99% of other cases where you have a variable or property name is a point of reference:
findById(id)
vs find(id)
. You can go either way on this.– Greg Burghardt
2 days ago
Except in the 99% of other cases where you have a variable or property name is a point of reference:
findById(id)
vs find(id)
. You can go either way on this.– Greg Burghardt
2 days ago
15
15
@GregBurghardt: A particular value is not necessarily named the same way for a method and its caller. For example, consider
double Divide(int numerator, int denominator)
being used in a method: double murdersPerCapita = Divide(murderCount, citizenCount)
. You can't rely on two methods using the same variable name as there are plenty of cases where that is not the case (or when it is the case, it's bad naming)– Flater
2 days ago
@GregBurghardt: A particular value is not necessarily named the same way for a method and its caller. For example, consider
double Divide(int numerator, int denominator)
being used in a method: double murdersPerCapita = Divide(murderCount, citizenCount)
. You can't rely on two methods using the same variable name as there are plenty of cases where that is not the case (or when it is the case, it's bad naming)– Flater
2 days ago
1
1
@Flater: Given the code in the question (finding stuff from some sort of persistent storage) I imagine you might call this method with a "murderedCitizenId" or "citizenId" ... I really don't think the argument or variable names are ambiguous here. And honestly I could go either way on this. It's actually a very opinionated question.
– Greg Burghardt
2 days ago
@Flater: Given the code in the question (finding stuff from some sort of persistent storage) I imagine you might call this method with a "murderedCitizenId" or "citizenId" ... I really don't think the argument or variable names are ambiguous here. And honestly I could go either way on this. It's actually a very opinionated question.
– Greg Burghardt
2 days ago
4
4
@GregBurghardt: You cannot distill a global rule from a single example. OP's question is in general, not specific to the example given. Yes, there are some cases where using the same name makes sense, but there are also cases where it doesn't. Hence this answer,, it's best to aim for consistency even if it's not necessary in a subset of use cases.
– Flater
2 days ago
@GregBurghardt: You cannot distill a global rule from a single example. OP's question is in general, not specific to the example given. Yes, there are some cases where using the same name makes sense, but there are also cases where it doesn't. Hence this answer,, it's best to aim for consistency even if it's not necessary in a subset of use cases.
– Flater
2 days ago
1
1
Names parameters resolve the confusion after the confusion has already existed, as you need to look at the method definition, while explicitly named methods entirely avoid the confusion by having the name present in the method call. Also, you can't have two methods with the same name and argument type but different argument name, which means you're going to need explicit names in certain cases anyway.
– Darkhogg
2 days ago
Names parameters resolve the confusion after the confusion has already existed, as you need to look at the method definition, while explicitly named methods entirely avoid the confusion by having the name present in the method call. Also, you can't have two methods with the same name and argument type but different argument name, which means you're going to need explicit names in certain cases anyway.
– Darkhogg
2 days ago
|
show 3 more comments
up vote
32
down vote
There are reasons to prefer FindById
(versus a compelling one in favor of Find
: Brevity).
Future-proofing: If you start with
Find(int)
, and later have to add other methods (FindByName(string)
,FindByLegacyId(int)
,FindByCustomerId(int)
,FindByOrderId(int)
, etc), people like me tend to spend ages until they finally find theFind(int)
method because they are looking forFindById(int)
. Not really a problem if you can and will changeFind(int)
toFindById(int)
once it becomes necessary, but that is something that will maybe happen in the future - future proofing is all about these maybes.Make things easier to read.
Find
is perfectly fine if the call looks likerecord = Find(customerId);
YetFindById
is slightly easier for reading if it'srecord = FindById(AFunction());
orrecord = FindById(AFunction() * A_MAGIC_NUMBER + AnotherFunction());
-
although you could argue that you have bigger problems if you reach that point.Consistency. You can consistently apply the
FindByX(int)
/FindByY(int)
pattern everywhere, but that won't work forFind(int X)
/Find(int Y)
.
Personally, I prefer a different approach:
// Use Id<Customer> or CustomerID, depending on local coding standards
CustomerRecord Find(Id<Customer> id)
Related: Strongly typing ID values in C#
The above is the answer to the question, the below is clarification and additional information based on feedback provided by comments.
The advantages of a simple Find
all boil down to the initially mentioned Brevity, which is valued highly by some, and not so by others:
- KISS.
Find
is simple and straightforward, and alongsideoperator
it's one of the 2 most expected function names in this context. (Some popular alternatives beingget
,lookup
, orfetch
, depending on context). Some schools of thought oppose unconditionally applying the KISS principle. - As a rule of thumb, if you have a function name that is a single well-known word which accurately describes what the function does, you need a very compelling argument to prefer a function name made up from multiple words, even if the latter is slightly better at describing what the function does. See: Length vs NumberOfElements. What is and what isn't a "very compelling argument" is subjective.
- Some schools of thought aim to avoid redundancy whereever possible. If we look at
FindById(int id)
, we can easily remove redundancy by changing it toFind(int id)
. It's worth mentioning that some schools of thought disagree and do embrace redundancy under various circumstances.
Regarding the suggested alternative Find(Id<Customer> id)
, there was an upvoted comment that heavily criticizes it. I'm including the response in this answer because similar criticism did appear in the linked question as well, meaning they are shared by multiple readers.
Concern 1: It's an abuse of generics. A CustomerId and an OrderID are not the same thing, but the implementation ends up being similar. A common approach to similar implementations is metaprogramming. Using generics for metaprogramming is their purpose, not an abuse. There is value in a discussion about either exposing or hiding the generic, something which will ultimately come down to local coding standards.
Concern 2: It doesn't stop simple mistakes./It's a solution in search of a problem The linked version absolutely does stop plenty of mistakes, with the most common one being the wrong argument order inDoSomething(int customerId, int orderId, int productId)
. If it's used in green-field projects there is no need to expose the underlying implementation, thus preventing even more possible mistakes. Strongly typed Ids also resolve the issue the OP asked us about.
Concern 3: It really just obscures code. It's hard to tell that an id is held inint aVariable
. It is easy to tell what kind of Id is held inId<Customer> aVariable
. That's because the latter is less obscure. Knowing what a variable represents is always more important than knowing how it does so, because unlike the "how", the "what" is useful on it's own.
Concern 4: These Ids are no strong types, just wrappers.std::string
is just a wrapper aroundchar*
. If a class wraps something, it does not follow that the class isn't a strong type. Using a class to wrap the implementation detail of an integer id serves the principle of making interfaces easy to use correctly, hard to use incorrectly, by removing unnecessary and harmful functionality like bitwise operations. It also provides encapsulation, which makes it a lot easier to account for new requirements such as larger IDs, leading zeros, or alphanumeric IDs. On top of that, implementingCustomerId
as a class supports the single responsibility principle.
Concern 5: It's over engineered. Here's a minimal version, although I do recommend addingoperator==
andoperator!=
as well, since I find they are easier to read thanEquals
.
.
public struct Id<T>: {
private readonly int _value ;
public Id(int value) { _value = value; }
public static explicit operator int(Id<T> id) { return id._value; }
}
8
I'd like to add that I can't really think of any downside to naming a methodFindByID
instead ofFind
, there's been many times where I got annoyed with an API that had vague methods likeFind
, but I've never said "FindByID
is too verbose, I really wish the API would have been more vague" (assuming that your method isn't 50 characters long or something, don't doFindByCustomerIDInUnitedStatesOnATuesdayWhenThePrinterIsOutOfInkAndWeRanOutOfCoffee
).
– jrh
2 days ago
1
Also... I know that parameter overloading is a language feature, but IMO it should be used responsibly, functions that make use of it should be rather similar (and more importantly, throw similar exceptions). I guess I would prefer that overloaded methods just allow me to be more or less specific (also yes, I know that default parameters accomplish most of this), it's a judgement call but I'd recommend erring on the side of making a new method instead of making yet another overload, partially because browsing and referring to the documentation for an overloaded function is harder.
– jrh
2 days ago
10
Why is brevity "extremely compelling"? Will you die younger if you type those extra four letters?
– Eric Lippert
2 days ago
4
@EricLippert: Each of the last few C# versions added a few features that don't add any new functionality, just improved code brevity. I imagine those were deemed a priority for the same reason.
– BlueRaja - Danny Pflughoeft
2 days ago
2
@jrh I did not mean to imply that the very short names are acceptable; I generally do not appreciate abbreviated symbols. I meant that I actually do have to deal withFindByCustomerIDInUnitedStatesOnATuesdayWhenThePrinterIsOutOfInkAndWhen...
often on my current project, as there are a lot of those. It's awful. Just offering up an anecdote where someone actually has to deal with that. I agree with your statements though. As long as you don't go crazy with it (like on my current project), sane maintainers are happy to have descriptive self-documenting symbols.
– Aaron
2 days ago
|
show 5 more comments
up vote
32
down vote
There are reasons to prefer FindById
(versus a compelling one in favor of Find
: Brevity).
Future-proofing: If you start with
Find(int)
, and later have to add other methods (FindByName(string)
,FindByLegacyId(int)
,FindByCustomerId(int)
,FindByOrderId(int)
, etc), people like me tend to spend ages until they finally find theFind(int)
method because they are looking forFindById(int)
. Not really a problem if you can and will changeFind(int)
toFindById(int)
once it becomes necessary, but that is something that will maybe happen in the future - future proofing is all about these maybes.Make things easier to read.
Find
is perfectly fine if the call looks likerecord = Find(customerId);
YetFindById
is slightly easier for reading if it'srecord = FindById(AFunction());
orrecord = FindById(AFunction() * A_MAGIC_NUMBER + AnotherFunction());
-
although you could argue that you have bigger problems if you reach that point.Consistency. You can consistently apply the
FindByX(int)
/FindByY(int)
pattern everywhere, but that won't work forFind(int X)
/Find(int Y)
.
Personally, I prefer a different approach:
// Use Id<Customer> or CustomerID, depending on local coding standards
CustomerRecord Find(Id<Customer> id)
Related: Strongly typing ID values in C#
The above is the answer to the question, the below is clarification and additional information based on feedback provided by comments.
The advantages of a simple Find
all boil down to the initially mentioned Brevity, which is valued highly by some, and not so by others:
- KISS.
Find
is simple and straightforward, and alongsideoperator
it's one of the 2 most expected function names in this context. (Some popular alternatives beingget
,lookup
, orfetch
, depending on context). Some schools of thought oppose unconditionally applying the KISS principle. - As a rule of thumb, if you have a function name that is a single well-known word which accurately describes what the function does, you need a very compelling argument to prefer a function name made up from multiple words, even if the latter is slightly better at describing what the function does. See: Length vs NumberOfElements. What is and what isn't a "very compelling argument" is subjective.
- Some schools of thought aim to avoid redundancy whereever possible. If we look at
FindById(int id)
, we can easily remove redundancy by changing it toFind(int id)
. It's worth mentioning that some schools of thought disagree and do embrace redundancy under various circumstances.
Regarding the suggested alternative Find(Id<Customer> id)
, there was an upvoted comment that heavily criticizes it. I'm including the response in this answer because similar criticism did appear in the linked question as well, meaning they are shared by multiple readers.
Concern 1: It's an abuse of generics. A CustomerId and an OrderID are not the same thing, but the implementation ends up being similar. A common approach to similar implementations is metaprogramming. Using generics for metaprogramming is their purpose, not an abuse. There is value in a discussion about either exposing or hiding the generic, something which will ultimately come down to local coding standards.
Concern 2: It doesn't stop simple mistakes./It's a solution in search of a problem The linked version absolutely does stop plenty of mistakes, with the most common one being the wrong argument order inDoSomething(int customerId, int orderId, int productId)
. If it's used in green-field projects there is no need to expose the underlying implementation, thus preventing even more possible mistakes. Strongly typed Ids also resolve the issue the OP asked us about.
Concern 3: It really just obscures code. It's hard to tell that an id is held inint aVariable
. It is easy to tell what kind of Id is held inId<Customer> aVariable
. That's because the latter is less obscure. Knowing what a variable represents is always more important than knowing how it does so, because unlike the "how", the "what" is useful on it's own.
Concern 4: These Ids are no strong types, just wrappers.std::string
is just a wrapper aroundchar*
. If a class wraps something, it does not follow that the class isn't a strong type. Using a class to wrap the implementation detail of an integer id serves the principle of making interfaces easy to use correctly, hard to use incorrectly, by removing unnecessary and harmful functionality like bitwise operations. It also provides encapsulation, which makes it a lot easier to account for new requirements such as larger IDs, leading zeros, or alphanumeric IDs. On top of that, implementingCustomerId
as a class supports the single responsibility principle.
Concern 5: It's over engineered. Here's a minimal version, although I do recommend addingoperator==
andoperator!=
as well, since I find they are easier to read thanEquals
.
.
public struct Id<T>: {
private readonly int _value ;
public Id(int value) { _value = value; }
public static explicit operator int(Id<T> id) { return id._value; }
}
8
I'd like to add that I can't really think of any downside to naming a methodFindByID
instead ofFind
, there's been many times where I got annoyed with an API that had vague methods likeFind
, but I've never said "FindByID
is too verbose, I really wish the API would have been more vague" (assuming that your method isn't 50 characters long or something, don't doFindByCustomerIDInUnitedStatesOnATuesdayWhenThePrinterIsOutOfInkAndWeRanOutOfCoffee
).
– jrh
2 days ago
1
Also... I know that parameter overloading is a language feature, but IMO it should be used responsibly, functions that make use of it should be rather similar (and more importantly, throw similar exceptions). I guess I would prefer that overloaded methods just allow me to be more or less specific (also yes, I know that default parameters accomplish most of this), it's a judgement call but I'd recommend erring on the side of making a new method instead of making yet another overload, partially because browsing and referring to the documentation for an overloaded function is harder.
– jrh
2 days ago
10
Why is brevity "extremely compelling"? Will you die younger if you type those extra four letters?
– Eric Lippert
2 days ago
4
@EricLippert: Each of the last few C# versions added a few features that don't add any new functionality, just improved code brevity. I imagine those were deemed a priority for the same reason.
– BlueRaja - Danny Pflughoeft
2 days ago
2
@jrh I did not mean to imply that the very short names are acceptable; I generally do not appreciate abbreviated symbols. I meant that I actually do have to deal withFindByCustomerIDInUnitedStatesOnATuesdayWhenThePrinterIsOutOfInkAndWhen...
often on my current project, as there are a lot of those. It's awful. Just offering up an anecdote where someone actually has to deal with that. I agree with your statements though. As long as you don't go crazy with it (like on my current project), sane maintainers are happy to have descriptive self-documenting symbols.
– Aaron
2 days ago
|
show 5 more comments
up vote
32
down vote
up vote
32
down vote
There are reasons to prefer FindById
(versus a compelling one in favor of Find
: Brevity).
Future-proofing: If you start with
Find(int)
, and later have to add other methods (FindByName(string)
,FindByLegacyId(int)
,FindByCustomerId(int)
,FindByOrderId(int)
, etc), people like me tend to spend ages until they finally find theFind(int)
method because they are looking forFindById(int)
. Not really a problem if you can and will changeFind(int)
toFindById(int)
once it becomes necessary, but that is something that will maybe happen in the future - future proofing is all about these maybes.Make things easier to read.
Find
is perfectly fine if the call looks likerecord = Find(customerId);
YetFindById
is slightly easier for reading if it'srecord = FindById(AFunction());
orrecord = FindById(AFunction() * A_MAGIC_NUMBER + AnotherFunction());
-
although you could argue that you have bigger problems if you reach that point.Consistency. You can consistently apply the
FindByX(int)
/FindByY(int)
pattern everywhere, but that won't work forFind(int X)
/Find(int Y)
.
Personally, I prefer a different approach:
// Use Id<Customer> or CustomerID, depending on local coding standards
CustomerRecord Find(Id<Customer> id)
Related: Strongly typing ID values in C#
The above is the answer to the question, the below is clarification and additional information based on feedback provided by comments.
The advantages of a simple Find
all boil down to the initially mentioned Brevity, which is valued highly by some, and not so by others:
- KISS.
Find
is simple and straightforward, and alongsideoperator
it's one of the 2 most expected function names in this context. (Some popular alternatives beingget
,lookup
, orfetch
, depending on context). Some schools of thought oppose unconditionally applying the KISS principle. - As a rule of thumb, if you have a function name that is a single well-known word which accurately describes what the function does, you need a very compelling argument to prefer a function name made up from multiple words, even if the latter is slightly better at describing what the function does. See: Length vs NumberOfElements. What is and what isn't a "very compelling argument" is subjective.
- Some schools of thought aim to avoid redundancy whereever possible. If we look at
FindById(int id)
, we can easily remove redundancy by changing it toFind(int id)
. It's worth mentioning that some schools of thought disagree and do embrace redundancy under various circumstances.
Regarding the suggested alternative Find(Id<Customer> id)
, there was an upvoted comment that heavily criticizes it. I'm including the response in this answer because similar criticism did appear in the linked question as well, meaning they are shared by multiple readers.
Concern 1: It's an abuse of generics. A CustomerId and an OrderID are not the same thing, but the implementation ends up being similar. A common approach to similar implementations is metaprogramming. Using generics for metaprogramming is their purpose, not an abuse. There is value in a discussion about either exposing or hiding the generic, something which will ultimately come down to local coding standards.
Concern 2: It doesn't stop simple mistakes./It's a solution in search of a problem The linked version absolutely does stop plenty of mistakes, with the most common one being the wrong argument order inDoSomething(int customerId, int orderId, int productId)
. If it's used in green-field projects there is no need to expose the underlying implementation, thus preventing even more possible mistakes. Strongly typed Ids also resolve the issue the OP asked us about.
Concern 3: It really just obscures code. It's hard to tell that an id is held inint aVariable
. It is easy to tell what kind of Id is held inId<Customer> aVariable
. That's because the latter is less obscure. Knowing what a variable represents is always more important than knowing how it does so, because unlike the "how", the "what" is useful on it's own.
Concern 4: These Ids are no strong types, just wrappers.std::string
is just a wrapper aroundchar*
. If a class wraps something, it does not follow that the class isn't a strong type. Using a class to wrap the implementation detail of an integer id serves the principle of making interfaces easy to use correctly, hard to use incorrectly, by removing unnecessary and harmful functionality like bitwise operations. It also provides encapsulation, which makes it a lot easier to account for new requirements such as larger IDs, leading zeros, or alphanumeric IDs. On top of that, implementingCustomerId
as a class supports the single responsibility principle.
Concern 5: It's over engineered. Here's a minimal version, although I do recommend addingoperator==
andoperator!=
as well, since I find they are easier to read thanEquals
.
.
public struct Id<T>: {
private readonly int _value ;
public Id(int value) { _value = value; }
public static explicit operator int(Id<T> id) { return id._value; }
}
There are reasons to prefer FindById
(versus a compelling one in favor of Find
: Brevity).
Future-proofing: If you start with
Find(int)
, and later have to add other methods (FindByName(string)
,FindByLegacyId(int)
,FindByCustomerId(int)
,FindByOrderId(int)
, etc), people like me tend to spend ages until they finally find theFind(int)
method because they are looking forFindById(int)
. Not really a problem if you can and will changeFind(int)
toFindById(int)
once it becomes necessary, but that is something that will maybe happen in the future - future proofing is all about these maybes.Make things easier to read.
Find
is perfectly fine if the call looks likerecord = Find(customerId);
YetFindById
is slightly easier for reading if it'srecord = FindById(AFunction());
orrecord = FindById(AFunction() * A_MAGIC_NUMBER + AnotherFunction());
-
although you could argue that you have bigger problems if you reach that point.Consistency. You can consistently apply the
FindByX(int)
/FindByY(int)
pattern everywhere, but that won't work forFind(int X)
/Find(int Y)
.
Personally, I prefer a different approach:
// Use Id<Customer> or CustomerID, depending on local coding standards
CustomerRecord Find(Id<Customer> id)
Related: Strongly typing ID values in C#
The above is the answer to the question, the below is clarification and additional information based on feedback provided by comments.
The advantages of a simple Find
all boil down to the initially mentioned Brevity, which is valued highly by some, and not so by others:
- KISS.
Find
is simple and straightforward, and alongsideoperator
it's one of the 2 most expected function names in this context. (Some popular alternatives beingget
,lookup
, orfetch
, depending on context). Some schools of thought oppose unconditionally applying the KISS principle. - As a rule of thumb, if you have a function name that is a single well-known word which accurately describes what the function does, you need a very compelling argument to prefer a function name made up from multiple words, even if the latter is slightly better at describing what the function does. See: Length vs NumberOfElements. What is and what isn't a "very compelling argument" is subjective.
- Some schools of thought aim to avoid redundancy whereever possible. If we look at
FindById(int id)
, we can easily remove redundancy by changing it toFind(int id)
. It's worth mentioning that some schools of thought disagree and do embrace redundancy under various circumstances.
Regarding the suggested alternative Find(Id<Customer> id)
, there was an upvoted comment that heavily criticizes it. I'm including the response in this answer because similar criticism did appear in the linked question as well, meaning they are shared by multiple readers.
Concern 1: It's an abuse of generics. A CustomerId and an OrderID are not the same thing, but the implementation ends up being similar. A common approach to similar implementations is metaprogramming. Using generics for metaprogramming is their purpose, not an abuse. There is value in a discussion about either exposing or hiding the generic, something which will ultimately come down to local coding standards.
Concern 2: It doesn't stop simple mistakes./It's a solution in search of a problem The linked version absolutely does stop plenty of mistakes, with the most common one being the wrong argument order inDoSomething(int customerId, int orderId, int productId)
. If it's used in green-field projects there is no need to expose the underlying implementation, thus preventing even more possible mistakes. Strongly typed Ids also resolve the issue the OP asked us about.
Concern 3: It really just obscures code. It's hard to tell that an id is held inint aVariable
. It is easy to tell what kind of Id is held inId<Customer> aVariable
. That's because the latter is less obscure. Knowing what a variable represents is always more important than knowing how it does so, because unlike the "how", the "what" is useful on it's own.
Concern 4: These Ids are no strong types, just wrappers.std::string
is just a wrapper aroundchar*
. If a class wraps something, it does not follow that the class isn't a strong type. Using a class to wrap the implementation detail of an integer id serves the principle of making interfaces easy to use correctly, hard to use incorrectly, by removing unnecessary and harmful functionality like bitwise operations. It also provides encapsulation, which makes it a lot easier to account for new requirements such as larger IDs, leading zeros, or alphanumeric IDs. On top of that, implementingCustomerId
as a class supports the single responsibility principle.
Concern 5: It's over engineered. Here's a minimal version, although I do recommend addingoperator==
andoperator!=
as well, since I find they are easier to read thanEquals
.
.
public struct Id<T>: {
private readonly int _value ;
public Id(int value) { _value = value; }
public static explicit operator int(Id<T> id) { return id._value; }
}
edited yesterday
answered 2 days ago
Peter
2,757415
2,757415
8
I'd like to add that I can't really think of any downside to naming a methodFindByID
instead ofFind
, there's been many times where I got annoyed with an API that had vague methods likeFind
, but I've never said "FindByID
is too verbose, I really wish the API would have been more vague" (assuming that your method isn't 50 characters long or something, don't doFindByCustomerIDInUnitedStatesOnATuesdayWhenThePrinterIsOutOfInkAndWeRanOutOfCoffee
).
– jrh
2 days ago
1
Also... I know that parameter overloading is a language feature, but IMO it should be used responsibly, functions that make use of it should be rather similar (and more importantly, throw similar exceptions). I guess I would prefer that overloaded methods just allow me to be more or less specific (also yes, I know that default parameters accomplish most of this), it's a judgement call but I'd recommend erring on the side of making a new method instead of making yet another overload, partially because browsing and referring to the documentation for an overloaded function is harder.
– jrh
2 days ago
10
Why is brevity "extremely compelling"? Will you die younger if you type those extra four letters?
– Eric Lippert
2 days ago
4
@EricLippert: Each of the last few C# versions added a few features that don't add any new functionality, just improved code brevity. I imagine those were deemed a priority for the same reason.
– BlueRaja - Danny Pflughoeft
2 days ago
2
@jrh I did not mean to imply that the very short names are acceptable; I generally do not appreciate abbreviated symbols. I meant that I actually do have to deal withFindByCustomerIDInUnitedStatesOnATuesdayWhenThePrinterIsOutOfInkAndWhen...
often on my current project, as there are a lot of those. It's awful. Just offering up an anecdote where someone actually has to deal with that. I agree with your statements though. As long as you don't go crazy with it (like on my current project), sane maintainers are happy to have descriptive self-documenting symbols.
– Aaron
2 days ago
|
show 5 more comments
8
I'd like to add that I can't really think of any downside to naming a methodFindByID
instead ofFind
, there's been many times where I got annoyed with an API that had vague methods likeFind
, but I've never said "FindByID
is too verbose, I really wish the API would have been more vague" (assuming that your method isn't 50 characters long or something, don't doFindByCustomerIDInUnitedStatesOnATuesdayWhenThePrinterIsOutOfInkAndWeRanOutOfCoffee
).
– jrh
2 days ago
1
Also... I know that parameter overloading is a language feature, but IMO it should be used responsibly, functions that make use of it should be rather similar (and more importantly, throw similar exceptions). I guess I would prefer that overloaded methods just allow me to be more or less specific (also yes, I know that default parameters accomplish most of this), it's a judgement call but I'd recommend erring on the side of making a new method instead of making yet another overload, partially because browsing and referring to the documentation for an overloaded function is harder.
– jrh
2 days ago
10
Why is brevity "extremely compelling"? Will you die younger if you type those extra four letters?
– Eric Lippert
2 days ago
4
@EricLippert: Each of the last few C# versions added a few features that don't add any new functionality, just improved code brevity. I imagine those were deemed a priority for the same reason.
– BlueRaja - Danny Pflughoeft
2 days ago
2
@jrh I did not mean to imply that the very short names are acceptable; I generally do not appreciate abbreviated symbols. I meant that I actually do have to deal withFindByCustomerIDInUnitedStatesOnATuesdayWhenThePrinterIsOutOfInkAndWhen...
often on my current project, as there are a lot of those. It's awful. Just offering up an anecdote where someone actually has to deal with that. I agree with your statements though. As long as you don't go crazy with it (like on my current project), sane maintainers are happy to have descriptive self-documenting symbols.
– Aaron
2 days ago
8
8
I'd like to add that I can't really think of any downside to naming a method
FindByID
instead of Find
, there's been many times where I got annoyed with an API that had vague methods like Find
, but I've never said "FindByID
is too verbose, I really wish the API would have been more vague" (assuming that your method isn't 50 characters long or something, don't do FindByCustomerIDInUnitedStatesOnATuesdayWhenThePrinterIsOutOfInkAndWeRanOutOfCoffee
).– jrh
2 days ago
I'd like to add that I can't really think of any downside to naming a method
FindByID
instead of Find
, there's been many times where I got annoyed with an API that had vague methods like Find
, but I've never said "FindByID
is too verbose, I really wish the API would have been more vague" (assuming that your method isn't 50 characters long or something, don't do FindByCustomerIDInUnitedStatesOnATuesdayWhenThePrinterIsOutOfInkAndWeRanOutOfCoffee
).– jrh
2 days ago
1
1
Also... I know that parameter overloading is a language feature, but IMO it should be used responsibly, functions that make use of it should be rather similar (and more importantly, throw similar exceptions). I guess I would prefer that overloaded methods just allow me to be more or less specific (also yes, I know that default parameters accomplish most of this), it's a judgement call but I'd recommend erring on the side of making a new method instead of making yet another overload, partially because browsing and referring to the documentation for an overloaded function is harder.
– jrh
2 days ago
Also... I know that parameter overloading is a language feature, but IMO it should be used responsibly, functions that make use of it should be rather similar (and more importantly, throw similar exceptions). I guess I would prefer that overloaded methods just allow me to be more or less specific (also yes, I know that default parameters accomplish most of this), it's a judgement call but I'd recommend erring on the side of making a new method instead of making yet another overload, partially because browsing and referring to the documentation for an overloaded function is harder.
– jrh
2 days ago
10
10
Why is brevity "extremely compelling"? Will you die younger if you type those extra four letters?
– Eric Lippert
2 days ago
Why is brevity "extremely compelling"? Will you die younger if you type those extra four letters?
– Eric Lippert
2 days ago
4
4
@EricLippert: Each of the last few C# versions added a few features that don't add any new functionality, just improved code brevity. I imagine those were deemed a priority for the same reason.
– BlueRaja - Danny Pflughoeft
2 days ago
@EricLippert: Each of the last few C# versions added a few features that don't add any new functionality, just improved code brevity. I imagine those were deemed a priority for the same reason.
– BlueRaja - Danny Pflughoeft
2 days ago
2
2
@jrh I did not mean to imply that the very short names are acceptable; I generally do not appreciate abbreviated symbols. I meant that I actually do have to deal with
FindByCustomerIDInUnitedStatesOnATuesdayWhenThePrinterIsOutOfInkAndWhen...
often on my current project, as there are a lot of those. It's awful. Just offering up an anecdote where someone actually has to deal with that. I agree with your statements though. As long as you don't go crazy with it (like on my current project), sane maintainers are happy to have descriptive self-documenting symbols.– Aaron
2 days ago
@jrh I did not mean to imply that the very short names are acceptable; I generally do not appreciate abbreviated symbols. I meant that I actually do have to deal with
FindByCustomerIDInUnitedStatesOnATuesdayWhenThePrinterIsOutOfInkAndWhen...
often on my current project, as there are a lot of those. It's awful. Just offering up an anecdote where someone actually has to deal with that. I agree with your statements though. As long as you don't go crazy with it (like on my current project), sane maintainers are happy to have descriptive self-documenting symbols.– Aaron
2 days ago
|
show 5 more comments
up vote
9
down vote
Another way of thinking about this is to use the type safety of the language.
You can implement a method such as:
Find(FirstName name);
Where FirstName is a simple object that wraps a string which contains the first name and means there can be no confusion as to what the method is doing, nor in the arguments with which it is called.
New contributor
4
Not sure what your answer is to the OPs question. Do you recommend to use a name like "Find" by relying on the type of the argument? Or do you recommend to use such names only when there is an explicit type for the argument(s), and use a more explicit name like "FindById" elsewhere? Or do you recommend to introduce explicit types to make a name like "Find" more feasible?
– Doc Brown
2 days ago
2
@DocBrown I think the latter, and I like it. It's actually similar to Peter's answer, iiuc. The rationale as I understand it is two-fold: (1) It's clear from the argument type what the function does; (2) You cannot make mistakes likestring name = "Smith"; findById(name);
.which is possible if you use non-descript general types.
– Peter A. Schneider
2 days ago
5
While in general I am a fan of compile time type safety, be careful with wrapper types. Introducing wrapper classes for the sake of type safety can at times severely complicate your API if done in excess. e.g., the whole "artist formerly known as int" problem that winapi has; in the long run I would say most people just look at the endlessDWORD
LPCSTR
etc. clones and think "it's an int / string / etc.", it gets to the point where you spend more time propping up your tools than you do actually designing code.
– jrh
2 days ago
1
@jrh True. My litmus test for introducing "nominal" types (differing only in name) is when common functions/methods don't make any sense in our use case, e.g.int
s are often summed, multiplied, etc. which is meaningless for IDs; so I'd makeID
distinct fromint
. This can simplify an API, by narrowing down what we can do given a value (e.g. if we have anID
, it will only work withfind
, not e.g.age
orhighscore
). Conversely, if we find ourselves converting a lot, or writing the same function/method (e.g.find
) for multiple types, that's a sign that our distinctions are too fine
– Warbo
2 days ago
add a comment |
up vote
9
down vote
Another way of thinking about this is to use the type safety of the language.
You can implement a method such as:
Find(FirstName name);
Where FirstName is a simple object that wraps a string which contains the first name and means there can be no confusion as to what the method is doing, nor in the arguments with which it is called.
New contributor
4
Not sure what your answer is to the OPs question. Do you recommend to use a name like "Find" by relying on the type of the argument? Or do you recommend to use such names only when there is an explicit type for the argument(s), and use a more explicit name like "FindById" elsewhere? Or do you recommend to introduce explicit types to make a name like "Find" more feasible?
– Doc Brown
2 days ago
2
@DocBrown I think the latter, and I like it. It's actually similar to Peter's answer, iiuc. The rationale as I understand it is two-fold: (1) It's clear from the argument type what the function does; (2) You cannot make mistakes likestring name = "Smith"; findById(name);
.which is possible if you use non-descript general types.
– Peter A. Schneider
2 days ago
5
While in general I am a fan of compile time type safety, be careful with wrapper types. Introducing wrapper classes for the sake of type safety can at times severely complicate your API if done in excess. e.g., the whole "artist formerly known as int" problem that winapi has; in the long run I would say most people just look at the endlessDWORD
LPCSTR
etc. clones and think "it's an int / string / etc.", it gets to the point where you spend more time propping up your tools than you do actually designing code.
– jrh
2 days ago
1
@jrh True. My litmus test for introducing "nominal" types (differing only in name) is when common functions/methods don't make any sense in our use case, e.g.int
s are often summed, multiplied, etc. which is meaningless for IDs; so I'd makeID
distinct fromint
. This can simplify an API, by narrowing down what we can do given a value (e.g. if we have anID
, it will only work withfind
, not e.g.age
orhighscore
). Conversely, if we find ourselves converting a lot, or writing the same function/method (e.g.find
) for multiple types, that's a sign that our distinctions are too fine
– Warbo
2 days ago
add a comment |
up vote
9
down vote
up vote
9
down vote
Another way of thinking about this is to use the type safety of the language.
You can implement a method such as:
Find(FirstName name);
Where FirstName is a simple object that wraps a string which contains the first name and means there can be no confusion as to what the method is doing, nor in the arguments with which it is called.
New contributor
Another way of thinking about this is to use the type safety of the language.
You can implement a method such as:
Find(FirstName name);
Where FirstName is a simple object that wraps a string which contains the first name and means there can be no confusion as to what the method is doing, nor in the arguments with which it is called.
New contributor
New contributor
answered 2 days ago
3DPrintScanner
1071
1071
New contributor
New contributor
4
Not sure what your answer is to the OPs question. Do you recommend to use a name like "Find" by relying on the type of the argument? Or do you recommend to use such names only when there is an explicit type for the argument(s), and use a more explicit name like "FindById" elsewhere? Or do you recommend to introduce explicit types to make a name like "Find" more feasible?
– Doc Brown
2 days ago
2
@DocBrown I think the latter, and I like it. It's actually similar to Peter's answer, iiuc. The rationale as I understand it is two-fold: (1) It's clear from the argument type what the function does; (2) You cannot make mistakes likestring name = "Smith"; findById(name);
.which is possible if you use non-descript general types.
– Peter A. Schneider
2 days ago
5
While in general I am a fan of compile time type safety, be careful with wrapper types. Introducing wrapper classes for the sake of type safety can at times severely complicate your API if done in excess. e.g., the whole "artist formerly known as int" problem that winapi has; in the long run I would say most people just look at the endlessDWORD
LPCSTR
etc. clones and think "it's an int / string / etc.", it gets to the point where you spend more time propping up your tools than you do actually designing code.
– jrh
2 days ago
1
@jrh True. My litmus test for introducing "nominal" types (differing only in name) is when common functions/methods don't make any sense in our use case, e.g.int
s are often summed, multiplied, etc. which is meaningless for IDs; so I'd makeID
distinct fromint
. This can simplify an API, by narrowing down what we can do given a value (e.g. if we have anID
, it will only work withfind
, not e.g.age
orhighscore
). Conversely, if we find ourselves converting a lot, or writing the same function/method (e.g.find
) for multiple types, that's a sign that our distinctions are too fine
– Warbo
2 days ago
add a comment |
4
Not sure what your answer is to the OPs question. Do you recommend to use a name like "Find" by relying on the type of the argument? Or do you recommend to use such names only when there is an explicit type for the argument(s), and use a more explicit name like "FindById" elsewhere? Or do you recommend to introduce explicit types to make a name like "Find" more feasible?
– Doc Brown
2 days ago
2
@DocBrown I think the latter, and I like it. It's actually similar to Peter's answer, iiuc. The rationale as I understand it is two-fold: (1) It's clear from the argument type what the function does; (2) You cannot make mistakes likestring name = "Smith"; findById(name);
.which is possible if you use non-descript general types.
– Peter A. Schneider
2 days ago
5
While in general I am a fan of compile time type safety, be careful with wrapper types. Introducing wrapper classes for the sake of type safety can at times severely complicate your API if done in excess. e.g., the whole "artist formerly known as int" problem that winapi has; in the long run I would say most people just look at the endlessDWORD
LPCSTR
etc. clones and think "it's an int / string / etc.", it gets to the point where you spend more time propping up your tools than you do actually designing code.
– jrh
2 days ago
1
@jrh True. My litmus test for introducing "nominal" types (differing only in name) is when common functions/methods don't make any sense in our use case, e.g.int
s are often summed, multiplied, etc. which is meaningless for IDs; so I'd makeID
distinct fromint
. This can simplify an API, by narrowing down what we can do given a value (e.g. if we have anID
, it will only work withfind
, not e.g.age
orhighscore
). Conversely, if we find ourselves converting a lot, or writing the same function/method (e.g.find
) for multiple types, that's a sign that our distinctions are too fine
– Warbo
2 days ago
4
4
Not sure what your answer is to the OPs question. Do you recommend to use a name like "Find" by relying on the type of the argument? Or do you recommend to use such names only when there is an explicit type for the argument(s), and use a more explicit name like "FindById" elsewhere? Or do you recommend to introduce explicit types to make a name like "Find" more feasible?
– Doc Brown
2 days ago
Not sure what your answer is to the OPs question. Do you recommend to use a name like "Find" by relying on the type of the argument? Or do you recommend to use such names only when there is an explicit type for the argument(s), and use a more explicit name like "FindById" elsewhere? Or do you recommend to introduce explicit types to make a name like "Find" more feasible?
– Doc Brown
2 days ago
2
2
@DocBrown I think the latter, and I like it. It's actually similar to Peter's answer, iiuc. The rationale as I understand it is two-fold: (1) It's clear from the argument type what the function does; (2) You cannot make mistakes like
string name = "Smith"; findById(name);
.which is possible if you use non-descript general types.– Peter A. Schneider
2 days ago
@DocBrown I think the latter, and I like it. It's actually similar to Peter's answer, iiuc. The rationale as I understand it is two-fold: (1) It's clear from the argument type what the function does; (2) You cannot make mistakes like
string name = "Smith"; findById(name);
.which is possible if you use non-descript general types.– Peter A. Schneider
2 days ago
5
5
While in general I am a fan of compile time type safety, be careful with wrapper types. Introducing wrapper classes for the sake of type safety can at times severely complicate your API if done in excess. e.g., the whole "artist formerly known as int" problem that winapi has; in the long run I would say most people just look at the endless
DWORD
LPCSTR
etc. clones and think "it's an int / string / etc.", it gets to the point where you spend more time propping up your tools than you do actually designing code.– jrh
2 days ago
While in general I am a fan of compile time type safety, be careful with wrapper types. Introducing wrapper classes for the sake of type safety can at times severely complicate your API if done in excess. e.g., the whole "artist formerly known as int" problem that winapi has; in the long run I would say most people just look at the endless
DWORD
LPCSTR
etc. clones and think "it's an int / string / etc.", it gets to the point where you spend more time propping up your tools than you do actually designing code.– jrh
2 days ago
1
1
@jrh True. My litmus test for introducing "nominal" types (differing only in name) is when common functions/methods don't make any sense in our use case, e.g.
int
s are often summed, multiplied, etc. which is meaningless for IDs; so I'd make ID
distinct from int
. This can simplify an API, by narrowing down what we can do given a value (e.g. if we have an ID
, it will only work with find
, not e.g. age
or highscore
). Conversely, if we find ourselves converting a lot, or writing the same function/method (e.g. find
) for multiple types, that's a sign that our distinctions are too fine– Warbo
2 days ago
@jrh True. My litmus test for introducing "nominal" types (differing only in name) is when common functions/methods don't make any sense in our use case, e.g.
int
s are often summed, multiplied, etc. which is meaningless for IDs; so I'd make ID
distinct from int
. This can simplify an API, by narrowing down what we can do given a value (e.g. if we have an ID
, it will only work with find
, not e.g. age
or highscore
). Conversely, if we find ourselves converting a lot, or writing the same function/method (e.g. find
) for multiple types, that's a sign that our distinctions are too fine– Warbo
2 days ago
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsoftwareengineering.stackexchange.com%2fquestions%2f382026%2fis-it-enough-for-methods-to-be-distinguished-just-by-argument-name-not-type%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
So when you overload Find to include
T Find<T>(string name)
or(int size)
how do you plan to resolve the inevitable problems?– UKMonkey
2 days ago
2
@UKMonkey what inevitable problems?
– Konrad
2 days ago
3
in the first case: if multiple entries have the same name then you would have to change the function signature; which means people will likely get confused with what it's meant to return; In the latter case, the argument is the same - and thus an illegal overload. You either start naming the function with "byX" or make an object for the argument so that you can have the equivalent of overload with same argument. Either works well for different situations.
– UKMonkey
2 days ago
2
@UKMonkey you can post an answer with some code examples if you want
– Konrad
2 days ago
2
Ids should probably be an opaque
ID
object and not just anint
. In that way get compile-time checking that you do not use an id for an int or viceversa in some part of your code. And with that you can havefind(int value)
andfind(ID id)
.– Bakuriu
2 days ago