Kevin You
Kevin You

Reputation: 84

If code structure

I know basic if statement structure, but this confuses me

public void setHandleName(String handle){
    if(handle.length() < 10){
        return;
    }

    handleName = handle;
}

It returns the value when the handle.length() is bigger than 10, isn't it supposed to be returning the value when handle.length() is less than 10?

Why does this if statement does the opposite?

if(false){ //do stuff; }

Where the if statement should be

if(true){ //do stuff; }

Sorry for the vague explanation, english is not my native language. Any help will be appreciated! Thanks!

Upvotes: 0

Views: 83

Answers (4)

T.J. Crowder
T.J. Crowder

Reputation: 1074989

It returns the value when the handle.length() is bigger than 10

No it doesn't. It never returns any value, the method is of type void. It does set the handleName instance field if the name is at least 10 characters long (and not set it if the name is less than 10 characters long).

, isn't it supposed to be returning the value when handle.length() is less than 10?

No, the if clearly says "If the length of handle is less than 10, return" and that's before it sets handleName. return leaves the function right away, bypassing any code that may follow.

Why does this if statement does the opposite?

if(false){ //do stuff; }

Because in that case, the logic is within the if block; in your first example, it's after an if block that returns early (bypassing the remainder of the function). That is, your first example is:

if (!condition) {
    return;
}
doSomething();

but your second example is

if (condition) {
    doSomething();
}

Here's an annotated version of your first example:

public void setHandleName(String handle){ // Accept a `handle` string parameter
    if(handle.length() < 10){             // If the length of `handle` is less
                                          // than 10, enter the block
        return;                           // Leave this method immediately, without
                                          // doing anything else
    }                                     // This is the end of the conditional bit

    handleName = handle;                  // Set `handleName` to `handle`
}

So if we go into the if block, we'll return, and never reach the handleName = handle; line, so we never set it. If we don't go into the if block, we don't return early, so we do set it.

We can (and probably should) rewrite setHandleName to use the structure from your later examples:

public void setHandleName(String handle){ // Accept a `handle` string parameter
    if(handle.length() >= 10){            // If `handle`'s length is 10 or higher
        handleName = handle;              // Set `handleName` to `handle`
    }                                     // This is the end of the conditional bit
}

Upvotes: 4

OneCricketeer
OneCricketeer

Reputation: 191844

Does this make it clearer what the code is actually doing?

public void setHandleName(String handle){
    if(handle.length() >= 10){
        handleName = handle;
    }
    // otherwise handleName is not set, and the method exits
    // nothing is returned because the method is 'void'
}

Please note that if(false){ //do stuff; } will never "do stuff"

Upvotes: 0

Makoto
Makoto

Reputation: 106470

There are a few things to note:

  • The return type on this method is void. This means that it doesn't have a value to return. You can absolutely use return in a void method, but that implies...
  • This method returns early based on a condition. Effectively, if the value doesn't satisfy a specific criteria, it silently ignores what was passed into it.

Upvotes: 0

Korgen
Korgen

Reputation: 5399

It doesn't return anything (as the void return type indicates). It sets the value of handleName to handle, but only if handle.length() is greater or equal to 10. If handle.length() is less than 10 it will just return from the method without doing anything more.

Upvotes: 0

Related Questions