|
|
|
|
|
by polyfractal
4670 days ago
|
|
Functions should be scoped appropriately so that they only deal with one thing at a time. A function that checks for network connectivity isn't going to be checking for missing rows. Those are different problems, and should be handled by different functions. Using your example, we have a function that queries a database. It will be given a valid database connection, and return the result of the query. There is no "valid database connection" logic, since that is handled elsewhere. There is no result validation logic, since that is handled upstream. This function only cares about A) querying and B) returning a value (possibly null). The only exceptions that is handles is when something specific to it's domain goes wrong - for example, unauthorized access to a table. That is an error that is above networking (the connection worked fine) but clearly not a data validation problem (no data), so we handle the exception here. If you find yourself throwing exceptions "across problem domains", that's a good indicator that your functions are doing too much. |
|