C does not have lists as part of the language, and definitely no list comprehensions. That's why you have to operate on arrays/vectors using indexes, or allocate or declare them explicitly, which makes the whole section a little bit more clumsy.
But if you reorder the very same statements you showed in your C example into a loop body like this
for (i=0; i<MAX_ELEMENTS; i++)
{
clamped[i] = iMAX( 64, iMIN( 128, source[i] ));
}
(adding brackets for those who dislike loop bodies without them, even for single statements), then I see no compelling reason why this could not pass a code review.
However, note the Python statement does more than the C code above: it also declares clamped_list
as a list of the same size as source_list
. If you add some equivalent of that to the code, the difference between C and Python becomes more obvious:
int i;
int clamped[MAX_ELEMENTS];
for (i=0; i<MAX_ELEMENTS; i++)
{
clamped[i] = iMAX( 64, iMIN( 128, source[i] ));
}
So the reasons here why the C code is more verbose (not more complex, the actual complexity is the same), are
- C has no inbuilt lists or list comprehensions, so explicit indexing/iteration is necessary
- C requires explicit declarations of variables and arrays
- Lots of C programmers prefer brackets even for one-liners.
Note there is also a synergy effect in this example: since the Python code requires so few "noise" code, putting the "min/max" statements with the list comprehension into just one line is still very readable. In C, it is probably better to put the code which describes the iteration on one line, and the min/max operation on a second, because both things together in one line becomes hard to comprehend.
Furthermore, since C++ 14 you can write such code almost as "dense" as in Python. Citing @Caleth's example from a comment, in modern C++ one can write the code this way
auto clamped_view = source | transformed([](auto i){ return max(64, min(128, i)); });
If that is as readable as the Python line is surely a matter of taste, and a matter of what kind of code one is used to.
auto clamped_view = source | transformed([](auto i){ return max(64, min(128, i)); });