以“ k”开头的常数有什么意义? [英] What is the significance of starting constants with 'k'?

查看:222
本文介绍了以“ k”开头的常数有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在自学Objective-C,并且在很多书籍和示例中都注意到以固定定义(例如

I'm teaching myself Objective-C and I noticed in a lot of books and examples the use of 'k' and camel-casing in constant definition, e.g.

#define kMyConstant 0

k的含义是什么?
这是Objective-C风格所独有的,还是C所共有的?
为什么偏离(我一直认为是最佳实践)K_MY_CONSTANT样式?

What is the significance of the 'k'? Is this unique to Objective-C style, or common to C in general? Why the deviation from (what I've always thought as a best practice) K_MY_CONSTANT style?

谢谢。

推荐答案

之前在SO问题

It was mentioned once before in the SO question, Lower case "k" in Cocoa.


这是一种通用编程符号
,不特定于Objective-C(即
匈牙利符号)和 k

It is a general programming notation not specific to Objective-C (i.e. Hungarian Notation) and the "k" stands for "constant".

如果您查看 Google缓存在Google的Objective-C指南中,您会看到他们曾经将其包含在样式指南中:

If you look at the Google cache of Google's guidelines for Objective-C you can see that they used to include it in their styleguide:


常量名称(#defines,enums,const局部变量等)应以小写字母k开头,然后使用大小写混合来分隔单词,即kInvalidHandle,kWritePerm。

Constant names (#defines, enums, const local variables, etc.) should start with a lowercase k and then use mixed case to delimit words, i.e. kInvalidHandle, kWritePerm.

尽管a编写起来很痛苦,它们对于保持代码的可读性至关重要。以下规则描述了您应该评论的内容和位置。但是请记住:尽管注释非常重要,但是最好的代码是自我记录。为类型和变量提供合理的名称比使用晦涩的名称然后尝试通过注释来解释要好得多。

Though a pain to write, they are absolutely vital to keeping our code readable. The following rules describe what you should comment and where. But remember: while comments are very important, the best code is self-documenting. Giving sensible names to types and variables is much better than using obscure names and then trying to explain them through comments.

但是自从实时版本。应该注意的是,这违反了可可粉官方编码指南来自苹果公司。

But it has since been removed in the live version of the document. It should be noted that it goes against the the Official Coding Guidlines for Cocoa from Apple.

这篇关于以“ k”开头的常数有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆