如何才能最好地避免在我的语言或框架中使用保留字或关键词?

发布于 2024-08-13 16:15:42 字数 1431 浏览 6 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

浮华 2024-08-20 16:15:42

一个不错的IDE...

A decent IDE...

你げ笑在眉眼 2024-08-20 16:15:42

如果您使用Visual StudioSQL Server Management Studio,那么它们会用不同的颜色突出显示保留关键字。

如果确实需要,某些语言有使用保留关键字的特殊语法,尽管并不真正推荐。例如,在 C# 中,您可以使用 at 符号作为前缀,例如,

var @class = "Hello";

或者在 SQL Server 中,您可以使用方括号:

SELECT [where] from Locations;

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;
薄荷港 2024-08-20 16:15:42

几乎所有编辑器和 IDE 都会自动以适当的(与其他符号对比)颜色突出显示关键字,前提是它知道您正在编辑的代码的语言语法。

如果您无法像以前那样使用这项技术,那么最好的办法就是记住所有关键字,并在手边准备一份备忘单。但实际上,你的编辑应该这样做。

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.

演出会有结束 2024-08-20 16:15:42

我从来没有遇到过这个问题,即使是在更大的项目中也是如此。我遵循一些标准约定,例如在变量名前加上变量类型的前缀。我还利用命名空间来确保获得我想要的确切对象/方法。说实话,我看不出给不保留的东西命名有什么困难。

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.

无言温柔 2024-08-20 16:15:42

我真的很少遇到与关键字冲突的问题:如果你命名一个列表 List,那么,好吧,你应该被它咬住,因为你应该使用更有意义的变量名......

当然,这一点没有实际意义如果您正在构建编译器并且实际上需要一个 Class 类...但这种情况很少发生。

在几乎所有其他情况下,当 List itemsList tokens 有意义时,List list 就没有意义。

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.

青衫儰鉨ミ守葔 2024-08-20 16:15:42

追求更清晰。

C# 示例

class

cssClass

SQL Server 示例

更新

PerformUpdate

Go for more clarity.

C# Example

class

cssClass

SQL Server Example

Update

PerformUpdate

末が日狂欢 2024-08-20 16:15:42

IDE 可以提供帮助。

一个好方法就是学习您正在使用的语言的语法。通常您可以找到这些语言的保留关键字列表。

定期测试 JavaScript 等动态语言将很快暴露关键字问题。 (因为我还没有让 Eclipse 能够与 javascript 很好地配合)。

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

寒尘 2024-08-20 16:15:42

使用结构化编程!

这意味着不同范围内的变量应该只存在并且只与该范围相关。当您想使用全局变量时,请显式地这样做。

如果程序的区域无法分割为函数,则将区域分割为类。这样,AreaX->ID 就不会与 AreaY->ID 冲突。类还可以绕过一些保留字。

对于一些保留字,您只需尽量避免使用通用名称即可。我知道 $HOME 在 PHP 中特别危险。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文