TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

OOP Alternative to Utility Classes

4 点作者 yiransheng超过 10 年前

1 comment

sparkie超过 10 年前
Please don&#x27;t do this. This is a perfect example of where living inside your OOP bubble too long has a net negative effect. You really want to construct a new object, set two fields, and invoke a method call to find the maximum of two numbers? This is nothing short of madness.<p>I&#x27;d pick the util method any day, as much as I dislike &quot;Utils&quot; classes. It&#x27;s not because they&#x27;re bad code, it&#x27;s more because they&#x27;re disorganized (no cohesion). The &quot;solution&quot; would suffer exactly the same problem of organization depending on what namespace you decide to dump the &quot;Max&quot; class in.<p>There is nothing wrong with having static classes with static methods which are completely stateless. That java decided to omit plain functions from the language was a big mistake, so settling for the next best thing when <i>a function is precisely what we want</i> is the sane thing to do.<p>A nicety that C# will have now is that you can import static classes as if they were namespaces, thus removing the need to continually write &quot;Utils&quot; everywhere, you can just stick the static method name, and only use &quot;Utils&quot; to disambiguate if necessary.