3

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?

curious_cat
  • 886
  • 2
  • 11
  • 14
Ankur
  • 788
  • 6
  • 13
  • 9
    What is _unreadable_ about a simple negation? – devnull Mar 02 '14 at 09:11
  • its simple in this case what if orginal function on collection was list.isNotEmpty() and I am negating using ! sign. If there are nested braces that would make it less readable. – Ankur Mar 02 '14 at 09:13
  • I agree its not that it is unreadable as I mentioned affects readablity, again which depends on indvidual. – Ankur Mar 02 '14 at 09:15
  • 1
    use [`CollectionUtils#isNotEmpty`](http://commons.apache.org/proper/commons-collections/javadocs/api-3.2.1/org/apache/commons/collections/CollectionUtils.html#isNotEmpty(java.util.Collection)) ? –  Mar 02 '14 at 09:15
  • Don't you think if `!` is that much useless that even cannot be used here, so what's it made for at all? – Mohsen Kamrani Mar 02 '14 at 09:16
  • 1
    Java (and C, and C++, and ...) doesn't *need* a 'not' function. It has a 'not' *operator.* You may not like how it is spelt, or looks, but you don't introduce redundancy into programming languages; at least not when you know what you're doing when you design it. – user207421 Mar 02 '14 at 09:23

1 Answers1

4

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.

mvreijn
  • 2,807
  • 28
  • 40
  • 1
    In fact first one gives less keystrokes than second one :) – Pradeep Simha Mar 02 '14 at 09:16
  • 2
    ...which is a plus, since programmers are lazy ;-) – mvreijn Mar 02 '14 at 09:18
  • ide helps with auto suggest for those things, my point is for readablity... – Ankur Mar 02 '14 at 09:23
  • 1
    @Ankur: every Java developer knows that `!` means `not`, and reads it as "not" in his/her head. Maybe you're not used to it yet, but it will come. Using `not()` decreases readability, because when I read this, I wonder what `not()` might do and I'm forced to check that it's simply a proprietary alias to to standard `!` operator. – JB Nizet Mar 02 '14 at 09:32
  • @JBNizet yes you are right we might need to check what not does, tahts why question any of standard lib gives any options, yes what i read ! is again depends on individual hence the question... – Ankur Mar 02 '14 at 09:42
  • Why would you need a standard API when you have a one-letter standard operator? – JB Nizet Mar 02 '14 at 09:43