views:

313

answers:

8

Naming things is hard. When naming my classes, variables, and methods I strive to avoid collisions with reserved words or keywords in my languages (MSSQL, VB.NET or C#, HTML, CSS, jQuery) and framework (.NET).

Too often I make mistakes and don't realize it until it's too late to easily go back and rename.

A scenario: (1) first create a database schema and then (2) realize a table or column name isn't a good choice for a C#/.NET object.

What techniques do you use?

+14  A: 

A decent IDE...

Anon.
As trite as this answer is, it is still correct. You should have syntax coloring and some sort of refactoring functionality on most IDEs.
Andres
a.k.a. "the compiler"
matt b
A scenario: (1) first create a database schema and then (2) realize a table or column name isn't good for a C#/.NET object.
Zack Peterson
I assume words like 'User' may be risky as it's part of the IPrinciple interface. Therefore, you could have tbUser, UserTable etc... thus eliminating the risk of collision
keyboardP
+2  A: 

I've never run into this problem, even in larger projects. There are standard conventions I follow such as prefixing variable name with their types. I also make use of namespaces to ensure I get the exact object/method I want. To be honest, I fail to see where the difficulty lies in naming things that aren't reserved.

keyboardP
+3  A: 

Nearly any editor and IDE will automatically highlight keywords in an appropriate (contrasting with other symbols) color, provided that it's aware of the language syntax of the code you're editing.

If you don't have access to this technology, as in the olden days, your best bet was to memorize all of the keywords, and keep a cheat sheet handy. But, really, your editor should do it.

Adam Bellaire
+1  A: 

Go for more clarity.

C# Example

class

cssClass

SQL Server Example

Update

PerformUpdate

ChaosPandion
+4  A: 

If you're using Visual Studio or SQL Server Management Studio then they'll highlight reserved keywords with a different colour.

Some languages have a special syntax for using reserved keywords if you really need to, although it's not really recommended. For example, in C# you can prefix with an at sign, e.g.

var @class = "Hello";

Or in SQL Server you can use square brackets:

SELECT [where] from Locations;
cxfx
Jay
I can't believe I just did that. Fixed. [Shame]
cxfx
+1  A: 

IDEs can help.

A good way is just to learn the syntax of the language you are using. Usually you can find a list of reserved keywords for the languages.

Testing dynamic languages like JavaScript regularly will expose problems with keywords pretty fast. (Since I haven't yet got eclipse to work well with javascript).

Ivan
+2  A: 

I've really rarely have had problems with clashing with keywords: If you name a list List, then, well, you deserve getting bitten by that because you should have used a more meaningful variable name...

Of course, the point goes moot if you're building a compiler and you actually need a Class class... but that's rarely the case.

In nearly every other case, there's no point for List list, when List items or List tokens makes sense.

Tordek
+1  A: 

Use structured programming!

This means variables within different scopes should only exist and only relate to that scope. When you want to use a global variable, explicitly do so.

If areas of the program cannot be segmented into a functions, then segment areas into classes. That way AreaX->ID does not collide with AreaY->ID. Classes can also get around some reserved words.

For a few of the reserved words you will just have to try to avoid generic names. I know $HOME is particularly perilous in PHP.

Jeff Davis