views:

218

answers:

7

Howdy all,

here is two samples with two different approaches to naming variables:

decimal amountDue = 1000;
decimal amountPaid = 800;

vs.

decimal dueAmount = 1000;
decimal paidAmount = 800;

Which one would you usually prefer and why?

+1  A: 

The first one (amountDue) means typing seven characters before getting useful intellisense. I'd opt for number two.

Damien_The_Unbeliever
don't base this off of intellisense, base it off read ability and which makes the most sense in a sentence.
Nick Berardi
That's your perogative (deciding that the ideal criteria is which reads best in a sentence). The OP provided no such criteria, and in fact asked why we have chosen ours. I prefer discoverability.
Damien_The_Unbeliever
Well, I have never assumed IntelliSence when naming my variables, but why not...
SeasonedCoder
+1  A: 

"Paid" has several attributes: paidAmount, paidDate, paidBy, paidTo, etc.

"Amount" is the data type (essentially currency or BigDecimal or whatever your language uses) and doesn't mean much.

S.Lott
And therefore...?
Dominic Rodger
And therefore, those names have the distinguishing part first and the uninteresting data type second. And therefore, names with the interesting part first are slightly easier to work with. And therefore, those names should be used.
S.Lott
A: 

http://wordsmith.org/anagram/anagram.cgi?anagram=amount+paid&t=1000

KristoferA - Huagati.com
Huh? Am I missing the funny anagram?
Dominic Rodger
Hopefully, it's not a "Damn Utopia" to come to some conclusion ;)
SeasonedCoder
+9  A: 

You should use which ever one reads better as an english sentence. Such as the amount due to the customer is 1000 dollars. In my opinion #1 is better. Because if you write the customer is due the amount of 1000 dollars, it breaks up the wording of the actual variable.

Nick Berardi
Agree. It is also easier to remember, and is the first, natural thing that comes to one's mind. Whenever I try to "optimize" the naming by, for example, having all "amount" first to keep them sorted, for whatever reason, I always end up turning them back to the "natural" form, that is: paidAmount.
Jem
+1  A: 

To stir the pot a little (and there's no fun if everyone agrees), I would choose option 1.

  • it indicates to me they are related
  • it's the natural way of saying it.
Lieven
+4  A: 

Whatever is most readable in the given context. I could see this ranging from either of your options to simply "paid" and "due".

For example:

public decimal RemainingAmount( Invoice invoice, int quantity, Coupon[] coupons )
{
     decimal paid = coupons.Sum( c => c.Value );
     decimal due = invoice.Price * quantity;

     return due - paid;
}
tvanfosson
I like your approach. In case of multiple amounts, would you stick to:<pre><code>...return duePrincipal + dueInterest - paidPrincipal - paidInterest;</pre></code>
SeasonedCoder
In that case I would probably go with the most natural sounding when reading it in English: principalDue + interestDue ...
tvanfosson
A: 

As others said, option #1 is better as naming follows how one would use those concepts in a sentence without sounding weird. However, I think you should also pay attention the business domain that you're modeling to name your variables. A concept may be referred to with a very unique name or terminology in a particular business domain that would not sound right when used in a sentence out of that business domain. If this is the case, then I would go with the terminology that the business domain is using so that the code is expressed in business domain terminology. This helps developers getting familiar with the business domain and also makes communications with clients easier as everyone speaks the same language.

For example, in this particular case, if I notice that the business documents and cliens are using due amount instead of amount due in referring to a payment expected, I would go with due amount.

Mehmet Aras