3 ms·
You probably want to use the conceptually simplest construct your language / standard library offers. In Python that's enumerate. In C, that'd be the standard f
by Zelizz 9y ago
You probably want to use the conceptually simplest construct your language / standard library offers. In Python that's enumerate. In C, that'd be the standard for loop. In Java that'd be the enhanced for loop. People reading your code, familiar with the language, should have to learn as little as possible to understand it.
I could make an awful enumerate implementation in C:
#define enumerate(item, array) for (int i = 0; i < sizeof(array)/sizeof(array[0]); i++) { item = array[i];
#define enumerate_end }
...
enumerate(int number, numbers)
printf("Number is %i\n", number);
enumerate_end
...But then everyone reading it would have to go read my enumerate definition, whereas they'd understand the regular for loop immediately.
I think higher-order functions are a great tool when they're used to remove complexity. The tricky part is that your reader needs to already understand how they work. If adding the definition of a higher-order function would make your code longer than simply not using it, I think it's a bad case to use it.