"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Item - Return empty collections or arrays rather than null

Item - Return empty collections or arrays rather than null

Published on 2024-11-06
Browse:853

Item - Retorne coleções ou arrays vazios, em vez de nulos

Do not return null:

  • Methods that return null in place of empty collections or arrays require additional client handling to avoid exceptions.

Problems with null:

  • Clients need to add redundant checks (if to check for null).
  • Omissions in these checks may go unnoticed, resulting in bugs.
  • It makes it difficult to implement the method that returns the collection or array.

Argument against null:

  • Don't worry about the performance of allocating empty collections or arrays unless it is proven to be a bottleneck.

Efficient alternatives:

  • Use empty collections or arrays instead of null.
  • Immutable collections can be returned repeatedly (e.g.: Collections.emptyList(), Collections.emptySet()).
  • Empty arrays can also be returned efficiently.

Optimized performance:

  • Use reusable empty immutable collections to avoid unnecessary new allocations.
  • Return the same empty array instead of creating a new one each time

Code examples:
Incorrect method that returns null:

// Exemplo incorreto
public List getCheeses() {
    return cheesesInStock.isEmpty() ? null : new ArrayList(cheesesInStock);
}

Inadequate customer treatment:

List cheeses = shop.getCheeses();
if (cheeses != null && !cheeses.isEmpty()) {
    // Lógica para lidar com queijos disponíveis
}

Correct method that returns an empty collection:

// Exemplo correto
public List getCheeses() {
    return cheesesInStock.isEmpty() ? Collections.emptyList() : new ArrayList(cheesesInStock);
}

Using an immutable empty collection:

public List getCheeses() {
    return cheesesInStock.isEmpty() ? Collections.emptyList() : new ArrayList(cheesesInStock);
}

Use with empty arrays:

// Retorno de array vazio corretamente
public Cheese[] getCheeses() {
    return cheesesInStock.toArray(new Cheese[0]);
}

Optimized use of empty array:

private static final Cheese[] EMPTY_CHEESE_ARRAY = new Cheese[0];

public Cheese[] getCheeses() {
    return cheesesInStock.toArray(EMPTY_CHEESE_ARRAY);
}

Conclusion:
Never return null: Always prefer empty collections or arrays. This simplifies the API, prevents errors, and rarely negatively impacts performance.

Release Statement This article is reproduced at: https://dev.to/giselecoder/item-54-retorne-colecoes-ou-arrays-vazios-em-vez-de-nulos-1ej6?1 If there is any infringement, please contact [email protected] delete
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3