Personally, I like the assert statement.
But in this context, I prefer Try-Catch for complex problems and if-else for simpler problems.
If-else (s) are not readable and supported where there are many, especially for a complex problem. That is why the attempt of salvation comes to the rescue. Assuming this input is correct, let's process it. When this entry turned out to be incorrect on some lines, just go to the catch block and return the default value.
He argued that the try-catch attempt is slower. Well that's true. But how much slower? 0.1 ms? 1 ms? I will sacrifice these milliseconds for readability and maintainability.
After all, there are prerequisites for programming in every function / procedure. We all know what the precondition means. The best way (my personal opinion) is to claim that all parameters meet all the prerequisites. Thus, we can assume that this entry is always correct, since the assistants on the other floor already guarantee this.
Hendra jaya
source share