For loop macro coding style

One of my university professors 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

What can be used as follows:

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

Another teacher with an industrial background discourages him from using, claiming that he was seen as an anti-pattern from his former employer (but he does not know the reason for this). In fact, using grepping through the source code of large projects, it is rarely possible to find these constructs outside of short academic examples.

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

+4
source share
2 answers

, . , .

: , , , , .

, :

  • ? , n.
  • i?
  • 0?
  • n ?

C ,

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

, , . , , .

, , , APL . , , , , , ...

, , , , , . , , .

+5

, (, , ) .

for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)

to

for_ij

, i, j n , .

n - n, n for_ij () . - , n, .

+3

Source: https://habr.com/ru/post/1663464/


All Articles