Skip navigation
Currently Being Moderated

Proper application design - hardcoded vs dynamic

Oct 13, 2013 1:17 PM

We have a table, 2 columns.  ID and NAME.  When our application installs, the script writes 3 records to it in the following sequence: 'Admins', 'Users', 'Visitors'.  So one would think, the table data looks like:



1 - Admins

2 - Users

3 - Visitors


So, when it comes to coding the application, is it better practice to assume that "Admins will always have an ID of 1, so I can write a SQL statement that may reference:  WHERE ID = 1 -- These are admins


Or should I be dynamic, where I would build a function called: getIDByName() and you can pass in the name 'Admins' and it will perform a SQL run and return back the number 1.  This would mean that when I write the above SQL statement I would say:


.... WHERE ID = getIDByName( 'Admins' ) -- Admins


I have a feeling that dynamic is the better approach, even though it means I'll have to code in more functions to automate it.

  • Currently Being Moderated
    Oct 14, 2013 8:21 AM   in reply to Aegis Kleais



    Is the "ID" column an auto-incrementing identity column that is controlled by the underlying database?  If so, I would err on the side of caution and not assume that the records will be written in the precise order you want.  What you might do is query that table once and cache the results in an application scope variable, then query that as needed to get the IDs as needed (saving multiple round trips to the database to retrieve the IDs over and over again.



    -Carl V.

    Mark as:
  • Currently Being Moderated
    Oct 14, 2013 9:28 AM   in reply to Aegis Kleais

    Glad that worked out!

    -Carl V.

    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points