gpt4 book ai didi

solid-principles - SOLID 原理,以及类内的硬编码配置

转载 作者:行者123 更新时间:2023-12-04 20:46:48 27 4
gpt4 key购买 nike

我最近在很多代码中注意到人们将硬编码的配置(如端口号等)值放在类/方法的深处,使其难以找到,也无法配置。

这是否违反了 SOLID 原则?如果不是,我是否可以向我的团队成员引用另一个“原则”来说明为什么这不是一个好主意?我不想只是说“这很糟糕,因为我不喜欢它”,但我很难想出一个好的论点。

最佳答案

反对在类中硬编码 TCP 端口号的一个很好的论据是违反“上下文独立性”。来自 GOOS ,我的重点是:

Context Independence

... the "context independence" rule helps us decide whether an object hides too much or hides the wrong information. A system is easier to change if its objects are context-independent; that is, if each object has no built-in knowledge about the system in which it executes. This allows us to take units of behavior (objects) and apply them in new situations. To be context-independent, whatever an object needs to know about the larger environment it’s running in must be passed in.



在此特定情况下 Context Independence我称之为“环境独立”。换句话说,具有硬编码端口号的类对运行时操作系统环境有不适当的依赖性,本质上说“我知道端口 7778 将始终可用”,这显然是错误的。

关于solid-principles - SOLID 原理,以及类内的硬编码配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16448076/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com