aradhak
aradhak

Reputation: 836

naming static int variables

I have quite a number of integer variables (most of them static, some non-static) in my code. Since am working on ajax testing, getting rid of these variables is not possible. Currently I use "int" prefixed to a word that matches the context where the variable is used. But too much of that makes my code look odd. For eg. I need to press cancel button whose id gets incremented with each run. So am using a static variable and I named it

static int intCancel;

Is there any particular pattern in which the variables can be named so as to make the code more readable?

Upvotes: 3

Views: 1846

Answers (3)

PeteH
PeteH

Reputation: 2454

In the C# world, there are tools such as Resharper and even Microsoft's Visual Studio itself which are quite happy to impose naming conventions upon you. This is a great approach because it means that everybody cranks out consistent-looking code, and a lot of the "subjectivity" is taken out.

Now Resharper and Visual Studio both apply different rules (to each other), but that's not really the point. The point is that you pick a convention and you stick with it. Be it camel case (which is what I typically use these days, and which is what I'd recommend to you if you're stuck for choice) or hungarian (which I would have used fifteen years ago), or whatever. Just pick one and be consistent, this way your code is more maintainable, and that's the holy grail imo.

I agree with the earlier comments about naming things appropriately (abbreviating names or making them obscure is just lazy), and by all means use enums where appropriate (although I suspect you just gave that as an example). But whatever you do, pick a convention and be consistent.

Upvotes: 1

raven1981
raven1981

Reputation: 1445

The Hungarian notation is a standard for naming variables:

http://en.wikipedia.org/wiki/Hungarian_notation

In your case would be:

static int iCancel

Upvotes: 0

John B
John B

Reputation: 32969

A common misconception is that variable names must be short. Consider expanding them to state what they are. IDEs make completing variable names easy. So numberTimesCancelSelected might be an option.

Consider reading Clean Code for a better discussion of this topic.

Upvotes: 5

Related Questions