Let's say I have a function that looks like this:
public void saveBooking() {
Before saving to the database, I have to perform various checks. What I can do in my main program is as follows:
//do all the validations and do any necessary handling. Then... saveBooking();
At the same time, I am sure that all data must pass all the checks necessary to save it in the database. However, this means that the saveBooking() function is saveBooking() dependent on validation methods. Every time I want to call saveBooking() , I have to make sure that I remember calling checks.
Alternatively, I can put all the checks inside the function itself so that all I had to do is call the method and everything will take care. However, in order to handle all errors on my own, I need to make an exception function and catch it in the main program. It should look something like this:
public void saveBooking() {
It also means that I have to create a few exceptions myself. Itβs good that I donβt have to worry about which checks I should set for myself.
With these, I'm not sure which one is the best code design. I personally prefer the first method, which is more readable, but it depends too much on each other, and it gets worse when I need to use it in many places. Please advice!
java
Davuth
source share