关于全球变革的意见 [英] opinion wanted about global vars

查看:49
本文介绍了关于全球变革的意见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在开发一个应用程序,它将在没有操作系统的情况下运行。

应用程序是由几个构建的定义明确的部分。起初我想要b / b
想要将这些部分分开并使用不透明的数据类型来支付
在它们之间传输信息。

在某些阶段我是卡住并需要将变量设为全局变量,并且我还需要将b * b用于将结构声明公开给其他部分。


查看我发现的代码用于改变数据结构的许多函数只包含一两条

行。所以我编写宏来加快速度。

通过程序集我发现编译器会因为

一些函数调用>
代码包括宏。


现在对于我真正的问题,您对使用全局

变量/结构和宏的看法是什么?要在这种环境中访问它们吗?

我通常会尽量避免使用全局变量,但在这种情况下,我看到了一些特别的优点,尤其是不透明数据

方法。


马克

Hi,

I am working on an application, which will run embedded without an OS.
The app is build up out of a couple of well defined parts. At first I
wanted to keep those parts seperated and use opaque data types to
transfer information in between them.
At some stage I was stuck and needed to make a variable global, and I
also needed to make the struct declaration public to some other parts.

Looking through the code I found out that lots of functions, which were
intended to alter the data structure would contain only one or two
lines. So I wrote macros to speed things up.
Looking through the assembly I discovered that the compiler would leave
some function calls completely out because of the optimisation of the
code including the macro.

So now for my real question, what is your opinion on using global
variables/structures and macro''s to access them in this sort of environment?
I normally try to avoid global variables as much as possible, but in
this case I see somereal advantages esspecially over the opaque data
approach.

Mark

推荐答案



" Capstar" < SP *** @ eg.homeip.net>在消息中写道

新闻:3F ************ @ eg.homeip.net ...

"Capstar" <sp***@eg.homeip.net> wrote in message
news:3F************@eg.homeip.net...
现在我的真实问题,是什么您是否认为在这种
环境中使用全局
变量/结构和宏来访问它们?我通常尽可能地避免使用全局变量,但在这种情况下,我看到了一些特别的优点,特别是对于不透明数据的方法。
now for my real question, what is your opinion on using global
variables/structures and macro''s to access them in this sort of environment? I normally try to avoid global variables as much as possible, but in
this case I see somereal advantages esspecially over the opaque data
approach.




(全局我的意思是一个具有不确定范围和无限范围的变量)

(结构化代码是不使用任何无条件跳转的代码而且没有

代码的路径可以相互交叉)


如果设计变得更好,那么使用全局变量。避免全局化是

不是
本身就是一个目标。目标是制作更好的设计和更易读的代码。


结构化编程也是如此,它不是

本身的目标。 br />
如果转到或中断或继续使你的代码更好,那么你应该使用

它们。

不这样做是错误的。 (或至少不太好)。我曾经读过一个比较

两个函数代表相同的抽象,一个使用goto,一个

没有

goto。结论是实现或多或少相当于
等于

,因为没有goto的函数更复杂,更难以阅读

of

两个,带goto的函数很糟糕,因为它使用了goto。那只是

idiotic。

不记得那是什么。


不安全代码和全局变量的情况欲望是罕见的,但

not

闻所未闻。如果你只是按照一般原则避开它们那么你就是在做错了。如果你需要全局变量,试着让它们变得静止,如果这样会伤害

更多

比它有助于不让它们变得静止。


关于全局变量的坏处是,所有

函数都有一个新的潜在副作用,可以访问该变量并且可能更多

困难
在使用它们的程序中引入线程。


请记住,避免goto和globals本身并不是一个目标而且



结构化代码本身并不是一个目标。


除非您正在进行学术练习。 (没有去那些

那些,

只是说有时候他们会成为自己的目标)。


我猜我说的是永远不会和总是

软件工程总是错误的。 :p $ />

-

托马斯,实用主义者。



(By global I mean a variable with indefinite scope and indefinite extent)
(Structured code is code that does not use any unconditional jumps and none
of the paths the code can take crosses over each other)

If the design turns out to be better, then use globals. Avoiding globals is
not
a goal in itself. The goal is to make better designs and more readable code.

It is the same thing with structured programming, it is not a goal in
itself.
If goto or break or continue makes your code better then you should use
them.
Not doing so is wrong. (or at least less good). I once read a comparison of
two functions representing the same abstraction, one using goto and one
without
goto. The conclusions was that the implementation was more or less
equivalent
because the function without goto was more complex and a bit harder to read
of
the two, the function with goto was bad because it used goto. That is just
idiotic.
Can''t remember where that was though.

Situations where unstrucured code and globals are desireable are rare, but
not
unheard of. If you avoid them just on general principle then you are doing
something wrong. If you need globals, try to make them static, if that hurts
more
than it helps do not make them static.

Bad thing about globals is that there is a new potential side effect in all
functions that has access to that variable and that it might be more
difficult
to introduce threading in a program that uses them.

Just remember that avoiding goto and globals is not a goal in itself and
that
structured code is not a goal in itself.

Unless of course you are doing an academic exercise. (Not having a go at
those,
just saying that sometimes they become a goal in themselves).

I guess I am saying that the words "never" and "always" are always wrong in
software engineering. :p

--
Thomas, the pragmatist.


Capstarescribió:
Capstar escribió:
我正在开发一个应用程序,它将在没有操作系统的情况下运行嵌入式。
I am working on an application, which will run embedded without an OS.




然后,为什么要在组中发布有关编程的内容对于一个具体的操作系统?


问候。



Then, why you post in a group about programming for a concrete OS?

Regards.


JuliánAlbo< JU ******** @ terra。 ES>在comp.lang.c上潦草地写下以下

Julián Albo <JU********@terra.es> scribbled the following
on comp.lang.c:
Capstarescribió:
Capstar escribió:
我正在开发一个应用程序,它将运行嵌入式OS。
I am working on an application, which will run embedded without an OS.


然后,为什么你在一个小组中发布有关具体操作系统的编程?

Then, why you post in a group about programming for a concrete OS?




为什么你在一个关于编程的小组中发帖,没有

与操作系统有什么关系?


-

/ - Joona Palaste(pa ** ***@cc.helsinki.fi)--------------------------- \

| 飞翔的柠檬树中的金鸡王G ++ FR FW + M-#108 D + ADA N +++ |

| http://www.helsinki.fi/~palaste W ++ B OP + |

\ -----------------------------------------芬兰的规则! ------------ /

微软做出一些不吸的事情的那一天可能是他们开始制造真空的那天清洁工。

- Ernst Jan Plugge



Why do YOU post in a group about programming, which does not have
anything to do with OSes?

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"The day Microsoft makes something that doesn''t suck is probably the day they
start making vacuum cleaners."
- Ernst Jan Plugge


这篇关于关于全球变革的意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆