Avoiding Language Keyword Conflicts

2019-08-25 02:09发布

How do you guys avoid keyword conflicts in your language?

For example, I'm creating a class (VB 2008) to hold all the configuration variables for some reports we generate. Naturally, one of the variables is "Date". And of course you can't have anything named the same as a keyword. In VB 2008 you do have the option of surrounding a conflicting word with []'s and fix it but I've always seen that as a hack. Any suggestions? What are your names to get around common keywords?

Code to help visualize...

Dim m_Title As String

Dim m_Date As String

Public Property Title() As String
    Get
        Return m_Title
    End Get
    Set(ByVal value As String)
        m_Title = value
    End Set
End Property


Public Property [Date]() As String
    Get

    End Get
    Set(ByVal value As String)

    End Set
End Property 

9条回答
对你真心纯属浪费
2楼-- · 2019-08-25 02:12

misspell your variable names!

查看更多
Anthone
3楼-- · 2019-08-25 02:12

On .NET, it is reasonable to consider the Common Language Specification (CLS) as the lowest common denominator that you should cater to. This is documented in ECMA-335 "Common Language Infrastructure (CLI) Partitions I to VI". Here's what it says specifically about names; a note in CLS Rule #4 (8.5.1 "Valid names"):

CLS (consumer): Need not consume types that violate CLS Rule 4, but shall have a mechanism to allow access to named items that use one of its own keywords as the name.

So no, it's not really a hack, but a definite rule. The reason why it's there is that, as .NET is extensible as far as languages targeting it go, you can never avoid name clashes. If you cover C#, there's VB. If you cover C# and VB, there's C++/CLI. If you cover all those, there's F# and Delphi Prism. And so on. Hence why it is mandated by CLS that languages provide a way to escape their keywords as identifiers; and all languages I've listed provide some way to do so (and thus are compliant CLS consumers).

In general, it is still considered good manners to avoid clashes with either C# or VB non-context keywords, mainly because those two languages are the most popular by a very large margin. For example, it is the reason why it's HashSet and not just Set - the latter is a VB keyword. The full listings are:

查看更多
放荡不羁爱自由
4楼-- · 2019-08-25 02:14

Most languages have something to escape any reserved words. C# has @ so you can use @class as an argument name (something MVC adopters are learning).

If the domain states that a certain word be used to describe it then that is what the escaping of reserved words is there for. I wouldn't be afraid to escape reserved words to get my model close to the domain even if it means more typing - the clarity is worth it!

查看更多
做自己的国王
5楼-- · 2019-08-25 02:15

"Date_" or "_Date".

查看更多
我想做一个坏孩纸
6楼-- · 2019-08-25 02:18

Probably think about more specific nature of the variable?

From your example, the "Date" can be "Created Date" or "Posted Date" or anything else. If you find your variable names too trivial, you may be oversimplifying (or even obfuscating) your code. Help your coworkers by creating a clear but concise variable names.

查看更多
ら.Afraid
7楼-- · 2019-08-25 02:22

There is no silver bullet, but modern languages help a lot with better abilities to manage namespaces.

In my case, I curse the fact that C has an 'index' command.

查看更多
登录 后发表回答