3 ms·
I'd just like to point out that in your string example, you gain nothing by keeping that string copy short. The compiler doesn't see lines, all it sees are toke
by CountHackulus 16y ago
I'd just like to point out that in your string example, you gain nothing by keeping that string copy short. The compiler doesn't see lines, all it sees are tokens, and what you've written there is fine for a compiler to read, but difficult for a human.
Instead of adding parentheses, try writing it like this:
while(*dst)
{
*dst = *src;
dst++;
src++;
}
It's instantly more readably, it's very obvious what's going on, and you've lost no performance. There's also no parentheses to add.
- _delirium 16y agoI actually find that style considerably less readable, though admittedly I'm quite familiar with C code. It's just less common for one (not the idiomatic way of writing code in the language), but it also emphasizes this tedious loop-iteration-at-a-time interpretation, while the original is easy to read as a sort of Matlab-style single vector operation, once you're used to it ("copy all the srcs to all the dsts"). Plus, once you start piling on a bunch of such code-explosions, you end up with functions that are much harder to scan, because what could've been an easy to scan 8-line function is pedantically written out as this super-explicit 30-line thing.
- scott_s 16y agoIt's idomatic C. It's difficult for unexperienced C coders to read, but easy for experienced C coders who have read lots of other people's code.