I guess it depends on your internal processes as much as anything.
In my company, we practise peer review, and all code that gets comitted must be 'buddied in' by another developer, who you have to explain your code to.
Time constraints are one thing, but if I review code that has heinously long classes, then I won't agree to the check-in.
It's hard to get used to at first, but at the end of the day, it's better for everyone.
Also, having a senior developer who is a champion for good class design, and is willing and able to give examples, helps tremendously.
Finally, we often do a coding 'show and tell' session where we get to show off our work to our peers, it behooves us not to do this with ugly code!
Long classes is one bad code smell of many possible.
Remedying overly large classes by creating lots of small ones may create its own problems. New engineers on your project may find it difficult to follow the flow of the code to work out what happens where. One artifact of this problem can be very tall call stacks, execution nesting through many small classes.
Another suggestion is to do only what is asked. Don't play the "What if" game and try to overdesign a solution. This has the "Keep it simple, stupid" idea behind it.
We're a java and maven shop, and one of the...I guess you could say forensic methods we use are the excellent FindBugs, PMD and javancss plugins. All will give warnings about excessive length in a method, and the cyclomatic complexity calculations can be very eye opening.
The single most important step for me to avoid large classes that often violate SRP was to use a simple dependency injection framework. This freed me from thinking too much about how to wire things together. I only use constructor injection to keep the design free from cycles. Tools like Resharper help to initialize fields from constructor arguments. This combination leads to a near zero overhead for creating and wiring up new classes, and implicitly encourages me to structure behavior in much more detail.
This all works best if data is kept separate from behavior, and your language supports constructs like events that can be used to decouple communication that flows in the downward direction of the dependency graph.
use some static code analysis tools in your automated builds and write/configure/use some rules so that for example someone has to write a justification when he/she breaks it..
发布评论
评论(7)
我想这取决于你的内部流程。
在我的公司,我们实行同行评审,所有提交的代码都必须由另一位开发人员“参与”,您必须向他们解释您的代码。
时间限制是一回事,但如果我审查具有令人发指的长类的代码,那么我不会同意签入。
一开始很难适应,但最终,这对每个人都更好。
此外,拥有一位拥护良好类设计、愿意并且能够举例的高级开发人员也会有很大帮助。
最后,我们经常进行编码“展示和讲述”会议,向同行展示我们的工作,我们应该不要用丑陋的代码来这样做!
I guess it depends on your internal processes as much as anything.
In my company, we practise peer review, and all code that gets comitted must be 'buddied in' by another developer, who you have to explain your code to.
Time constraints are one thing, but if I review code that has heinously long classes, then I won't agree to the check-in.
It's hard to get used to at first, but at the end of the day, it's better for everyone.
Also, having a senior developer who is a champion for good class design, and is willing and able to give examples, helps tremendously.
Finally, we often do a coding 'show and tell' session where we get to show off our work to our peers, it behooves us not to do this with ugly code!
使用 Resharper 等工具和 Extract Method 命令。
Use a tool like Resharper and the Extract Method command.
长类是许多可能的不良代码味道之一。
通过创建大量小类来纠正过大的类可能会产生其自身的问题。项目的新工程师可能会发现很难遵循代码流程来弄清楚在哪里发生了什么。这个问题的一个后果可能是非常高的调用堆栈,通过许多小类嵌套执行。
Long classes is one bad code smell of many possible.
Remedying overly large classes by creating lots of small ones may create its own problems. New engineers on your project may find it difficult to follow the flow of the code to work out what happens where. One artifact of this problem can be very tall call stacks, execution nesting through many small classes.
另一个建议是只做要求的事情。不要玩“假设”游戏并尝试过度设计解决方案。这背后有“保持简单,愚蠢”的想法。
Another suggestion is to do only what is asked. Don't play the "What if" game and try to overdesign a solution. This has the "Keep it simple, stupid" idea behind it.
我们是一家 java 和 maven 商店,其中之一...我想你可以说我们使用的取证方法是优秀的 FindBugs、PMD 和 javancss 插件。所有这些都会对方法中的长度过长发出警告,并且圈复杂度计算可能会非常令人大开眼界。
We're a java and maven shop, and one of the...I guess you could say forensic methods we use are the excellent FindBugs, PMD and javancss plugins. All will give warnings about excessive length in a method, and the cyclomatic complexity calculations can be very eye opening.
对我来说,避免经常违反 SRP 的大型类的最重要的一步是使用简单的依赖注入框架。这让我不再过多思考如何将事物连接在一起。我只使用构造函数注入来使设计免受循环的影响。 Resharper 等工具有助于从构造函数参数初始化字段。这种组合导致创建和连接新类的开销几乎为零,并且隐含地鼓励我更详细地构建行为。
如果数据与行为分开,并且您的语言支持事件之类的结构,这些结构可用于解耦沿依赖图向下流动的通信,那么这一切效果最好。
The single most important step for me to avoid large classes that often violate SRP was to use a simple dependency injection framework. This freed me from thinking too much about how to wire things together. I only use constructor injection to keep the design free from cycles. Tools like Resharper help to initialize fields from constructor arguments. This combination leads to a near zero overhead for creating and wiring up new classes, and implicitly encourages me to structure behavior in much more detail.
This all works best if data is kept separate from behavior, and your language supports constructs like events that can be used to decouple communication that flows in the downward direction of the dependency graph.
在自动构建中使用一些静态代码分析工具并编写/配置/使用一些规则,以便例如某人在违反规则时必须编写理由。
use some static code analysis tools in your automated builds and write/configure/use some rules so that for example someone has to write a justification when he/she breaks it..