How can I improve this while loop code snippet?

seanballais

Sean Francis N. Ballais

Posted on April 8, 2018

How can I improve this while loop code snippet?

So, I have this code snippet.

while (!Character.isWhitespace(this.source.charAt(this.currentCharIndex))) {
    char c = this.source.charAt(this.currentCharIndex);
    // ...
}
Enter fullscreen mode Exit fullscreen mode

What it does is just basically loops through a string until it hits a white space character.

However, I find the code a bit messy. How can I improve it?

I can try this code snippet instead.

char c = this.source.charAt(this.currentCharIndex);
while (!Character.isWhitespace(c)) {
    c = this.source.charAt(this.currentCharIndex);
}
Enter fullscreen mode Exit fullscreen mode

It looks cleaner but this.source.charAt(this.currentCharIndex) seems redundant (the same as the previous snippet) so I need your opinion on how you would tackle this problem.

Thanks! :D

đź’– đź’Ş đź™… đźš©
seanballais
Sean Francis N. Ballais

Posted on April 8, 2018

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related