C 风格命名约定的要点
我在大学上的一门课是用 C 语言进行大量编程。我们应该在作业中使用“C”风格的命名约定或获得停靠标记(例如,变量的命名类似于 int line_counter,一个函数clear_array())我发现这个约定真的很烦人,尤其是在使用了一年的Java之后,这些东西的命名更方便,比如lineCounter或clearArray()。下划线很烦人,打字很麻烦,而且会增加语法错误的数量。为什么要遵守这个约定?它有什么逻辑或者背后有什么意义吗?或者这只是另一个让“C”更难编写代码的技巧?
One of the classes I'm taking in college is where we are doing a ton of programming in C. We are supposed to use 'C' style naming conventions in the assignments or get docked marks (eg a variable is named like int line_counter, a function clear_array() ) I find this convention really really annoying esp coming after a year of Java where such things are named more conviniently like lineCounter or clearArray(). Underscores are annoying, a hassle to type and increase the amount of syntax errors. Why should this convention be followed? Is there some logic to it or some point behind it? Or is it just another trick to make 'C' even harder to write code in?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
风格命名约定涉及传统、当地协议和统一性。你必须习惯不同的风格,因为一旦进入就业市场,并不能保证你会使用你喜欢的代码约定。从这个意义上说,重点是您必须了解 Java 风格并不是您要处理的唯一风格。
至于这是否是一个好的决定,很难做出决定。我对像你这样的样式违规感到恼火,即使我有数十年的编程经验,但你不能真正假装将旧代码改造成新约定。这会占用大量非生产时间,并且给其他程序员带来麻烦。
您可以在编辑器(例如 vim)中使用制表符补全来缓解下划线输入缓慢的问题。编写方法只需输入几个字母并按 Tab 键即可。您不太可能在第一个字母中打下划线。
Style naming conventions are a matter of tradition, local agreement and uniformity. You have to get used to a different style because there's no guarantee, once out in the job market, that you will use the code convention you like. In this sense, the point is that you have to learn that the Java style is not the only style you will ever deal with.
On regard if it's a good decision or not, it's hard to decide. I am annoyed by styling violations as you are, even if I have tens of year of experience in programming, but you cannot really pretend to reform an old code to new conventions. It takes a lot of non-productive time and screws everything for the other programmers.
You can mitigate the problem of slow-to-type-underscore using tab-completion in your editor (e.g. vim). Writing a method will just become typing a few letters and pressing tab. It's unlikely you will hit an underscore in the first letters.
习惯特定的样式约定只是您编写的代码行数的问题。在这种情况下,你在 C 中觉得烦人,因为你习惯了 Java,这也是 Python 中的约定:与你相反(我主要用 Python 编程),我更喜欢带下划线的变量名称(尽管我理解 java 的变量名称)也非常清晰易读)
另一方面,出于好奇,您可能知道编写给定字符的难度取决于键盘的本地分布。 C 中使用的许多符号很难用(例如西班牙语键盘)书写。
我认为这是一个非常糟糕的主意,就是不使用该语言的标准约定并开发自定义约定。这对其他人和您来说都非常糟糕,因为您必须学习或交互的所有文档、代码等都将以该语言的标准风格编写
Getting used to a specific style convention is just a question of the number of lines of code you have written. In this case, that that you find annoying in C because you were used to Java, is also a convention in Python: Contrarily to you (I program mostly in Python), I like better the underscored variable names (although I understand the java ones are also very clear to read)
On the other hand, and as a curiosity, you probably know that the difficulty to write a given character depends on the local distribution of your keyboard. Many of the symbols used in C are a hell to write with, for example, a Spanish keyboard.
What I think it is a really bad idea, is not to use the standard conventions for the language and develop a custom convention. This is really bad for others and also for you because all the documentation, code etc you have to study or interact with will be written in the standard style for the language
不存在“C 风格命名约定”这样的东西;您会在 C 代码和 C++ 代码中发现相当多不同的样式。也就是说,你只需要忍耐并遵守教授要求的惯例即可。一般而言,样式约定的目的是减少错误并轻松推断有关符号的信息,而无需查找其声明。也就是说,关于哪种风格或哪种风格最好存在很多差异。不过,保持一致的风格对于理解整个代码库很重要,如果作业都是使用相同、一致的风格编写的,那么您的教授可能会更容易对作业进行评分和理解。
几乎你工作的任何公司都会要求你遵守公司的编码约定,因此你的教授有类似的要求并不是没有道理的。尽管我刚开始时需要一些时间来适应 Google 的 C++ 编码约定 ,风格一致对于代码的可读性来说无疑是一个福音。没有什么比不同风格的混合更难以理解的了。
There is no such thing as "C-style naming conventions"; you will find quite a number of different styles both in C code and in C++ code. That said, you will just have to suck it up and go with the convention required by your professor. The purpose of style conventions, in general, is to reduce errors and to make it easy to infer information about a symbol without needing to look up its declaration. That said, there are many differences about which style or styles are best. Having a consistent style, though, is important for the understanding of the code base as a whole, and it is probably easier for your professor to grade and understand the homework if it is all written using the same, consistent style.
Pretty much any company you work for will require you to adhere to the company's coding convention, so it is not unreasonable for your professor to have similar requirements. Although it took some getting used to Google's C++ coding conventions when I first started, it is undoubtedly a boon to the code's readability to be in a consistent style. Nothing is more unintelligible than a mix of different styles.
我不同意你的老师决定将分数扣除大写,但你必须遵循他或她的指示。
等级旨在反映对材料的理解。我个人认为在教授 C 入门课程时根据理解进行评分就足够了。初学者掌握语言结构有足够的困难。为了鸡毛蒜皮的事情扣分是没有必要的,也是残酷的。
老师的独特风格或遵循公司风格的优点是不同的问题。
I disagree with your teacher's decision to dock points for capitalization, but you're going to have to follow his or her instructions.
Grades are intended to reflect understanding of the material. I personally found it sufficient when teaching introductory C courses to grade on understanding. Beginners have enough difficulty mastering language constructs. It is unnecessary and cruel to dock points for trivialities.
The merits of your teacher's particular style, or of following a corporate style, are separate questions.