Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there always isEmpty whereas I use !isEmpty 99% of the time [closed]

Tags:

java

This question has bogged me quite a while. During programming there is regularly the question whether there is something in an object or not. For this reason was the isEmpty method invented. Great, but in practice we use it like !isEmpty almost all the time.

As a consequence, notEmpty would be a much more appreciated addition to a language like Java. The question is: why don't language designers think of this before defining the API? At least give a counterpart for isEmpty

EDIT: I meant there should be a notEmpty as well as isEmpty. Depending on the domain, both of them may be used but in most cases when a UI is not involved, I think notEmpty applies better.

EDIT2: To close the discussion, here is an example:

!metadata.isEmpty() == metadata.notEmpty()

I'd prefer we had the right side of the equation as well.

like image 344
allprog Avatar asked Aug 16 '13 10:08

allprog


People also ask

What is the difference between isEmpty and isBlank?

isBlank() vs isEmpty() Both methods are used to check for blank or empty strings in java. The difference between both methods is that isEmpty() method returns true if, and only if, string length is 0. isBlank() method only checks for non-whitespace characters.

What does isEmpty () do in Java?

The isEmpty() method checks whether a string is empty or not. This method returns true if the string is empty (length() is 0), and false if not.

What is the difference between isEmpty and null?

The difference between null and empty is that the null is used to refer to nothing while empty is used to refer a unique string with zero length.

Does isEmpty checks for null?

isEmpty(<string>) Checks if the <string> value is an empty string containing no characters or whitespace. Returns true if the string is null or empty.


3 Answers

isEmpty follows the naming convention of other syntactically positive isXXX methods such as isVisible and isEnabled

like image 99
Reimeus Avatar answered Oct 04 '22 09:10

Reimeus


I think it follows the convention of using is[Something]() for booleans in place of get[Something]().

Also, having to use !notEmpty() might get a bit confusing!

like image 30
Louis Fellows Avatar answered Oct 04 '22 09:10

Louis Fellows


My opinion is, it is more logical because like == and !=. Logically It is better to have isEmpty and !isEmpty, It is easy to understand.

like image 25
Ruchira Gayan Ranaweera Avatar answered Oct 04 '22 08:10

Ruchira Gayan Ranaweera