4 ms·
Not to argue your point but I think the "elite" old-sk00l C style implementation would be this: void truncate(char *s) { if (s && *s) s[strlen(s)-1] = 0; }
by foxfluff 5y ago
Not to argue your point but I think the "elite" old-sk00l C style implementation would be this:
void truncate(char *s) { if (s && *s) s[strlen(s)-1] = 0; }
I don't have a hard and fast rule on when to use const for locals. In bigger functions, sometimes adding a const is just a nice quick way to ask the compiler if that variable gets modified later. So it can help readability. With tiny functions, it rarely matters much, you see everything at glance anyway. That's bikeshedding territory.
I tend to prefer to elide unnecessary temporary variables (then you don't need to worry whether it's const or how it should be named.. problem eliminated) and just use expressions like above. But this is also not a hard rule. In general, any hard rule smells like cults and religion.