Iraimbilanja
Iraimbilanja

Reputation:

Naming a count variable

what's better? nNodes or nodeCount

Upvotes: 16

Views: 16301

Answers (8)

Markus Hettich
Markus Hettich

Reputation: 574

It depends, but i would choose the second one (nodeCount). If your project have defined a special convention you should follow this convention or discuss the convention with your team. It also could depend on the domain of your application. It could be ok to follow general accepted notations in your domain.

There are some rules which could help you to do decisions like this.

If something could be misunderstood it will be misunderstood by someone. Eliminate the possiblity of misunderstanding. The chance of misunderstanding is way less in the second suggestion nodeCount. Everyone who speaks english should understand it. The first suggestion on the other side depends on the understanding of the n prefix.

Write the best describing noun at the end. Is it more a count or more a collection of nodes ? For further use of the variable it is more important to understand that it is a count (a number) then what is counted. Therefore nNodes is more bad then nodeCount.

Follow basic programming principles like KISS (Keep it simple, stupid) or "Don’t write clever code. Write 'embarrassingly obvious' code." Ask your mother if she understand your code. If she did then you have written good code :)

Upvotes: 2

BartB
BartB

Reputation: 116

Possible names:

  • count
  • size
  • length

Upvotes: 1

Niyaz
Niyaz

Reputation: 54793

numNodes ?

Upvotes: 2

Franci Penov
Franci Penov

Reputation: 76001

The first one in C++, the second one in C#/Java.

Though this is really very subjective question and should be answered by your team/company guidelines.

Upvotes: 2

William Brendel
William Brendel

Reputation: 32189

It really depends on the accepted naming conventions of whichever language you are using, but nodeCount is more readable. If you are using Java, you can use the Java Naming Conventions guide.

Upvotes: 4

Brian
Brian

Reputation: 118865

Whichever is consistent with the rest of your code base / style guide / dev team.

Upvotes: 4

paxdiablo
paxdiablo

Reputation: 881633

The second, since it reads properly.

Upvotes: 20

Hosam Aly
Hosam Aly

Reputation: 42453

This is simply a style issue. I prefer nodeCount because I find it clearer. nNodes is more similar to Hungarian notation, but doesn't say it's a count variable.

Upvotes: 16

Related Questions