将类放入单独的CS文件中的优点 [英] Advantage of putting classes into seperate cs files

查看:57
本文介绍了将类放入单独的CS文件中的优点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些作者通读一些书,建议每个类都拥有自己的.cs文件.连同每个接口,枚举,委托和结构.

作为一个小组,我们使用Resharper,并且Resharper甚至有一个工具,只需按一下按钮,即可自动将上面提到的所有类型分离到各自的文件中.

我认为这样做对可维护性更好,但是为了说服我的小组坚持这一点,这样做还有什么好处(如果有的话)?

Reading through a few books some authors recommend that each class gets its own .cs file. Along with each interface, enum, delegate, and struct.

As a group we use Resharper and Resharper even has a tool that will automatically separate all of the mentioned types above into their own files with the push of a button.

I believe this is better for maintainability, but in order to convince my group to adhere to this what other, if any, benefits does this accomplish?

推荐答案

具体的优点或缺点.

它仅与代码文本的可读性有关.它不会影响其他任何东西.也许,如果您将多个类放在一个文件中,除了可能会占用一些额外空间的其他内容之外,您还需要一些注释来分隔它们.它还取决于文件中的总行数.有人说每种类型一个文件是必须的,但是我认为这是毫无意义的学问.只要您可以轻松地找到代码并管理文件,所有这些详细信息都将变得无关紧要.

现在,如何说服小组?这是我的主意:您需要放松所有人的目录结构和文件,但需要严格的注释样式(当然,还需要正确的项目/解决方案结构,程序集签名过程,没有重复的工件等).最重要的是正确使用版本控制系统(如果您没有正确使用版本控制系统,请考虑您根本没有进行任何开发).

甚至关于格式.借助Visual Studio,我发明了一条使所有人都方便的好规则:

每个开发人员只要通过自动格式化就可以完全使用自己的格式化样式,但是格式化规则应该有所不同,甚至文件之间也一样.现在,在相对少见的情况下,一个开发人员修改了另一个开发人员拥有的文件,此人有权根据自己的意愿将其重新格式化并将代码提交给版本控制系统.原始作者不会受到影响,因为她/他可以随时将其重新格式化.我的规则主要适用于C#,其中使用Visual Studio以全面的方式完成自动格式化.



许多非常优秀的开发人员对格式和布局样式过于敏感. 重要的是,不要通过执行多余的规则/政策来伤害这些人.要非常小心!

[END EDIT]

—SA
There are no specific advantages or drawbacks.

It''s only about readability of the code text; and it does not effect anything else. Perhaps if you put several classes in one file, you would like to have some comments delimiting them, in addition to other stuff, which could eat up some extra space. It also depends on total number of lines in the file. Some say one file per type is a must, but I think this would be pointless pedantism. As soon as you can easily find your code and manage files, all those detail don''t matter.

Now, how to convince the group? Here is my idea: you need to relax everyone in terms of directory structure and file, but demand strict commenting style (and of course, correct project/solution structure, assembly signing procedure, no repeated artifacts, etc.). Most important stuff is the proper use of Revision Control System (and if you don''t use a good one properly, consider you are not doing any development at all).

Even about the formatting. With Visual Studio, I invented a good rule to make if convenient for everyone:

Every developer can use her/his own formatting style, as soon as it is fully formed by auto-formatting, but the formatting rules should vary, even file to file. Now, in those relatively rare cases when one developer modifies the file owned by another developer, this person has a full right to reformat it to her/his liking and commit the code to the Revision Control System. The original author does not suffer, because she/he can reformat it back at any moment of time. My rules mostly apply to C# where the auto-formatting is done in a comprehensive way with Visual Studio.



Many really good developers are overly sensitive to formatting and layout styles. It''s very important not to hurt such people by enforcing redundant rules/policies. Be very careful!

[END EDIT]

—SA


这篇关于将类放入单独的CS文件中的优点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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