Avoiding inter-module dependencies, avoiding event as a global variable – What should be given more importance? why?

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

Avoiding inter-module dependencies, avoiding event as a global variable - What should be given more importance? why?

问题

方法1 ->
模块A
a.c

#include <os_wrapper.h>
event* a;
createEventFlag(a); - RTOS function

OS包装
os_wrapper.h

extern event* a;

模块B
b.c

#include <os_wrapper.h>
.
.
postEventFlag(a);

方法2 ->
模块A
a.c

#include <os_wrapper.h>
static event* a;
createEventFlag(a); - RTOS function

eventFlagPostWrapper(); - 用户定义的包装函数

a.h

void eventFlagPostWrapper(void);

模块B
b.c

#include <os_wrapper.h>
#include <a.h>

.
.
eventFlagPostWrapper();
英文:

Approach 1 ->
module A
a.c

#include &lt;os_wrapper.h&gt;
event* a;
createEventFlag(a); - RTOS function

OS wrapper
os_wrapper.h

extern event* a;

module B
b.c

#include &lt;os_wrapper.h&gt;
.
.
postEventFlag(a);

Approach 2 ->
module A
a.c

#include &lt;os_wrapper.h&gt;
static event* a;
createEventFlag(a); - RTOS function

eventFlagPostWrapper(); - user defined wrapper function 

a.h

void eventFlagPostWrapper(void);

module B
b.c

#include &lt;os_wrapper.h&gt;
#include &lt;a.h&gt;

.
.
eventFlagPostWrapper();

答案1

得分: 1

两种方法都存在模块间的依赖关系。方法1中两个模块都依赖于 a,而方法2中依赖关系是 eventFlagPostWrapper。因此,都没有避免模块间的依赖关系,因此您最好的方法是避免全局变量,因为耦合度相同,但安全性更高。

您可以考虑查看Jack Ganssle的 A Pox on Globals,该文直接解决了这个问题。

英文:

Both approaches have an inter-module dependency. Approach 1 has both modules dependent on a while Approach 2 the dependency is eventFlagPostWrapper. So neither avoids inter-module dependency, so your best approach then is the one that avoids the global variable, since the coupling is the same, but the safety improved.

You might consider Jack Ganssle's A Pox on Globals which directly addresses this issue.

huangapple
  • 本文由 发表于 2023年6月5日 13:23:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403667.html
匿名

发表评论

匿名网友

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

确定