Skip to main content
edited body
Source Link
Captain Man
  • 733
  • 1
  • 5
  • 15

java.util.Optional<T>

A short suggestion because no one has suggested it yet. Instead of returning null or throwing an exception when the book you are searching for isn't found, you should use Optional.

public Optional<Book> search(String title) {
    for (int i = 0; i < this.Volume; i++) {
        if (/* matching logic... */) {
            // logging...
            return Optional.of(storage[i]);
        }
    }
    // logging...
    return Optional.empty();
}

My reasoning against returning null is this: IsIt is never 100% clear if a method will return null or not in Java and maybe someone will think this won't so they won't guard against the null condition with if (foundBook != null) { ... }. If you return an Optional, it's an extremely clear signal that the content of the result may or may not be present. They still may handle it wrong but at least you can be more certain they know what's going on.

My reasoning against throwing some exception when the book is not present is this:

  1. If you throw a checked exception every single call of this method will need to be wrapped with try { Book b = search("blah") } catch (SomeException e) { ... }. Checked exceptions are a pain to deal.
  2. If you throw an unchecked exception then you have the same problem of null, maybe people don't realize it could happen. So they don't wrap it in a try-block.

java.util.Optional<T>

A short suggestion because no one has suggested it yet. Instead of returning null or throwing an exception when the book you are searching for isn't found, you should use Optional.

public Optional<Book> search(String title) {
    for (int i = 0; i < this.Volume; i++) {
        if (/* matching logic... */) {
            // logging...
            return Optional.of(storage[i]);
        }
    }
    // logging...
    return Optional.empty();
}

My reasoning against returning null is this: Is is never 100% clear if a method will return null or not in Java and maybe someone will think this won't so they won't guard against the null condition with if (foundBook != null) { ... }. If you return an Optional, it's an extremely clear signal that the content of the result may or may not be present. They still may handle it wrong but at least you can be more certain they know what's going on.

My reasoning against throwing some exception when the book is not present is this:

  1. If you throw a checked exception every single call of this method will need to be wrapped with try { Book b = search("blah") } catch (SomeException e) { ... }. Checked exceptions are a pain to deal.
  2. If you throw an unchecked exception then you have the same problem of null, maybe people don't realize it could happen. So they don't wrap it in a try-block.

java.util.Optional<T>

A short suggestion because no one has suggested it yet. Instead of returning null or throwing an exception when the book you are searching for isn't found, you should use Optional.

public Optional<Book> search(String title) {
    for (int i = 0; i < this.Volume; i++) {
        if (/* matching logic... */) {
            // logging...
            return Optional.of(storage[i]);
        }
    }
    // logging...
    return Optional.empty();
}

My reasoning against returning null is this: It is never 100% clear if a method will return null or not in Java and maybe someone will think this won't so they won't guard against the null condition with if (foundBook != null) { ... }. If you return an Optional, it's an extremely clear signal that the content of the result may or may not be present. They still may handle it wrong but at least you can be more certain they know what's going on.

My reasoning against throwing some exception when the book is not present is this:

  1. If you throw a checked exception every single call of this method will need to be wrapped with try { Book b = search("blah") } catch (SomeException e) { ... }. Checked exceptions are a pain to deal.
  2. If you throw an unchecked exception then you have the same problem of null, maybe people don't realize it could happen. So they don't wrap it in a try-block.
Source Link
Captain Man
  • 733
  • 1
  • 5
  • 15

java.util.Optional<T>

A short suggestion because no one has suggested it yet. Instead of returning null or throwing an exception when the book you are searching for isn't found, you should use Optional.

public Optional<Book> search(String title) {
    for (int i = 0; i < this.Volume; i++) {
        if (/* matching logic... */) {
            // logging...
            return Optional.of(storage[i]);
        }
    }
    // logging...
    return Optional.empty();
}

My reasoning against returning null is this: Is is never 100% clear if a method will return null or not in Java and maybe someone will think this won't so they won't guard against the null condition with if (foundBook != null) { ... }. If you return an Optional, it's an extremely clear signal that the content of the result may or may not be present. They still may handle it wrong but at least you can be more certain they know what's going on.

My reasoning against throwing some exception when the book is not present is this:

  1. If you throw a checked exception every single call of this method will need to be wrapped with try { Book b = search("blah") } catch (SomeException e) { ... }. Checked exceptions are a pain to deal.
  2. If you throw an unchecked exception then you have the same problem of null, maybe people don't realize it could happen. So they don't wrap it in a try-block.