Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why java has no not function/ alternatives for same [closed]

Ok so if you want to check if a list is not empty

we would need to do something like

if(! mylist.isEmpty())

this affects code readability, so how can we write the same thing in a readable way, calling out negation of condition check.

One of possibility is to have a static helper function like:

static boolean not(boolean condition) { return !condition;}

How bad is this idea? Are there other options in apache common or guava etc? Or any other way you have achieved this?

like image 568
Ankur Avatar asked Aug 26 '26 12:08

Ankur


1 Answers

It is not a bad idea in itself and it will not affect anything. However I think many people will disagree with you regarding the "unreadability" of the normal way of negating things using !.

if ( ! list.isEmpty() )

versus

if ( not( list.isEmpty() ) )

does not make much of a difference IMHO.

like image 138
mvreijn Avatar answered Aug 28 '26 01:08

mvreijn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!