如何在一个非常庞大的代码库中维护枚举(以及添加新值)

huangapple go评论129阅读模式
英文:

How to maintain enums (and adding new values) in a very large codebase

问题

在一个非常庞大且古老的代码库中,假设我们正在使用一个数据库列 status,其枚举值为 NEWIN_PROGRESSCOMPLETEDREJECTED

现在,status 在代码中用于多个条件,比如:

if (status == 'NEW' || status == 'IN_PROGRESS') {
  // 进行某些操作
}

或者可能在一些 SQL 语句中,例如:

WHERE status NOT IN ('REJECTED')

现在,如果我们想要向 status 中添加一个新的枚举值,比如 'CANCELLED',那么我们需要处理代码中所有使用 status 的地方。考虑到代码库可能相对分散、庞大且相当古老,这种更改将会非常困难。我们如何改进这一点,使得更容易维护这类更改呢?

我并没有涉及任何特定编程语言,只是探讨概念,因为这应该适用于多种编程语言。

英文:

In a very large and old codebase, suppose we're using a database column status with enum values as NEW, IN_PROGRESS, COMPLETED, REJECTED.

Now this status is used in multiple conditions in code like

if (status == `NEW` || status ==  `IN_PROGRESS`) {
  // do something
}

or might be in some SQL statement like

WHERE status NOT IN ("REJECTED")

Now if we want to add a new enum value to status eg. "CANCELLED", then we'd have to handle all the places where status was used in the code.

Considering that the codebase can be somewhat distributed, large and quite old, it would prove to be very difficult for this sort of change. How can we improve this such that it would be easier to maintain these sort of changes?

I'm not writing about any particular language, just the concept, as it should be replicable for multiple languages.

答案1

得分: 3

这是Java还是JavaScript?无论如何,您应该能够向您的枚举添加一些多态行为。这意味着枚举的每种类型都知道如何执行某些操作,或者是否允许执行某些操作。让我们看一个简单的Java示例:

public enum Status {
    NEW(true),
    IN_PROGRESS(true),
    REJECTED(false);

    private final boolean allowsSomething;

    Status(boolean allowsSomething) {
        this.allowsSomething = allowsSomething;
    }

    public boolean allowsSomething() {
        return allowsSomething;
    }
}

现在,让我们重构您的方法。不再使用这种方式:

void someMethod() {
    Status status = someStatus();
    if (status == Status.NEW || status == Status.IN_PROGRESS) {
        // do something
    }
}

我们将使用以下方式:

void someMethod_refactored() {
    Status status = someStatus();
    if (status.allowsSomething()) {
        // do something
    }
}

假设您现在想要向枚举添加一个额外的值,例如CANCELLED。您将被迫指定allowsSomething标志,然后这个枚举的所有客户端将正常工作。

英文:

is this Java or JavaScript? anyway, you should be able to add some polymorphic behavior into your enum. This means each type of the enum knows how to do something or if it allows doing something. let's look at a simple Java example:

public enum Status {
	NEW(true),
	IN_PROGRESS(true),
	REJECTED(false);

	private final boolean allowsSomething;

	Status(boolean allowsSomething) {
		this.allowsSomething = allowsSomething;
	}

	public boolean allowsSomething() {
		return allowsSomething;
	}
}

Now, let's refactor your method. Instead of this:

void someMethod() {
	Status status = someStatus();
	if (status == Status.NEW || status == Status.IN_PROGRESS) {
		// do something
	}
}

We'll use it like this:

void someMethod_refactored() {
	Status status = someStatus();
	if (status.allowsSomething()) {
		// do something
	}
}

Let's assume you now want to add an additional value to the enum, for example, CANCELLED. You will be forced to specify the allowsSomething flag, and then all the clients of this enum will work correctly.

huangapple
  • 本文由 发表于 2023年3月12日 19:53:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75712920.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定