On the hardcoding of string constants
I tried to stress the importance of NOT doing comparisons on hardcoded string values (and also enums). In my eyes against SQL best practises, is "smelly" code and so on.
You know what is worse than hardcoding something? Making something "soft-coded" / "configurable" / "flexible" when it is already hardcoded somewhere else. When you do that, you have all the disadvantages of flexibility (flexibility = more opportunity for error) and none of the advantages (because you can't actually change it anyway). You end up with a value that appears to be something you can change when in fact it has to be set to one and only one value in order to work. It serves no purpose, and in fact it gets in the way.
So I'd ask you to think about two things:
(1) The software that inserts these records. Is it hardcoded? Then hardcode the software that reads it.
(2) The database. When you deploy your software, will the database already exist and have data in it? If yes, then any symbolic data (such as A/D/I) in those tables is as good as hardcoded-- unless you plan to run migration scripts with your deployments.
In my opinion, comparing to a hardcoded string constant is not only perfectly fine in this situation, it is the go-to method for handling it.
Getting rid of that case
statement
Now, maybe you have a problem with the case
statement. That's fine. You can get around it with some delegates, like this:
//Declare our hardcoded values
const string OrderAdd = "A";
const string OrderDelete = "D";
const string OrderIgnore = "I";
//Initialize. Add to static constructor somewhere.
var lookup = new Dictionary<string, Action<DataRow>);
lookup.Add(OrderAdd, ExecuteAddOrder);
lookup.Add(OrderDelete, ExecuteDeleteOrder);
lookup.Add(OrderIgnore, (DataRow) => {} );
//Handle a record
var code = dataRow["OrderAction"] as string;
if (!lookup.Exists(code)) throw new InvalidOperationException();
var action = lookup[code];
action(dataRow);
//Define handlers
void ExecuteAddOrder(DataRow sourceRecord)
{
//Add order here
}
void ExecuteDeleteOrder(DataRow sourceRecord)
{
//Delete order here
}
While the above seems cool it actually doesn't add much value. It adds a little. Depending on your team, the decrease in readability may not be worth it.
On procrastinating
Unless you are very lucky, you are not working on a piece of artwork. You are working a practical solution to a business problem. In many cases, timeliness has much more business value than elegance. Your success as an engineer will be highly dependent on your ability to solve technical problems quickly and practically. Coming up with the perfect solution a month too late will not get you any extra credit whatsoever.
Remember this, and remember that a year from now, no matter how lovely your code, you will probably look back at it and laugh.
else
for ignored without any sanity checks is bad. I'd add anotherelse if(ignored)
and throw an exception is theelse
to indicate that it should be unreachable. And why are you using anif/else if
chain instead of aswitch
?