People are good at learning by examples. It’s just like when one of my friend asked for my help. Her clients want to make an interface for data entry using Access (you can contact me if you need one ^^). They will use it to store survey data. They gave her an example from the last survey, hoping that she will make the new one based on that example. In fact, the new interface must exactly like the previous (or the example given).
So, we created the visual interface first. After it done, we must add some functions that prevent accidently alter to data entered. I must admit that this is my first experience writing code in VB. Have no choice, I dug deep into the example code. Not so long after I found this code:
Public Sub closeRecord(txt1 As Boolean, txt2 As Boolean, txt3 As Boolean, txt4 As Boolean, txt5 As Boolean, txt6 As Boolean, txt7 As Boolean, txt8 As Boolean, txt9 As Boolean)
idResponden.Locked = txt1
noPlot.Locked = txt2
Luas.Locked = txt3
JenisTanaman.Locked = txt4
Umur.Locked = txt5
Kecamatan.Locked = txt6
Desa.Locked = txt7
Dusun.Locked = txt8
Catatan.Locked = txt9
This code used to set the appropriate text field’s status so that the entered data can’t be altered. Not until the user click an edit button. If the user click the edit button, the locked status will be set to False and the data can be altered.
closeRecord function has nine parameters (really long for a function right?). Every single parameter set status for one text field. So if the interface has twelve text fields, the function will catch twelve parameters. See another function that called the function from before:
Public Sub RecordOff()
closeRecord False, True, True, True, True, True, True, True, True
True parameters are there? It’s not my place to judge this is a bad code or not. I was just thinking about another variations from the function.
The first variation that came to my head was setter-like function in OOP. Assuming that a text field is an object, we can split the function into nine function. Every function sets their own text field. It will looks like this:
Public Sub closeIdResponden(status as Boolean)
idResponden.Locked = status
And all the other eight function for all of the text fields. It offers more flexibility. Just in case if we want to set just one of the text fields and not all of them. But, it’ll be less effective if we don’t want to set just one text field in the whole program. For this case, the function above will be called like this:
Public Sub RecordOff()
'and all the other seven
Then, I came up with another idea. According to the function uses toward the whole program, all of the text field except the first was set to the same status. So, why we don’t just set the other eight text fields. Here is the code looks like:
Public Sub closeRecord(status As Boolean)
idResponden.Locked = False
noPlot.Locked = status
Luas.Locked = status
Umur.Locked = status
Kecamatan.Locked = status
Desa.Locked = status
Dusun.Locked = status
Catatan.Locked = status
So it will just set the other text field well. The flexibility of the code will decreased, but it is okay for the whole program.
The point here is if you could decreased the flexibility as long as it will make the code more effective and didn’t violate the whole code. Just like the old saying, the right man for the right job. It’s just another function to me, what about you?