views:

183

answers:

6

Just curious how you would comment this line of code:

string customerNm = customerNm.EndsWith("s") ? customerNm+= "'" : customerNm+="'s";
+1  A: 
// Enforce English grammar
string customerNm = customerNm.EndsWith("s") ? customerNm+= "'" : customerNm+="'s";
Developer Art
Thanks for the refactoring. neat
bizl
+3  A: 

I would not necessarily comment it at all. It's clear enough as-is.

If you do comment it, you should explain why you do it the way you do.

E.g.:

/* Don't add "'s" for names ending on "s" 
  (request by Important Customer in June 1978) */
sleske
All comments, other than those for a public API, should answer the "why" question. I can read the code to find out what you did, but I want to know why you did it this way, or even did it at all.
Thomas Owens
+4  A: 

Put it into its own function, and name the function appropriately. That should be clear enough. (And you can test it more easily, too!)

string makePossessive(string customerName) {
    ...
}
Rob Hruska
I second this approach. Now, it's unit-testable.
yodaj007
Though am on fan of one-line functions, I see the reasoning. Thanks
bizl
I'll agree with you, one-line functions can be odd. However, some people also don't prefer the conditional operator because it can make things less readable. If you expanded your statement out into an if/else, it wouldn't be a one-liner anymore.
Rob Hruska
that makes sense!
bizl
+1  A: 

//Apply genitive case
string customerNm = customerNm.EndsWith("s") ? customerNm+= "'" : customerNm+="'s";
Rafael Romão
+3  A: 

I would comment it with "not working". Because you access 'customerNm' before you assign to it.

(You use "customerNm+=..." which means: create a new string instance as a concatenation of the old instance [which isn't assigned yet] and ...).

This shouldn't even get compiled.

I think what you mean is:

customerNm += customerNm.EndsWith("s") ? "'" : "'s";

where customerNm is a string already assigned to once before.

rstevens
+1  A: 

Not the question you're asking, but it looks like your code violates grammar rules.

A name should not be treated like a plural noun just because it ends in 's'. For instance, if James has a dog, it's James's dog, not James' dog. However, if two life partners named Mike have a dog, it's both Mikes' dog.

Exception: if a multisyllabic name ends in as "ess" or "ezz" sound, than it can be treated like a plural ending in 's'. If Linus has a dog, it can be Linus' dog, although I believe Linus's is also acceptable.

P Daddy
Interesting. Thanks for the link
bizl
LOL! I corrected your grammar with poor grammar! Last paragraph, "as" instead of "an" and "than" instead of "then". I'm gonna leave it. For the irony.
P Daddy