那些条条框框真的重要么?反编码规范的做法的思考
今天整理文件,看到我早年写的一个编程规范的文档,随便写一点。欢迎大家讨论:
(1)对 for if while 等块结构加上花括号,无论是否只有一条语句;
(2)花括号占一行;
现在发现这两条很教条,有时候反而不利于代码的可读性。现在我倾向在每一行不超过屏幕的宽度上尽可能写多一些代码,而压缩行,使得一个方法,乃至一个类可以在一屏被显示。方块的代码比细长的更容易阅读,对比:
class Person{ public string Name { get; set; } public int Age { get; set; } public string Address { get; set; } public string EMail { get; set; } public string Phone { get; set; } public DateTime Birthday { get; set; } public Person() { } public Person( string name, int age, string address, string email, string phone, DateTime birthday ) { Name = name; Age = age; Address = address; EMail = email; Phone = phone; Birthday = birthday; }}
class Person{ public string Name { get; set; } public int Age { get; set; } public string Address { get; set; } public string EMail { get; set; } public string Phone { get; set; } public DateTime Birthday { get; set; } public Person() { } public Person(string name, int age, string address, string email, string phone, DateTime birthday) { Name = name; Age = age; Address = address; EMail = email; Phone = phone; Birthday = birthday; }}
Func<int, int, int> IntPow = (x, y) => { int r = x; for (int i = 1; i < y; i++) r *= x; return r; }Console.WriteLine(IntPow(3, 2));
class FileSystem{ public virtual void SetInfo(FileSystemInfo info) { ... }}class Fat32FileSystem : FileSystem{ public virtual void SetInfo(Fat32FileSystemInfo info) { ... }}class class FileSystemInfo { ...}class class Fat32FileSystemInfo : FileSystemInfo{ ...}
for(int i = 0; i < 5; i++) String str = "a";
[解决办法]
public Person( string name, int age, string address, string email, string phone, DateTime birthday )
[解决办法]
对于你的最后一条,请牢记,聚合是has-a 继承是is-a 两者虽然在用法上可以互换
但是过于泛滥的聚合会导致维护性很差。
------解决方案--------------------
我记得曾经讨论过一个关于“三目运算”的例子.
//举个例子而已,别较真a>b?b:b>0?b:0
[解决办法]
class Person{ public string Name { get; set; } public int Age { get; set; } public string Address { get; set; } public string EMail { get; set; } public string Phone { get; set; } public DateTime Birthday { get; set; } public Person() { } public Person(string name, int age, string address, string email, string phone, DateTime birthday) { Name = name; Age = age; Address = address; EMail = email; Phone = phone; Birthday = birthday; }}
[解决办法]
ding .
[解决办法]
java 工具 PMD 有很多值得借鉴的内容,看完那个再讨论吧
[解决办法]
微软也有 StyleCop,比 PMD 弱点
要知道考试都有卷面整洁分,看着一个穿着邋遢的人你什么感觉
规范格式除了便于阅读外,还有其他一些统计、检查工具用得着,早年作对日开发时见到一些
[解决办法]
if(a>b || b>0) x=b;else x=0;
[解决办法]
加班?这么晚了还不睡?
来看看牛人,公司现在基本没规范
[解决办法]
这是个技术活
[解决办法]
同意1楼和2楼的意见
[解决办法]
学习 学习
[解决办法]
规范是要求,可以根据项目的实际情况制定。
[解决办法]
学习了
[解决办法]
反对,搞成什么玩意,一坨屎一样,好看吗? 除了要滚屏还有别的理由吗?
知道编译器有全屏功能么?
[解决办法]
public string Name { get; set; }
public int Age { get; set; }
public string Address { get; set; }
public string EMail { get; set; }
public string Phone { get; set; }
public DateTime Birthday { get; set; }
我比较喜欢这种写法
[解决办法]
很有参考价值
有些简单的参数检查,个人习惯用一行代码
if(...) return null;
[解决办法]
不要为了遵守规范而违背了当初制定规范的目的。
[解决办法]
还是习惯这么写
public string Name { get; set; }
public int Age { get; set; }
不过中间会留一个空行