如何组织大型代码文件? [英] How to organise large code files?

查看:302
本文介绍了如何组织大型代码文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我越来越意识到,我在任何单个文件中的代码通常可以跨越数百行很容易,虽然我知道的实现可能是合理的,但它仍然感到混乱和无组织的。



据我了解,在有些情况下大量的代码时,只需要的情况下,但最新最好的方式来组织这一切?



我想过的方法,私人期从公共内部<分离变量/ code>,但我不希望因为我不禁想,一个阶级的成分在一个文件中的归属。



这整件事当我与一个WPF窗口,它似乎总是以指数速度非常迅速成长为一个巨大的混乱的代码隐藏工作复利



另外:C#有一个关键字名为部分,它允许你劈了过来任意数量的文件类不影响功能。然而,我注意到,微软似乎只使用部分这使我是否只是拆分一类,因为它有质疑对你隐藏生成的代码(的WinForms / WPF)。很多线路是合法使用部分 - ?是不是



感谢


< DIV CLASS =h2_lin>解决方案

分开你的代码到责任。对于每个责任,定义单个类型。也就是说,按照单一职责主要。这样做会导致码更小的单元,其每一个执行一个非常特定的功能。不仅在更小的文件这样的结果,而且在更好的设计和可维护性。


I am increasingly aware that my code in any single file can often span hundreds of lines quite easily and although I know the implementation might be sound, it still feels messy and unorganised.

I understand that there are situations where a lot of code is neccessary, but whats the best way to organise it all?

I've thought about separating variables from methods, privates from publics and internals but I don't want to because I can't help thinking that the components of ONE class belong in ONE file.

This whole thing is compounded when I'm working with the codebehind of a WPF window, which always seem to grow at an exponential rate into one huge mess very quickly.

Also: C# has a keyword called partial, which allows you to split a class over any number of files without affecting the functionality. However, I have noticed that Microsoft only seem to use partial to hide generated code from you (Winforms / WPF.) Which leads me to question whether splitting a class simply because it has many lines is a legitimate use of partial - is it?

Thanks

解决方案

Separate your code into responsibilities. For each responsibility, define a single type. That is, follow the Single Responsibility Principal. Doing so will result in smaller units of code, each of which performs a very specific function. Not only does this result in smaller files, but also in better design and maintainability.

这篇关于如何组织大型代码文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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