[For our newcomers] About isXXX methods

For example, isXXX methods?

There are ʻisFile and ʻisDirectory methods of the File class. File class official document

feature is?

It simply returns a boolean. boolean is a data type that represents a boolean value such as true or false. It can be used as a branch condition for ʻif statementsand as a continuation condition forwhile statements`.

code example of isXXX method

As an example, let's create the ʻisBlank` method.

isBlank


//Determines if the specified value is an empty string.
//True if empty string.
//Otherwise it returns false.
public boolean isBlank(String value) {
  if (value.isEmpty()) {
    return true;
  }

  return false;
}

How to use?

The code is shown using the ʻis Blank` created earlier as an example.

First is the ʻif statement`.

Used as a branch condition


if (isBlank(value)) {
  //Processing when no characters are entered
  System.out.println("No characters have been entered.");
} else {
  //Processing when characters are entered
  System.out.println("The entered characters are "" + value + ""is.");
}

ʻIf (isBlank (value))` By writing like this, You can clearly convey to the reader the intent that "if no value has been entered".

Next is the while statement. ! Is Blank is used to invert true and false.

Use as a continuation condition


while (!isBlank(value)) {
  //Processing when characters are entered
  System.out.println("The entered characters are "" + value + ""is.");
}

It is while (! IsBlank (value)). As with the ʻif statement`, the intent is clear if a value has been entered.

value.isEmpty () is fine

It's not a mistake. However, the definition of blank can change from time to time. The previous ʻis Blank` only determines whether it is an empty string.

What if the argument is null? If you make a method call to null, you will get an exception.

//java if the content of value is null.lang.NullPointerException occurs
value.isEmpty()

Corresponds to null

By making the empty string judgment process a method, you can easily handle null. Now, let's actually make ʻis Blank correspond to null`.

isBlank


//Determines if the specified value is null or an empty string.
//True if null or empty string.
//Otherwise it returns false.
public boolean isBlank(String value) {
  if(value == null) {
    return true;
  }

  if(value.isEmpty()) {
    return true;
  }

  return false;

  //You can write as follows, but it is for newcomers.
  // return value == null || value.isEmpty();
}

I just added the following logic to ʻisBlank`.

Added line


  if(value == null) {
    return true;
  }

In this way, you can apply the changes to all the places that call ʻisBlankby making only one modification. It's okay to say thatblank also includes null` when you've finished writing the code and are about to test it.

Summary

Someone else may take care of the code tomorrow, a week later, or a year later. If you put together the processing like this, it will be easier later.

Recommended Posts

[For our newcomers] About isXXX methods
About Ruby methods
[For newcomers] Introducing JUnit
About Ruby instance methods
About calling instance methods
A story about starting a Java-related book club for newcomers
Find out about class methods
About for statement and if statement
About validation methods in JUnit
About naming Rails model methods
About pluck and ids methods
About Java class variables class methods
About Popover fix for iOS 13