简体   繁体   English

Objective-C编码约定

[英]Objective-c coding conventions

I'm seeking for best practices and coding conventions, so have a question on this. 我正在寻找最佳实践和编码约定,因此对此有疑问。 Looked trough almost all of them, but couldn't find any answers. 看着几乎所有的人,但找不到任何答案。 Example function: 示例功能:

- (int)getSomeNumber
{
    int result = 0;
    if (something) {
       result++;
    }
    return result;
}

Question is, is it better to write result++, or ++result. 问题是,编写result ++还是++ result更好。 If someone knows some suggestions or answers why is it better to write in one way or the other it would be very great. 如果有人知道一些建议或答案,为什么以一种或另一种方式写更好,那将是非常好的。 Thanks in advance! 提前致谢!

It doesn't matter the slightest when used as a statement (note that it does matter when part of a larger expression), as long as you are consistent. 只要您保持一致,用作语句就没有任何关系(请注意,当使用较大表达式的一部分时,它确实有关系)。 Don't mix styles within one source file or project. 不要在一个源文件或项目中混合样式。

Well, even better in this specific example would be return something ? 1 : 0; 好吧,在这个特定示例中甚至更好的方法是return something ? 1 : 0; return something ? 1 : 0; . Some people even prefer return something; 有些人甚至更喜欢return something; , but I don't because in my eyes it makes no sense to convert a Boolean into an integer. ,但我不是这样,因为在我看来,将布尔值转换为整数没有任何意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM