A Good Programmer Always Thinks Like a User

A good programmer always thinks like a tester and a user

Alright, in this new series of Smart Coding, I am going to point out exact problems and how smart coders should code it.

So here is a perfect example of a good and bad coder.

In this live page below, we have Major Categories and Minor Categories drop down lists. When you select a Major Category, the minor categories belonging to that major category will be loaded.

SmartCoder1.jpg

If you can see from the right top corner, there is AJAX is being used and I see the Loading… message for a long time. That is because on DropDown item changed event, the code is probably trying to load matching minor categories.

So what happens when site is slow or database response is slow?

I am waiting and waiting and waiting. This is what happens. That is a perfect example of Bad Code and Bad Coder.

Being a smart code, what you could do is, load both Major Categories and Minor Categories at once and filter Minor Categories in-memory based on the selected Major Category. That will take no time to load the data.

So, major categories and minor categories usually do not chanage a lot. It makes perfect sense to cache them in memory. It might not be a bad idea to store them in Application State. You can even dump all data in a single DataSet with two DataTables. Based on the major category selected, you can just filter the data using a DataView or use Row Filter on DataTable.

A good programmer always thinks like a tester and a user. 

Cheers!

Next Recommended Reading Always leave office on time

Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.