Naming Convention help - Beyond the basics?

P

Phillip Conrad

Here is a little problem I've run into, and none of the naming
conventions have helped... Ever since I switched from C to C# and
FxCop, I've going crazy trying to fix some style issues.

I have 3 major issues...

---------

Let's say I have a class called Parameter.

In another class, I want to define a method taking a Parameter.

public void Foo(Parameter ???) {}

The problem is, I'm getting writer's block trying to come up with what
to call the passed variable!

Here's a poor style solution - you have a variable that's one case off
from the class:

public void Foo(Parameter parameter) {}

Here's another poor style solution - you have a variable that's an
abbreviation, and FxCop hates that:

public void Foo(Parameter param) {}

So... What do I call it? Are there generic names that people typically
use, such as "item" or "value"? Are there lists of generic names out
there?

---------

On a similar note... Let's say I have a constructor and a property...

private string _name;
public string Name {get {return _name;} set{_name = value;}}
public Foo(string name)
{
Name = name;
}

I know this is bad form because you have a single case difference, but
I can't always think of a better way of naming things. What can I do?

---------

Finally...

public void IsValidString(string ???)

I want to run a test on a string... What is a good name to call the
passed variable? "testString" won't work, because that's a holdover to
Hungarian notation. "test" sounds too banal... Any suggestions?
 
D

dkode

Just make sure you use the same technique for casing every time.

I also always keep global variables like so:
MyClass _myClass;

Local variables are the same, except no underscore.

When I have an array i'm naming, i just try to pluralize whatever word
it is. If i cant pluralize the word, then i try to find a similar word
that i can. This way i know there is more than one.

I try not to append datatypes onto the variable name, this just adds
extra typing when you can easily mouseover the variable (if in an IDE)
and see what its type is.

thats my 2 cents
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Phillip said:
Let's say I have a class called Parameter.

In another class, I want to define a method taking a Parameter.

public void Foo(Parameter ???) {}

The problem is, I'm getting writer's block trying to come up with what
to call the passed variable!

Here's a poor style solution - you have a variable that's one case off
from the class:

public void Foo(Parameter parameter) {}

Here's another poor style solution - you have a variable that's an
abbreviation, and FxCop hates that:

public void Foo(Parameter param) {}

So... What do I call it? Are there generic names that people typically
use, such as "item" or "value"? Are there lists of generic names out
there?

I would use p or param.

Methods should not be longer than you can easily read the entire
method and then it should not matter much what the name is.

If your project has a coding convention follow that. Always.
On a similar note... Let's say I have a constructor and a property...

private string _name;
public string Name {get {return _name;} set{_name = value;}}
public Foo(string name)
{
Name = name;
}

I know this is bad form because you have a single case difference, but
I can't always think of a better way of naming things. What can I do?

I belive that:

private string name;
public string Name {get {return name;} set{name = value;}}
public Foo(string name)
{
this.name = name;
}

is very widely used.

Not very pretty. But any C# programmer should recognize it.
public void IsValidString(string ???)

I want to run a test on a string... What is a good name to call the
passed variable? "testString" won't work, because that's a holdover to
Hungarian notation. "test" sounds too banal... Any suggestions?

public void IsValidString(string s)

Programmers are not stupid. And in a 10-25 line method they are
capable of remembering the name of the parameter.

Arne
 
J

John Plummer

If Parameter has a specific purpose or type you could prefix it with an
adjective, otherwise I would go with parameter.

I think differentiating by case is really only an issue if the items you are
differentiating are externally visible (so they can be used by languages
that are not case sensitive). Whilst a user may see 'parameter' in
intellisense it should not cause any conflicts.

This applies to your second point also.

I generally use camel case names for member level variables except when the
variable is the private field of a property then I prefix with an underscore
(with the exception of constructors and private setter methods for read only
properties, I should not see an underscore in a method).

I used to use underscore prefixing for all private fields, but then do you
extend it to fields that are classes? Do you then prefix event handlers from
those fields...

Lastly your method name sounds like it is testing to see if the parameter is
a valid string (which it will be if the paramter is a string) you probably
want to test that it is valid as something else (isValidEmail,
isValidName...). As a last resort use isValidText which would lead to a
parameter of text or textToTest.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top