最佳实践,1 种方法和 2 种方法具有相同的功能

发布于 2025-01-03 08:39:13 字数 432 浏览 3 评论 0原文

我有以下场景。 有些人使用方法 1,有些人使用方法 2。 两种方法具有相同的功能,根据新闻表中的新闻 ID 锁定或解锁新闻。 你有什么建议,哪一个更好,为什么?

注意:为了简单起见,我使用 void 返回类型而不是 bool,因此请忽略这一点

方法 1:

public void LockNews(long newId)
{
 ......
}

public void UnlockNews(long newId)
{
 ...
}

方法 2:

public void LockUnlockNews(long newId,bool Islock)
{
 ......
}

I have following scenario.
Some people use approach 1 and some use approach 2.
Both approaches have same functionality lock or unlock news against news id in news table.
what you suggest,which one is better and why?

Note:I use void return type instead of bool for simplicity so please ignore this

Approach 1:

public void LockNews(long newId)
{
 ......
}

public void UnlockNews(long newId)
{
 ...
}

Approach 2:

public void LockUnlockNews(long newId,bool Islock)
{
 ......
}

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

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

发布评论

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

评论(4

笑,眼淚并存 2025-01-10 08:39:13

方法 1,对我来说,因为恕我直言,方法应该代表单个操作。为了编写可测试的代码或任何形式的自动化测试,它将保持清晰的分离并且更有意义。

方法 2 倾向于“执行所有操作”,恕我直言,应该避免这种操作。

Approach 1, for me, because IMHO a method should represent a single operation. In the interest of writing testable code or any form of automated testing, it would maintain a clear separation and make a lot more sense.

Approach 2 is leaning towards a "does everything" operation, which IMHO should be avoided.

等往事风中吹 2025-01-10 08:39:13

我更喜欢方法 1。它可以清楚地说明发生了什么。如果您使用方法 2 并调用

LockUnlockNews(42, true);

并不能立即明确这是锁定还是解锁。
只是火上浇油:如果你将 bool 更改为 enum 或 const,那么我的论点就无效了。

LockUnlockNews(42, LOCK);

就像

LockNews(42);

I prefer Approach 1. It makes it clear what is going on. If you use approach 2 and call

LockUnlockNews(42, true);

It is not immediately clear whether this is a lock or an unlock.
Just to throw some fuel on the fire: If you changed the bool to an enum or a const, then my argument is null and void.

LockUnlockNews(42, LOCK);

is just as clear as

LockNews(42);
背叛残局 2025-01-10 08:39:13

第一种方法。

您的方法是一个命令,并且应该尽可能明确。我什至会问你为什么除了新闻本身之外还有人知道如何锁定/解锁?对我来说新闻应该对此负责:

var news = GetNewsSomehow(newsId);
news.Lock();
news.Unlock();

更有意义,不是吗? :) 你可以清楚地看到你的对象是什么以及它们有什么行为。这就是 OOP 中所谓的封装

First approach.

Your method is a Command and should be as explicit as possible. I would even ask you why someone but News itself know how to lock/unlock? For me News should be responsible for it:

var news = GetNewsSomehow(newsId);
news.Lock();
news.Unlock();

Makes more sense, doesn't it? :) You clearly see what your objects are and what behaviors they have. This is what is called encapsulation in OOP.

许久 2025-01-10 08:39:13

我通常将这两种方法结合起来,如下所示:

公共方法提供清晰的界面并且更易于测试

public void LockNews(long newId)
{
    LockUnlockNews(newId, true);
}

public void UnlockNews(long newId)
{
    LockUnlockNews(newId, false);
}

私有方法执行实际业务提高模块化从一个位置管理事物:

private void LockUnlockNews(long newId,bool Islock)
{
    ......
}

I combine both approaches generally like below:

Public methods provide clear interface and are more testable:

public void LockNews(long newId)
{
    LockUnlockNews(newId, true);
}

public void UnlockNews(long newId)
{
    LockUnlockNews(newId, false);
}

Private method does actual business and increases modularity managing things from one location:

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