Naming Convention for UDF's, Views and SP's

  • I recently has to take a written SQL test for a client and one of the questions got me stumped (from memory was something like this):

    Why should User Defined Functions not be prefixed UDF or udf, views not Prefixed with VW or vw and Stored Procedures not USP or usp

    I couldn't come up with a reason for not using these naming conventions..:crazy:

    Can anyone enlighten me?

    Andy

    ==========================================================================================================================
    A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila. Mitch Ratcliffe

  • They all can be. Makes things sloppy in my opinion. However, if you are dealing very large numbers of objects, for example, I saw this with stored procedures when we had over 1000 of them, and they all have the same first several letters, it actually can slow down performance of the underlying system as it tries to find the proc name. Now, this was back on 2000 and some hardware that wasn't terribly beefy, but it is possible.

    Nah, the main reason I'd argue against that style notation is that it's VERY old school. Even most developers have tossed that object naming style for just using plain names for clarity.

    Also, it's extremely inconsistent unless you also name all your tables tblXXX (and DO NOT, EVER, do that, :w00t:).

    I just wouldn't because it reduces clarity and readability. Also, when I'm typing and anticipating my code completion to fill in the proc name or whatever, I know have to type about 6-8 characters instead of 3-5, slowing me down considerably.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • I do mostly agree with Grant and technically, all that data is available in the metadata and shouldn't need to be reflected in the object name.

    However, I am quite fond of having some sort of naming convention for views, as it's not visible at a glance that you're referencing a view rather than a table and I find there are less developer mistakes when they're obviously marked in the name (e.g. re-utilising views inappropriately rather than going back to the base tables).

  • Andy Hyslop (2/11/2013)


    I recently has to take a written SQL test for a client and one of the questions got me stumped (from memory was something like this):

    Why should User Defined Functions not be prefixed UDF or udf, views not Prefixed with VW or vw and Stored Procedures not USP or usp

    I couldn't come up with a reason for not using these naming conventions..:crazy:

    Can anyone enlighten me?

    Andy

    I agree with what has already been suggested but I have a cold, hard reason for not doing it for tables and views. There have been many times where I've needed to change the structue of a table but couldn't do so because of some bad coding practices on the front end. To trick the front end into not seeing the change, I changed the table name to something else and then made a view using the original table name. I don't know about you but it kind of defeats the whole purpose to have a view with a prefix of "tbl". ๐Ÿ˜‰

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Just to reinforce what Jeff has said. I have also found views with a prefix of "tbl", I think for the same reasons - something started off as a table and the structure evolved over the years until it was changed to a view - so it may sound daft but it does happen.

    So to me I would absolutely avoid:

    tables with the prefix tbl

    stored procs prefixed usp_ - it is just pointless letters

    data types included in column names

    Mike John

  • Jeff Moden (2/11/2013)


    Andy Hyslop (2/11/2013)


    I recently has to take a written SQL test for a client and one of the questions got me stumped (from memory was something like this):

    Why should User Defined Functions not be prefixed UDF or udf, views not Prefixed with VW or vw and Stored Procedures not USP or usp

    I couldn't come up with a reason for not using these naming conventions..:crazy:

    Can anyone enlighten me?

    Andy

    I agree with what has already been suggested but I have a cold, hard reason for not doing it for tables and views. There have been many times where I've needed to change the structue of a table but couldn't do so because of some bad coding practices on the front end. To trick the front end into not seeing the change, I changed the table name to something else and then made a view using the original table name. I don't know about you but it kind of defeats the whole purpose to have a view with a prefix of "tbl". ๐Ÿ˜‰

    Synonyms could probably get around this type of scenario to Jeff. Then you don't need a view either. ๐Ÿ˜› I do agree though that object prefixes are not a good idea.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 โ€“ Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Synonyms won't work there. Remember, the structure of the table has been modified but the GUIs have to think it hasn't. This is especially true when you add a column but the app has some poorly formed embedded code that does INSERT/SELECTs without and insert column-list.

    But, point taken. Why would anyone use Hungarian notation to name a synonym? ๐Ÿ˜›

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • I have never been a tbl-er but I have been a usp-er.

    Lately I put everything in a schema. Is this a reasonable statement as far as a best practice? Always put all objects in a schema besides dbo.

    Educational comments most welcome....

  • I put most everything in the DBO schema, Crissy. The only time I deviate from that is for utility and scratch objects. Of course, most of the work I do is at the batch level and what little I do for the front end can easily tolerate not using a bunch of schemas because I go for the next level of "security by separation" by keeping things in different databases.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • The databases I work on usually belong to clients and already have a schema. Often these instances are full of third-party objects with all kinds of naming conventions. So even though it does involve an extra bit of typing (isn't that what Red Gate SQL Prompt is for?), I use unique project-based prefixes so my objects will at least all be grouped together when I'm working in SSMS.

    Also, though I'm starting to use scalar functions less and less, I still like to prefix them with "svf" or "tvf" so I know what output to expect. I'm not adverse to long descriptive names though...I think when it's a year or two later and I have to come back and look at something I hardly remember I'm always glad I used intelligible names. That includes variable names within procedures as well.

    I waver back-and-forth inconsistently when naming columns base on datatype such as "intGender" vs "strGender" when it may not be obvious. Lately I tend to skip the camel casing and if it says EmployeeID then it's an integer and if EmployeeDesc then obviously it's a string. I have one current client who is bat-@#$% crazy about camel-casing EVERYTHING. So it's bitDog this, dtCat that, guidSession until it gets tiresome. He also thinks uniqueidentifiers are the best thing to use for primary keys on every table...oh, crap, now I'm ranting...don't get me started...:-P

    ย 

  • Chrissy321 (2/11/2013)


    I have never been a tbl-er but I have been a usp-er.

    Lately I put everything in a schema. Is this a reasonable statement as far as a best practice? Always put all objects in a schema besides dbo.

    Educational comments most welcome....

    It depends.

    If I have discrete sets of functionality, then I break things up into schemas. But it needs to be substantial. I've never created a schema for 2 or 3 tables alone. If there really isn't any way to differentiate the parts of an application or if the application is small, then I just leave it in dbo. Since schema ownership can make things easier to manage, I find it helpful for big databases.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Thanks all for the replies - most helpful ๐Ÿ™‚

    I have never prefixed tables with tbl but I have prefixed usp's and views

    It just seemed like an odd question the way it was phrased, I think the word never prefix was a little strong and that threw me a little.

    From what I can gather from the conversation, yes it makes sense not to do so (and I won't from now on) and there are good reasons not to however as I said never was a little strong, is there a huge impact (for example to coin Jeffs phrase writing RBAR when set based will do the job better) then no?

    ==========================================================================================================================
    A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila. Mitch Ratcliffe

  • Also, it's extremely inconsistent unless you also name all your tables tblXXX (and DO NOT, EVER, do that, ).

    Why would you never do this? I've seen dimension tables prefixed Dim and fact tables prefixed Fact. I use dtbl and ftbl. With my tables, I have tbl, tlkp, trel, xtbl and stbl. I find this very useful when going back to the design long after it's created to understand what I've done.

    As for the stored procedures, I'll use add_, tfm_, sel_ and others. Again, this helps me remember it's purpose when going back to it later.

    I don't have many views, but it helps to have at least vw_ if only to distinguish it from tables.

    I've been doing this a long time and seen the benefits. Without some real factual reason, I don't think it's a good idea to tell someone to never ever use the system.

  • Ever is probably an over-reaction, but in general these types of naming conventions just don't make sense. How is tblInvoices easier to understand than Invoices? How is it more clear? Further, when you get hit by a bus, what does it communicate to the next person who takes over your job. I just do not see it.

    I'll give you an exception I have used and that's for lookup data. You know, the little name/value pair tables that are just there to enforce select lists? On those I've used c_TableName. The 'c_' stood for Code tables. It served two purposes. It made them all group together within SSMS, and it differentiated these tables from real system tables.

    However, now, I wouldn't even do that. I'd put them into a separate schema but with standard names. It would serve the same purpose but improve the overall readability. Instead of c_ which means nothing unless I tell you what it means, I could use Code.TableName or Lookup.TableName which makes things more clear.

    Naming standards should improve clarity and communication. Specialized abbreviations just don't add to the clarity. In fact, they tend to reduce it. But, this is one of those things that comes down to preference, not a technical reason. So if it works for you, go for it. But it's not a standard I'd support given the choice.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • RonKyle (2/13/2013)


    Also, it's extremely inconsistent unless you also name all your tables tblXXX (and DO NOT, EVER, do that, ).

    Why would you never do this? I've seen dimension tables prefixed Dim and fact tables prefixed Fact. I use dtbl and ftbl. With my tables, I have tbl, tlkp, trel, xtbl and stbl. I find this very useful when going back to the design long after it's created to understand what I've done.

    As for the stored procedures, I'll use add_, tfm_, sel_ and others. Again, this helps me remember it's purpose when going back to it later.

    I don't have many views, but it helps to have at least vw_ if only to distinguish it from tables.

    I've been doing this a long time and seen the benefits. Without some real factual reason, I don't think it's a good idea to tell someone to never ever use the system.

    I know what you mean about wanting to know the purpose. This is actually quite useful. I prefer to use a suffix for this type of thing instead of a prefix. This serves two main advantages. First it will keep like items grouped in SSMS. And secondly I find it makes it far easier to code with. Say you have a Product and you need the basic CRUD sprocs. With your naming convention you would have add_Product, sel_Product, del_Product, upd_Product. Those are certainly clear what they do but they are hard to code with and in SSMS you will have stuff scattered all over. If instead you moved it to the end like this. Product_add, Product_sel, Product_del, Product_upd. Now when you are coding you think I need the Product sproc, intellisense will now show you only those that start with Product. For me I also think first of the object I am working with and then what I need to do to it. Just my 2ยข.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 โ€“ Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 15 posts - 1 through 15 (of 29 total)

You must be logged in to reply to this topic. Login to reply