There is not always data for your code

When we are developing new functionality or performing tests in the application, it is very common to focus only when there is data to show in the view, what will happen if there is no data stored? For example, the first time the user is using the application.

I learned to always take into account cases when information does not exist the hard way. One time I was programming a new functionality within the application, at that time did not take into consideration that not all the necessary data will always exist to use in the view. I remember that when I tested this new view locally, everything worked without problems (because there was always data), I even added tests that passed the circleCI without problems.

Guess what happened when the client first tried using the view in staging? Many common javascript problems began to occur, such as the typical undefine is not an object, because the variables were null and an attempt was made to access a property that did not exist. When the client notified me about the error, it was very strange for me to hear that the view gave an error, after having used it without any problems and even added the tests that were working properly.

When I start to review the code, the first thing that comes to mind is: The necessary information will not always exist. After I notice this added an “if condition” to avoid the problems that were occurring and voila! Everything started to work as it should be.

Another very important aspect that must be taken into account is the design of the view when there is no information. I was recently working with charts in javascript. When I started to design the view, it looked very good, but when there was no data, it looked terrible because the graphics were empty causing the view to be seen with a lot of white space.

To solve this problem I added a message telling the user that there was no data at the moment, with an icon to reduce the blank space, and with this new change, the first time the view was used, it is seen with a user-friendly graphic design.

For these reasons it is vital to take into account the lack of data for the application, so we can avoid errors or that the view design looks awful due to the amount of white space. Also, the way to correct this problem is not so complicated, a simple if and a message solves this issue.