最佳实践:在类定义中对public / protected / private的排序? [英] Best practice: ordering of public/protected/private within the class definition?
问题描述
我从头开始一个新项目,希望它是干净/有良好的编码标准。
I am starting a new project from the ground up and want it to be clean / have good coding standards. In what order do the seasoned developers on here like to lay things out within a class?
A:1)public方法2)private方法3)public vars 4)private vars
A : 1) public methods 2) private methods 3) public vars 4) private vars
B:1)public vars 2)private vars 3)public methods 4)private方法
B : 1) public vars 2) private vars 3) public methods 4) private methods
C :1)public vars 2)public methods 3)private方法4)private vars
C : 1) public vars 2) public methods 3) private methods 4)private vars
我一般喜欢把public static vars放在顶端,方法列在你的构造函数之前,还是应该首先列出构造函数?这种事...
I generally like to put public static vars at the top, but then would a public static method be listed ahead of your constructor, or should the constructor always be listed first? That sort of thing...
我知道这是finnicky,但我只是想知道:这是什么最好的做法?
I know it's finnicky but I just wondered: what are best practices for this?
PS:没有我不使用Cc#。我知道。我是一个luddite。
推荐答案
在清除代码,Robert C. Martin建议编译器始终将成员变量放在类的顶部(常数首先是私有成员),方法应该是以这样的方式排序,使得他们阅读像一个故事,不会导致读者需要跳过代码太多。这是一种更明智的组织代码的方式,而不是访问修饰符。
In Clean Code, Robert C. Martin advises coders to always put member variables at the top of the class (constants first, then private members) and methods should be ordered in such a way so that they read like a story that doesn't cause the reader to need to jump around the code too much. This is a more sensible way to organize code rather than by access modifier.
这篇关于最佳实践:在类定义中对public / protected / private的排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!