Question

So I got a question. Today I started optimizing my code and got to this point for example:

return !this.message.startsWith("/");

Well follow where I took my optimization tips from here (linked to the rule)

And so how should be that code written to more optimized version? I currently got:

return !(this.message.length () > ZERO) && this.message.charAt(ZERO) == Slash;

Did I write it right? And if I wrote it wrong, then what did I write wrong?

Thanks, tambre

Was it helpful?

Solution

Put aside the validity of the rule you cite (which I disagree with, by the way, because I think making the code less readable is a worse sin than any minor performance improvement—if any—that transformation might provide), the resulting code should be:

return !(this.message.length () > ZERO && this.message.charAt(ZERO) == Slash);

You have the parentheses around only the first term of the condition, which is wrong. A slightly more readable version would be:

return this.message.length () <= ZERO || this.message.charAt(ZERO) != Slash;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top