3 ms·
Agree, especially as there's so little upside to writing it this way. It isn't going to run any faster. The style emphasises code-density over readability, but
by MaxBarraclough 6y ago
Agree, especially as there's so little upside to writing it this way. It isn't going to run any faster. The style emphasises code-density over readability, but density is only a virtue when it aids readability.
I'd far prefer:
++index;
ch = index < strlen ? str.charAt(index) : 0;
I don't see a good reason to make it a macro, though.
- kuschku 6y agoindex += 1; if (index < strlen) { ch = str.charAt(index); } else { ch = 0; } If I was using kotlin, I'd do it even cleaner: inline fun String.at(index: Int): Char? = if (index >= 0 && index < length) charAt(index) else null [...] index += 1 ch = str.at(index) ?: 0; The bytecode is exactly the same, but I’d argue it’s much cleaner
- mjcohen 6y agoI haven't used kotlin, but where is length defined? Should it be a parameter to the function?
- pirocks 6y agoThe above is an extension function(which is basically a member function of class not written in that class), which has an implicit `this` parameter. In other words `length` is actually `this.length`, just as it would be in a regular method.