for循环宏编码样式 [英] for loop macro coding style

查看:128
本文介绍了for循环宏编码样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在大学里的一位老师建议像这样使用宏来减少c99代码中的重复.

One of my tutors at university suggests using macros to reduce repetition in c99 code, like this.

#define foreach(a, b, c) for (int a = b; a < c; a++)
#define for_i foreach(i, 0, n)
#define for_j foreach(j, 0, n)
#define for_ij for_i for_j

可以这样使用:

for_ij { /*do stuff*/; }
for_i  { /*do stuff*/; }

另一位具有行业背景的导师不鼓励使用该软件,声称它被他的前雇主视为反模式(但他不知道其背后的原因).实际上,通过在大型项目的源代码中使用grep,很少有人会在简短的学术示例之外找到这些结构.

Another tutor with industrial background discourages its use, claiming it was seen as an anti-pattern at his former employer (but he does not know the reason behind this) . In fact by grepping through source code of large projects one rarely finds those constructs outside of short academic examples.

我的问题是:为什么这种构造在实践中很少使用?在某种程度上危险吗?

My question is: why is this construct so rarely used in practice? Is it dangerous to some extent?

推荐答案

这是对学术界与现实世界之间差距的完美说明,令人难以置信.但这看起来太不可思议了.

This is such a perfect illustration of the gap between academia and the real world, it is hard to believe. But it looks too weird to be made up.

要回答您的问题:在实践中使用这种构造,并且由于使用隐式变量和条件隐藏信息,因此存在风险.

To answer your question: NO this kind of construction is not used in practice and it is risky as it hides information, using implicit variables and conditions.

休闲读者会思考以下问题:

Here are some questions the casual reader will ponder about:

  • 上限是多少?不太明显,应该是n.
  • i的实际范围是多少?
  • 它是基于0的吗?
  • 它是否包含n或之前停止了?
  • what is the upper bound? It is not so obvious it should be n.
  • what is the actual range for i?
  • it is 0 based?
  • does it include n or stop before?

C程序员非常擅长阅读惯用结构,例如

C programmers are very good at reading idiomatic constructions such as

for (int i = 0; i < n; i++) {
    ...
}

隐藏此循环逻辑的详细信息不会节省任何费用,适得其反,容易出错,应使用本地编码约定禁止使用.不在我的团队中的唯一原因是没人想出这样一个怪异的想法.

Hiding the details of this loop logic saves nothing, is counter-productive, error prone and should be outlawed by local coding conventions. The only reason it isn't in my teams is nobody came up with such a weird idea.

您可能需要与想要使用这些缩写(可能是APL的怀旧名)的大学导师保持谨慎,并避免冲突.您可能想和他一起玩打高尔夫,那里有一个

You may need to use caution with the university tutor who wants these abbreviations used, possibly an APL nostalgic, and avoid conflict. You might want to play some code golfing with him, there is a stack exchange dedicated to that and he will love how people waste countless hours shaving bytes from their source codes...

但是您应该遵循另一位导师的建议,并编写清晰明了的代码,仔细缩进并隔开,在有意义的地方使用有意义的名称,在明显使用的地方使用简短的名称.尽可能使用惯用结构,因为它会使代码更易读,更不易出错并且更适合优化编译器.

But you should follow the other tutor's advice and write clear and explicit code, carefully indented and spaced out, with meaningful names where it matters and short ones where their use is obvious. Favor idiomatic constructions where possible as it makes the code more readable, less error prone and better fit for optimizing compilers.

这篇关于for循环宏编码样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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