dragonwell
Programmer
I've been running into this design issue, and I'm sure it's a common one. Just wondering how you have handled it, or if there is a pattern that can be applied.
In my code I need to conditionally execute code based on a variable's value. Ideally, of course, we would test only boolean types. In the case where there are more than true/false values to test we sometimes use switches but even you need to have some idea of what the case values could be.
For example, my Employee class has a Status property. Status is an integer value, such as 1 = active, 2= on leave, 3=dismissed, etc. These values are stored in a Status table of the database, and the Employee table has a status column of type integer to identify exactly one status id per employee. So now when my Employee object is instantiated, it gets it's Status property from the database.
The program eventually needs to follow different execution paths depending on the Status of the employee.
ie
But what I don't want to do is hard code these values into the code. It always seems that at some point, the code needs to know what the values are.
Even worse, I have run into cases where primary keys are used to enumerate permanent values such as in the employee/status example. What if a row is accidentally deleted and a new one put back in? The "Active" could end up with status id of 6, and the code is broken.
I have thought about hard-coding an enumaration (based on int values) into the code, but ideally these values should be editable by the administrator.
I am now coding in .net and I am thinking the solution is somehow tied into the app.config system, but I have not really thought that through yet.
Please comment.
Best regards to all,
David
In my code I need to conditionally execute code based on a variable's value. Ideally, of course, we would test only boolean types. In the case where there are more than true/false values to test we sometimes use switches but even you need to have some idea of what the case values could be.
For example, my Employee class has a Status property. Status is an integer value, such as 1 = active, 2= on leave, 3=dismissed, etc. These values are stored in a Status table of the database, and the Employee table has a status column of type integer to identify exactly one status id per employee. So now when my Employee object is instantiated, it gets it's Status property from the database.
The program eventually needs to follow different execution paths depending on the Status of the employee.
ie
Code:
if(myEmployee.Status != 1)
{
//...
}
But what I don't want to do is hard code these values into the code. It always seems that at some point, the code needs to know what the values are.
Even worse, I have run into cases where primary keys are used to enumerate permanent values such as in the employee/status example. What if a row is accidentally deleted and a new one put back in? The "Active" could end up with status id of 6, and the code is broken.
I have thought about hard-coding an enumaration (based on int values) into the code, but ideally these values should be editable by the administrator.
I am now coding in .net and I am thinking the solution is somehow tied into the app.config system, but I have not really thought that through yet.
Please comment.
Best regards to all,
David