# Code Smell 251 - Collections Empty

> TL;DR: Use declarative names. Always

# Problems

- Readability

- [Cache](https://maximilianocontieri.com/code-smell-49-caches) Invalidation

- Performance Penalties

- Type Safety

# Solutions

1. Replace *count() == 0* and *size()==0* usages

# Context

*isEmpty()* and *count()==0* seem to be equivalent but have deep differences.

The semantics are clearer.

Skipping this declarative method violating the collection encapsulation might have performance issues.

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/8b7f7ec65cf3a60e31a3c31d4477615f)

```java
import java.util.EmptyStackException;
import java.util.Stack;

public class SchrodingerStack<T> {
    private Stack<T> stack;

    public SchrodingerStack() {
        stack = new Stack<>();
    }

    public void push(T item) {
        stack.push(item);
    }

    public T pop() {
        if (stack.size() == 0) {
            throw new EmptyStackException();
        }

        T item = stack.pop();
        return item;
    }

    public int size() {
        return stack.size();
        // This has O(n) linear time
        // And the stack might not be fully reachable in memory
        // While you wait, the stack isEmpty and notEmpty 
        // at the same time
    }

    public static void main(String[] args) {
        SchrodingerStack<String> stack = new SchrodingerStack<>();

        stack.push("Siamese");
        stack.push("Garfield"); 

        while (stack.size() > 0) {
            System.out.println("Popped element: " + stack.pop());
        }

        if (stack.size() == 0 ) {
            // Less readable
            // violating encapsulation
            // and coupled to the implementation
            System.out.println("The stack is empty.");
        } else {
            System.out.println("The stack is not empty.");
        }
    }
}
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/7e789f2e3e6e070e12177182192a62f7)

```java
import java.util.EmptyStackException;
import java.util.Stack;

public class SchrodingerStack<T> {
    private Stack<T> stack;
    private boolean isEmpty;

    public SchrodingerStack() {
        stack = new Stack<>();
        isEmpty = true;
    }

    public void push(T item) {
        stack.push(item);
        isEmpty = false; 
    }

    public T pop() {
        if (isEmpty()) {
            throw new EmptyStackException();
        }

        T item = stack.pop();
        if (stack.isEmpty()) {
            isEmpty = true;             
        }
        return item;
    }

    public boolean isEmpty() {
        return isEmpty;
        // This has O(1) constant time
    }

    public int size() {
        return stack.size();
        // This has O(n) linear time
        // And the stack might not be fully reachable in memory
        // While you wait, the stack isEmpty and notEmpty 
        // at the same time
    }

    public static void main(String[] args) {
        SchrodingerStack<String> stack = new SchrodingerStack<>();

        stack.push("Siamese");
        stack.push("Garfield"); 

        while (!stack.isEmpty()) {
            System.out.println("Popped element: " + stack.pop());
        }

        if (stack.isEmpty()) {
            // Semantic operation not violating encapsulation
            System.out.println("The stack is empty.");
        } else {
            System.out.println("The stack is not empty.");
        }
    }
}
```

# Detection

[X] Automatic 

You can check for this expression using syntax abstraction trees.

# Tags

- Readability

# Level

[X] Beginner

# AI Generation

LLMs generate abstractions using *empty()* functions

# AI Detection

Gemini detected the problem of using *count() == 0*

# Conclusion

Using *IsEmpty()* is recommended for checking if a collection is empty due to its clarity and potential performance benefits.

# Relations

%[https://maximilianocontieri.com/code-smell-233-collections-count]

# Disclaimer

Code Smells are my [opinion](https://maximilianocontieri.com/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned).

# Credits

Photo by [Valentin Lacoste](https://unsplash.com/@valentinlacoste) on [Unsplash](https://unsplash.com/photos/long-angle-photography-of-tunnel-jNSJE8dMro0)
    
* * *

> Good programming is good writing.

_John Shore_
 
%[https://maximilianocontieri.com/software-engineering-great-quotes]

* * *

This article is part of the CodeSmell Series.

%[https://maximilianocontieri.com/how-to-find-the-stinky-parts-of-your-code]
