mike g
mike g

Reputation: 1791

What is your system for avoiding keyword naming clashes?

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?

Upvotes: 4

Views: 736

Answers (16)

AndreasT
AndreasT

Reputation: 9801

First of all, most code conventions prevent such a thing from happening.

If not, I usually add a descriptive prose prefix or suffix:

the_class or theClass infix_or (prefix_or(class_param, in_class) , a_class) or_postfix

A practice, that is usually in keeping with every code style advice you can find ("long names don't kill", "Longer variable names don't take up more space in memory, I promise.") Generally, if you think the keyword is the best description, a slightly worse one would be better.

Note that, by the very premise of your question you introduce ambiguity, which is bad for the reader, be it a compiler or human. Even if it is a custom to use class, clazz or klass and even if that custom is not so custom that it is a custom: it takes a word word, precisely descriptive as word may be, and distorts it, effectively shooting w0rd's precision in the "wrd". Somebody used to another w_Rd convention or language might have a few harsh wordz for your wolds.

Most of us have more to say about things than "Flower", "House" or "Car", so there's usually more to say about typeNames, decoratees, class_params, BaseClasses and typeReferences.

This is where my personal code obfuscation tolerance ends:

Never(!!!) rely on scoping or arcane syntax rules to prevent name clashes with "key words". (Don't know any compiler that would allow that, but, these days, you never know...).

Try that and someone will w**d you in the wörd so __rd, Word will look like TeX to you!

Upvotes: 0

Ken
Ken

Reputation:

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

Upvotes: 0

Aaron Maenpaa
Aaron Maenpaa

Reputation: 122910

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.

Upvotes: 0

John MacIntyre
John MacIntyre

Reputation: 13031

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.

Upvotes: 0

allesklar
allesklar

Reputation: 9590

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

Upvotes: 0

too much php
too much php

Reputation: 91028

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.

Upvotes: 0

user29053
user29053

Reputation:

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.

Upvotes: 0

blabla999
blabla999

Reputation: 3200

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.

Upvotes: 0

Paul W Homer
Paul W Homer

Reputation: 2750

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.

Upvotes: 5

Ryan Thames
Ryan Thames

Reputation: 3204

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.

Upvotes: 1

Soviut
Soviut

Reputation: 91555

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

Upvotes: 2

Marc Charbonneau
Marc Charbonneau

Reputation: 40517

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

Upvotes: 2

Paulo Lopes
Paulo Lopes

Reputation: 5801

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

int _int = 0;

Upvotes: 1

mike g
mike g

Reputation: 1791

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

 int dEfault;
 boolean tRansient;
 Class cLass;

Upvotes: -2

Eppz
Eppz

Reputation: 3206

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.

Upvotes: 4

Jon Skeet
Jon Skeet

Reputation: 1500855

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!

Upvotes: 7

Related Questions