Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 440
mike g Avatar asked Nov 30 '22 12:11

mike g


2 Answers

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!
like image 91
Jon Skeet Avatar answered Feb 01 '23 10:02

Jon Skeet


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.

like image 24
Paul W Homer Avatar answered Feb 01 '23 10:02

Paul W Homer