With SQL functions, if a parameter is optional, should you still provide a value for it?

Question

With SQL functions like ROUND, if a parameter is optional, should you still provide a value for it?

Answer

Before I can give an answer to this question, it’s important to note that the documentation for functions like ROUND can slightly differ depending on the flavor of SQL you are using. In the SQLite documentation, we can see that the second parameter for the ROUND function is optional, whereas the SQL Server documentation shows that same parameter as required. Always make sure you’re checking the proper documentation!

As for the original question, it’s really up to you as the programmer to decide, you just have to be consistent with your decision. If you provide a value for an optional parameter, you should be doing that everywhere else in your code. The only real benefit to including a default value for an optional parameter is that it’s more explicitly clear as to what’s going on. Imagine someone reading your code who isn’t all that familiar with SQL, they might be able to get a better idea of what’s going on if you do provide values for the optional parameters.

9 Likes