1
boolean listIsEmpty = myList.size()==0; 

Will listIsEmpty change to false as soon as the size of myList changes of is this a one-time assignment? If the latter, can I have a 'dynamic' boolean that 'watches' myList?

fge
  • 119,121
  • 33
  • 254
  • 329
fweigl
  • 21,278
  • 20
  • 114
  • 205
  • 1
    Please note that `.isEmpty()` is more efficient than `size() == 0`. – Uwe Plonus Jul 03 '13 at 13:49
  • 2
    @UwePlonus Is it really? – millimoose Jul 03 '13 at 13:51
  • 2
    @UwePlonus It may be clearer, but seriously? You're claiming that there's actually a performance improvement? You'll have to back that up with a source. – Kayaman Jul 03 '13 at 13:52
  • `isEmpty` vs `size` http://stackoverflow.com/questions/9341740/ifliststr-size-0-versus-ifliststr-isempty – Ajinkya Jul 03 '13 at 13:53
  • @Karna That answer is patently wrong. – millimoose Jul 03 '13 at 13:55
  • boolean is a primitive type thus it stores a value and NOT a reference. – Multithreader Jul 03 '13 at 13:57
  • 1
    Spoilers: [`LinkedList.size()`](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/util/LinkedList.java#LinkedList.size%28%29), [`AbstractCollection.isEmpty()`](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/util/AbstractCollection.java#AbstractCollection.isEmpty%28%29) which is inherited in `LinkedList`. `LinkedList` keeps track of the item count explicitly - and in fact, it's possible for pretty much every common collection type to do so. This means that `isEmpty()` is (theoretically) **slower** because of the extra call. – millimoose Jul 03 '13 at 13:58
  • Of course in reality it makes no difference either way, whats more important is readability – Richard Tingle Jul 03 '13 at 14:03
  • @RichardTingle Except that "readability" is subjective, when people say "this is more readable" they tend to mean "this is what I'm used to" or "this is what I like more". Consider that .NET doesn't have an equivalent of `isEmpty()`, the convention there is to use `x.Count == 0`. Does that make the code unreadable? Hardly. Does it make the code "less readable"? How do you quantify that? (I admit I might have a peeve here, having experienced too many debates over minutiae of style that amounted to little else than people airing out their control issues.) – millimoose Jul 03 '13 at 14:25
  • @millimoose Of course, frankly I think they're both as readable as each other and I don't mind which one people use. What I'm saying is that if the performance difference (if it even exists) is a billionth of the total program run time then the only rational debate you can have is over readability and and performance difference is being used as a proxy war for opinions people actually have based on readability – Richard Tingle Jul 03 '13 at 14:32
  • @RichardTingle So, basically, there's no rational debate in sight when you're discussing differences this tiny. – millimoose Jul 03 '13 at 15:39
  • @millimoose For interest only its interesting, but I wouldn't base what to actually do on it – Richard Tingle Jul 03 '13 at 15:53

6 Answers6

10

Simple answers: no and no.

The expression is evaluated once and then the result in your boolean will not change.

To have such a 'dynamic' boolean you have to create your own list with a listener that will achieve his.

Uwe Plonus
  • 9,803
  • 4
  • 41
  • 48
3

No, because this is not defining a mathematical relationship, it is defining a set of instructions. After the instructions are executed, one cannot expect listIsEmpty to automaically update.

To do automatic updates, look into the "Listener Pattern".

To write this condition in a more sensible manner, it is the List's responsibility to know if it is empty, which is why it contains an isEmpty() method. Instead of caching this information and acting on the cache, delegate the request.

if (myList.isEmpty()) {
  ... do stuff ...
}

Will prevent the cache from returning a stale view of the list's emptiness.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
2

Your listIsEmpty won't change unless you go over this specific line of code again

To have your boolean change as soon as your list size increases, should make a method that checks your list size whenever you add or remove something.

Maroun
  • 94,125
  • 30
  • 188
  • 241
JREN
  • 3,572
  • 3
  • 27
  • 45
2
boolean listIsEmpty() {
    return myList.isEmpty();
}

A variable assignment is evaluated at that spot.

Using a function like above, if (listIsEmpty()) { ... } evaluates the function (method) body everytime.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • Why do you want to wrap this in yet another method call? That's adding another layer unnecessarily. – Makoto Jul 03 '13 at 13:51
  • Much better Joop, this makes sense now – JREN Jul 03 '13 at 13:52
  • @Makoto but the question was, whether an expression can be named and using the name reevaluated. – Joop Eggen Jul 03 '13 at 13:53
  • I disagree. The question seemed more like they wanted a way to dynamically check the size of the list, and `myList.isEmpty()` more than accomplishes that without the need to wrap it in a method call. – Makoto Jul 03 '13 at 13:55
1

The way your code is written now, it won't change until the size is re-evaluated. But, instead of doing that, you can use the isEmpty() check directly on your list; this way, you don't have to constantly monitor the list's size.

Makoto
  • 104,088
  • 27
  • 192
  • 230
1

Your ListisEmpty varible is not gonna change untill you change it yourself. you have to update your ListIsempty variable each time you update your Mylist ,

Kas
  • 3,747
  • 5
  • 29
  • 56