Week 1 of officially starting (restarting) this project.
I created the test database via https://www.freemysqlhosting.net and begun working on the entities, repositories, services, and controllers for basic CRUD functions. My brain kept rushing to also start on searching book titles, logs dates, etc. But I knew that I needed to force myself to focus. So first the basic CRUD implementation, Swagger (finally! That’s another post to get it to run.) and test test test. I wish I could now jump to more fun queries but…
Found 35 TODO items in 10 files
nl.marisabel.imReadingAPI
domains
books
BooksServiceImplementation.java
(36, 5) // TODO never return null!
(52, 5) // TODO never return null!
googleSearchApi
GoogleBooksApiService.java
(29, 5) // TODO handle errors try/catch - wrong/expired API KEY, invalid URL
logs
LogsController.java
(36, 7) // TODO implement 601
(57, 7) // TODO implement 601
(58, 7) // TODO implement 901 - no data found
(69, 7) // TODO implement 601
(70, 7) // TODO implement 901 - no data found
LogsServiceImplementation.java
(31, 5) // TODO never return null!
(42, 5) // TODO never return null!
(56, 4) // TODO handle 601 & 901
(68, 7) // TODO return confirmation of deletion
(71, 4) // TODO handle 601 & 901
readingData
ReadingDataController.java
(41, 7) // TODO implement 601
(62, 7) // TODO implement 601
(63, 7) // TODO implement 901 - no data found
(74, 7) // TODO implement 601
(75, 7) // TODO implement 901 - no data found
ReadingDataServiceImplementation.java
(31, 5) // TODO never return null!
(59, 7) // TODO never return null!
(60, 4) // TODO handle 601 & 901
(72, 7) // TODO return confirmation of deletion
(75, 4) // TODO handle 601 & 901
shelves
ShelvesServiceImplementation.java
(34, 5) // TODO never return null!
(47, 5) // TODO never return null!
(64, 5) // TODO never return null!
(107, 5) // TODO never return null!
tags
TagsController.java
(31, 6) // TODO fix the error codes for 2 more global one (ID not found vs ISBN not found)
(38, 6) // TODO fix the error codes for a more global one (name duplicates)
TagsServiceImplementation.java
(34, 5) // TODO never return null!
(47, 5) // TODO never return null!
(64, 5) // TODO controller tag by name /tags/{name}
(65, 5) // TODO never return null!
Turns out, I first need to reach near perfection with the basic methods first. Implementing various responses and error responses. Also, update something I learned today from the Clean Code by Robert C. Martin. He advises against returning null and suggests returning appropriate empty values instead. This approach ensures you can work with results directly without extra checks. For example, returning an empty array for an expected array result. This way, you avoid forcing calling code to handle issues immediately, making exceptions a better choice in many cases than returning null.
This is one of the methods I need to update:
// TODO never return null!
@Override
public BooksDTO updateBook(String isbn, BooksDTO updatedBooksDTO) {
if (booksRepository.existsById(isbn)) {
BooksEntity updatedEntity = dtoToEntity(updatedBooksDTO);
updatedEntity.setIsbn(isbn); // Update the ID
BooksEntity savedEntity = booksRepository.save(updatedEntity);
return entityToDto(savedEntity);
}
return null;
}
Instead, we would change the null to:
throw new BookNotFoundException("Book with ISBN " + isbn + " not found");
I also maintain a list of specific error codes that I intend to refine to avoid redundancy before implementing them. It’s interesting how I now find myself planning error handling alongside URI paths and other aspects. In the past, I used to view errors as mere mistakes, but now I realize they are more like tools – perhaps annoying but necessary tools that enhance communication by defining what is expected and what is not.
Next week will be full of those.
Leave a Reply