[英]Should I place every class in separate file?
Should I place every class in separate file?我应该将每个班级放在单独的文件中吗? Even those short helper classes that are used only in one place?即使是那些只在一个地方使用的短助手类? Like this one:像这个:
public class IntToVisibilityConverter : GenericValueConverter<int, Visibility>
{
protected override Visibility Convert(int value)
{
return value == 0 ? Visibility.Collapsed : Visibility.Visible;
}
}
我这样做,通常这样做是最佳做法,但有时这是一个见仁见智的问题。
That depends greatly of personal preference, but I like to do it.这很大程度上取决于个人喜好,但我喜欢这样做。
In this case, I would have a folder inside my application called ValueConverters, and put all converters, including short ones, inside their own files.在这种情况下,我的应用程序中有一个名为 ValueConverters 的文件夹,并将所有转换器(包括短转换器)放在它们自己的文件中。
I find it makes it easier to get an overview of what your project consist of from the Solution Explorer.我发现通过解决方案资源管理器可以更轻松地了解您的项目包含的内容。
I'll rephrase the question for you: should I use StyleCop ?我会为你改述这个问题:我应该使用StyleCop吗? (it includes this rule). (它包括此规则)。 The answer is yes.答案是肯定的。 I use it and my code is much more readable (but I have to admit I disable all the rules that require the method documentation to be complete :-) )我使用它并且我的代码更具可读性(但我不得不承认我禁用了所有需要方法文档完整的规则:-))
I do think that when you program in a team, having a fixed and uniform code format is very important.我确实认为,当您在团队中编程时,拥有固定且统一的代码格式非常重要。 And even when you program "solo".甚至当你编程“solo”时。 A cluttered code is more difficult to read and errors can hide better in the clutter :-)杂乱的代码更难阅读,错误可以更好地隐藏在杂乱中:-)
It is usually the best practise to put every class in a seperate file.将每个类放在单独的文件中通常是最佳做法。 Taking into account your short helper classes;考虑到您的短期助手课程; you could create a helper class which contain all your helper methods, to prevent having way too many classes.您可以创建一个包含所有辅助方法的辅助类,以防止有太多的类。 If your helper class gets too big, you can seperate your helper functions per category如果您的辅助类变得太大,您可以将每个类别的辅助函数分开
It is good practice to do so.这样做是个好习惯。
You can easily find the class if you name the file after the class.如果以类命名文件,则可以轻松找到该类。
Resharper has a built in error for classes not matching the file name they are in... Resharper 对于与它们所在的文件名不匹配的类有一个内置错误...
Typically, IMO yes.通常,IMO 是的。 Think about any new developers who must find where code lives.想想任何必须找到代码所在位置的新开发人员。 Yes, you can use go to definition, but that is not the be all, end all.是的,您可以使用 go to definition,但这不是全部,全部结束。 However, I will say that sometimes if you have an interface that is small and only used for the class that it is within, then you can probably get away with it.但是,我会说,有时如果你有一个很小的接口并且只用于它所在的类,那么你可能会侥幸逃脱。 However, even that can expand and later be required to be pulled out (and maybe those contracts should be in another namespace anyways).但是,即使这样也可以扩展并在以后需要被拉出(也许这些合约无论如何都应该在另一个命名空间中)。
So, ultimately, I would say the majority of the time, yes, but there are some caveats.所以,最终,我会说大部分时间,是的,但有一些警告。 As with anything, it is never black and white与任何事物一样,它从来都不是非黑即白
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.