views:

282

answers:

15

Typically languages have keywords that you are unable to use directly with the exact same spelling and case for naming things (variables,functions,classes ...) in your program. Yet sometimes a keyword is the only natural choice for naming something. What is your system for avoiding/getting around this clash in your chosen technology?

+7  A: 

I just avoid the name, usually. Either find a different name or change it slightly - e.g. clazz instead of class in C# or Java. In C# you can use the @ prefix, but it's horrible:

int @int = 5; // Ick!
Jon Skeet
...or klass. I will use @class in MVC when setting HTML attributes for the obvious reason.
tvanfosson
argh!! that is horrible
mike g
Yulp...apparently there are 260,000 words in the Oxford English dictionary, I'm sure there must be a suitable candidate so you don't have to use a language keyword :)
Kev
It's awful...but I see it everywhere :(
Ryan Thames
It's become sufficiently well established in Java, that it's practically idiomatic. Sometimes the best word to describe something really *is* class.
Jon Skeet
I have to use @ every now and then when I need to process JSON received from an external source containing C# identifiers
DrJokepu
Also, I really don't recommend misspelling identifiers. You won't remember the right spelling: "Was it clazz? Or klass? Maybe klazz?" - it will get really confusing.
DrJokepu
Can't say it's ever caused a problem. When you're talking about a Java class, what better word is there than "class"?
Jon Skeet
I agree, if it's about a class, it should be called a class. What I meant is I would rather avoid misspelling, I would just use @ instead (or in Java, prefix it with something, like _)
DrJokepu
Also, an other problem with misspelling is that if someone whose first language is not English will ever have to work on your code, it might be a bit confusing to him as he might not realize the written -> pronounced -> written link right away.
DrJokepu
+4  A: 

My system is don't use keywords period!

If I have a function/variable/class and it only seems logical to name it with a keyword, I'll use a descriptive word in front of the keyword.

(adjectiveNoun) format. ie: personName instead of Name where "Name" is a keyword.

Eppz
A: 

My system in Java is to capitalize the second letter of the word, so for example:

 int dEfault;
 boolean tRansient;
 Class cLass;
mike g
yuck ... but whatever works for you I suppose ...
BobbyShaftoe
Do you camel-case everything else?
Michael Myers
huh - that looks very ugly to me (and is actually slowing my reading speed down). Would not be allowed in our company.
blabla999
Yes...For me I found it doesn't 'bleed out' and distract - unlike funky spellings such as 'clazz', and I am too stubborn to rename when I think its the best name.
mike g
Oh too slow, (converting to dvorak here), yes was to the camel-case question.
mike g
+1  A: 

As stated before either change class to clazz in Java/C#, or use some underscore as a prefix, for example

int _int = 0;
Paulo Lopes
In Objective-C world the _ prefix indicates a private instance variable, so it would be confusing to see it used on the stack. I'm not sure how many other languages follow this rule today though.
Marc Charbonneau
wouldn't work in Python, too.
bene
+2  A: 

I just use a more descriptive name. For instance, 'id' becomes identifier, 'string' becomes 'descriptionString,' and so on.

Marc Charbonneau
+2  A: 

In Python I usually use proper namespacing on my modules to avoid name clashes.

import re
re.compile()

instead of:

from re import *
compile()

Sometimes, when I can't avoid keyword name clashes I simply drop the last letter off the name of my variable.

for fil in files:
    pass
Soviut
This is appropriate to avoid clashes between identifiers, but it won't work for keywords.
oefe
+1  A: 

There should be no reason to use keywords as variable names. Either use a more detailed word or use a thesaraus. Capitalizing certain letters of the word to make it not exactly like the keyword is not going to help much to someone inheriting your code later.

Ryan Thames
+4  A: 

There is nothing intrinsically all-encompassing about a keyword, in that it should stop you from being able to name your variables. Since all names are just generalized instances of some type to one degree or another, you can always go up or down in the abstraction to find another useful name.

For example, if your writing a system that tracks students and you want an object to represent their study in a specific field, i.e. they've taken a "class" in something, if you can't use the term directly, or the plural "classes", or an alternative like "studies", you might find a more "instanced" variation: studentClass, currentClass, etc. or a higher perspective: "courses", "courseClass" or a specfic type attribute: dailyClass, nightClass, etc.

Lots of options, you should just prefer the simplest and most obvious one, that's all.

I always like to listen to the users talk, because the scope of their language helps define the scope of the problem, often if you listen long enough you'll find they have many multiple terms for the same underlying things (with only subtle differences). They usually have the answer ...

Paul.

Paul W Homer
A: 

Happy those with a language without ANY keywords...

But joke apart, I think in the seldom situations where "Yet sometimes a keyword is the only natural choice for naming something." you can get along by prefixing it with "my", "do", "_" or similar.

I honestly can't really think of many such instances where the keyword alone makes a good name ("int", "for" and "if" are definitely bad anyway). The only few in the C-language family which might make sense are "continue" (make it "doContinue"), "break" (how about "breakWhenEOFIsreached" or similar ?) and the already mentioned "class" (how about "classOfThingy" ?).

In other words: make the names more reasonable. And always remember: code is WRITTEN only once, but usualy READ very often.

blabla999
A: 

Typically I follow Hungarian Notation. So if, for whatever reason, I wanted to use 'End' as a variable of type integer I would declare it as 'iEnd'. A string would be 'strEnd', etc. This usually gives me some room as far as variables go.

If I'm working on a particular personal project that other people will only ever look at to see what I did, for example, when making an add-on to a game using the UnrealEngine, I might use my initials somewhere in the name. 'DS_iEnd' perhaps.

Dalin Seivewright
A: 

I write my own [vim] syntax highlighters for each language, and I give all keywords an obvious colour so that I notice them when I'm coding. Languages like PHP and Perl use $ for variables, making it a non-issue.

too much php
A: 

Developing in Ruby on Rails I sometime look up this list of reserved words.

allesklar
wow - quite a long list
mike g
A: 

In 15 years of programming, I've rarely had this problem.

One place I can immediately think of, is perhaps a css class, and in that case, I'd use a more descriptive name. So instead of 'class', I might use 'targetClass' or something similar.

John MacIntyre
A: 

In python the generally accepted method is to append an '_'

class -> class_
or -> or_
and -> and_

you can see this exemplified in the operator module.

Aaron Maenpaa
A: 

I switched to a language which doesn't restrict identifier names at all.