English 中文(简体)
How can I best avoid using reserved or key words in my language or framework? [closed]
原标题:

Closed. This question needs details or clarity. 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.

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.





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签