Getters and setters are special, you read the Get prefix, know it's a getter, know that it should not modify any state, and what comes after the Get is the name of the property, which is a noun.
I'm working under the assumption that, by and large, a function should not change state, should be idempotent. It's good to aim for that but it's not always possible.
Consider a method of a string that trimms whitespace, does not modify the subject, and returns a new value. Should it be:
str.Trim();
or
str.Trimmed();
Personally I prefer the latter.
Jeroen Wiert Pluimers is quite right, this is not a noun/verb distinction. Clearly my grasp of grammar, even in my mother tongue, is weak. Scratch what I said about nouns.
One of the most underrated and yet very important tasks at the start of a project - is to establish a dictionary of the vocabulary for the problem domain. Firstly to secure a common understanding of what things are, and secondly to create a base for good naming conventions based on something like Cwalina's guidelines.
David Heffernan I agree for methods, but not for free functions. But I can't give you a good reason, just the way I feel. I guess for free functions I think in terms of math where parameter is not mutated.
I think it's pretty clear (;
ReplyDeleteSo which kind(s) of bra does it strip?
ReplyDeleteAnswer will be later today.
I wonder if it should return a bra instead of a string :D
ReplyDeleteMaybe it should return a thread instead of a string?
ReplyDeleteI'm not linking any pictures, but getStringFromObject() from Java comes to my mind.
ReplyDeleteIt's for Bra–ket notation of course. :-)
ReplyDeleteUli Gerhardt So it strips only the left side?
ReplyDeleteUwe Raabe We'd need to see more to be sure. :-)
ReplyDeleteClearly it strips the bra from a bra-ket pair. So pass it '' and it'll return the ket '|b>'. I mean, what else could it possibly do?
ReplyDeleteAsbjørn Heid Exactly!
ReplyDeleteAt least it wasn't
ReplyDeletefunction StripBra( gString:string ):string;
Agreed, for instance, I've never been able to remember the 'Contnrs' unit file in which TObjectList resides :P
ReplyDeleteShouldn't return anything since its a verb. Function names should be nouns.
ReplyDeleteDavid Heffernan BraStripper?
ReplyDeleteDavid Heffernan Getter and Setter?
ReplyDeleteLars Fosdal Stripped
ReplyDeleteDavid Heffernan Curious -- I've never heard anyone say this. I'd argue that functions should be verbs. Why should they be nouns?
ReplyDeleteNot being well at natural languages: Isn't "Stripped" just a specific tense of a verb?
ReplyDeleteNick Hodges IIRC - My favorite book for code conventions uses this argument. I wonder if Code Complete 2 does as well?
ReplyDeleteamazon.com - Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries (2nd Edition): Krzysztof Cwalina, Brad Abrams: 9780321545619: Amazon.com: Books
Getters and setters are special, you read the Get prefix, know it's a getter, know that it should not modify any state, and what comes after the Get is the name of the property, which is a noun.
ReplyDeleteI'm working under the assumption that, by and large, a function should not change state, should be idempotent. It's good to aim for that but it's not always possible.
Consider a method of a string that trimms whitespace, does not modify the subject, and returns a new value. Should it be:
str.Trim();
or
str.Trimmed();
Personally I prefer the latter.
Jeroen Wiert Pluimers is quite right, this is not a noun/verb distinction. Clearly my grasp of grammar, even in my mother tongue, is weak. Scratch what I said about nouns.
I wonder if my favorite now is
ReplyDeletefunction Topless( aStr:string):String;
I find the tenor of many of these comments unfit for this community. All non technical comments here are not appropriate.
ReplyDeleteDavid Heffernan You are killing all the fun :)
ReplyDeleteOne of the most underrated and yet very important tasks at the start of a project - is to establish a dictionary of the vocabulary for the problem domain. Firstly to secure a common understanding of what things are, and secondly to create a base for good naming conventions based on something like Cwalina's guidelines.
ReplyDeleteDavid Heffernan But your Trimmed() is a method, not a free function. Would you still call it Trimmed() if it was a free function?
ReplyDeleteI think I would. Otherwise it seems like it would mutate the subject.
ReplyDeleteDavid Heffernan I agree for methods, but not for free functions. But I can't give you a good reason, just the way I feel. I guess for free functions I think in terms of math where parameter is not mutated.
ReplyDeleteAsbjørn Heid I would always write Sum and Product rather than Add and Multiply, but there you go
ReplyDeleteDavid Heffernan You must be a lot of fun at parties
ReplyDeleteOr perhaps my idea of fun differs from yours
ReplyDeleteDavid Heffernan I'm pretty sure there's an ISO standard for what constitutes as fun.
ReplyDeleteThe definition of fun is primarily opinion-based ...
ReplyDelete