Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Suppose I have a table "Progress" with two columns. One column is ID which is identity. Another column is Progress_Label

ID Progress_Label

1 Submitted

2 Approved by user

3 Rejected by leadership

4 Cancelled

5 Completed

What is the best programming practice, should I go by ID or by label? In my stored procedures, functions, or in programming code methods etc should I search records by the ID = 3 for example or should I type "Where progress_lable is Rejected by leadership" ?

If somebody would want to edit the labels, all the code would stop working if I go by the label?

At the same type if I type the label, code looks more understandable since it says right in the code what is it we are looking for?

Are there any articles regarding this?

share|improve this question

3 Answers 3

Use the ID.

The label does change, sometimes, and for human-readable code you can just use things like enums:

enum DocumentProgress
{
    Submitted = 1,
    Approved by user = 2,
    Rejected by leadership = 3,
    Cancelled = 4,
    Completed = 5
}

Which might be used like this:

Item.Progress = DocumentProgress.Submitted;
share|improve this answer

I think the right answer is that it depends upon the stability of your database -- if the IDs are auto-generated numbers for lookup values, then you should probably use the text value, at least until you are sure you won't be adding or removing them. If on the other hand the IDs have their own established value outside of the text in the database, then use that.

share|improve this answer

Create a method called GetProgressIdByLabel so you can do something like

int progressId = GetProgressIdByLabel("Submitted")

This reads nicely, your method should throw an exception if someone uses a label that doesn't exist

share|improve this answer
    
Using string literals in a code is not really a good practice. –  David Sergey Jun 21 '13 at 17:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.