Posts

Showing posts with the label Best Practices

Is accepting Optional as argument a code smell?

I was reviewing some piece of code and came across a function that was taking Optional  as argument. In the same class, another function was taking a couple of Optional as arguments. When I thought about it, I felt that taking an Optional should be avoided. You can return an Optional from a function, but avoid taking Optional arguments. Before calling a function, you should check for the presence or absence of the values that you are passing. Hence a function taking one or more Optional is a code smell. One argument that can possibly presented in favor of taking Optional as argument is every caller checking for the presence of the arguments. Consider the code example below: If you pay attention, the error handling from the caller's point of view is ugly if the caller wants to report correct error. Much worse, the error check is done after using the values. A side effect of product() taking Optional is that it must return an Optional. Otherwise it has to throw an ...

Have a timestamp, for Christ's sake!

This post is an appeal to all the developers and content writers. Please take it seriously. Let us help each other by following the simple guideline of timestamping each artifact that is produced. Whenever you encounter an issue, or while troubleshooting a problem, what is the first thing you do? I just copy and paste the error message in Google and search if someone else have encountered the same problem and if a resolution is available. Sometimes I get conflicting resolutions showing up in the Google results. For e.g. one site might suggest me to perform task A to resolve the problem, while another site might suggest to perform task B. If I have time, I will try each one of them until the issue is resolved. But unfortunately I find it a terrible waste of my time. Instead of appreciating the good intention of the author to share his wisdom, I end up cursing the person. Why? The reason most of the times is very simple. Those articles don't have a date and time about when they ...