如何避免创建庞大的类

发布于 2024-09-04 05:53:23 字数 1431 浏览 13 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

棒棒糖 2024-09-11 05:53:24

我想这取决于你的内部流程。

在我的公司,我们实行同行评审,所有提交的代码都必须由另一位开发人员“参与”,您必须向他们解释您的代码。

时间限制是一回事,但如果我审查具有令人发指的长类的代码,那么我不会同意签入。

一开始很难适应,但最终,这对每个人都更好。

此外,拥有一位拥护良好类设计、愿意并且能够举例的高级开发人员也会有很大帮助。

最后,我们经常进行编码“展示和讲述”会议,向同行展示我们的工作,我们应该不要用丑陋的代码来这样做!

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!

幸福不弃 2024-09-11 05:53:24

使用 Resharper 等工具和 Extract Method 命令。

Use a tool like Resharper and the Extract Method command.

场罚期间 2024-09-11 05:53:24

长类是许多可能的不良代码味道之一。

通过创建大量小类来纠正过大的类可能会产生其自身的问题。项目的新工程师可能会发现很难遵循代码流程来弄清楚在哪里发生了什么。这个问题的一个后果可能是非常高的调用堆栈,通过许多小类嵌套执行。

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.

愛上了 2024-09-11 05:53:24

另一个建议是只做要求的事情。不要玩“假设”游戏并尝试过度设计解决方案。这背后有“保持简单,愚蠢”的想法。

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.

回首观望 2024-09-11 05:53:24

我们是一家 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.

陌若浮生 2024-09-11 05:53:24

对我来说,避免经常违反 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.

爱的故事 2024-09-11 05:53:24

在自动构建中使用一些静态代码分析工具并编写/配置/使用一些规则,以便例如某人在违反规则时必须编写理由。

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..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文