开发者

How can I best avoid using reserved or key words in my language or framework? [closed]

开发者 https://www.devze.com 2022-12-14 02:46 出处:网络
Closed. This question needs details or cl开发者_开发百科arity. It is not currently accepting answers.
Closed. This question needs details or cl开发者_开发百科arity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 2 years ago.

Improve this question

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?


A decent IDE...


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;


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.


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.


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.


Go for more clarity.

C# Example

class

cssClass

SQL Server Example

Update

PerformUpdate


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).


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号