Write Cleaner T-SQL Using Version Control

  • Comments posted to this topic are about the item Write Cleaner T-SQL Using Version Control

  • There are other potential benefits. Here are some that spring to mind:

    1) Having a local copy of all database files means that you can search your database without hitting the server. Not only this, but the tools which are available to do this search have more power (eg, a Regex search) than those in SQL Server (even though SQL Search is great for this too).

    2) It's possible to do bulk database refactoring by building tools which start by modifying the files in your local repo. Then use your VCS to do a schema compare of what's in the repo vs what's in the DB and, if all looks well, apply the changes.

    If all does not look well, roll back the changes, modify the file-update tool and try again. You have not hit the database yet, so no one else has been impacted.

    I've used this method to replace dbname..object with dbname.dbo.object, for example (lots of procs affected).

    3) Running hand-in-hand with DB version control are VS database projects. If you ensure that your DB can be 'built' successfully, with no errors or warnings, you can be assured that your deployed database is in a consistent state.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • [font="Arial Black"]From the Article:[/font]


    So if we take out the code that has been commented out by not being reachable we end up with:

    create procedure usp_DoSomething (@PARAMETER int)

    as

    select top 1

    a, b, c, d, e

    from table

    where p = @PARAMETER;This is much more succinct and easier to read and understand.

    Version control is nice and a great idea but if you end up with comment-less code like the above, then you're using it for the wrong thing.

    Yes, I agree that you don't need to include the name of the store procedure in a comment header because it's already there. I also agree that you don't need to list the revision history in the code because it's in the version control system and I agree that you can and should remove commented out code provided that you list the actual reason why the code was removed in VCS. But the code gives no clue as to what it's for or how to use it. That means that you HAVE to go to another place to find out what this code is about.

    Then there's the joy that comes when someone decides that a different VCS is supposed to be used and the data in the old VCS either can't be migrated or someone simply forgets to do it and all that information is lost.

    Not having a properly designed and useful production header is the worst idea possible whether VCS is at play or not. Stop making excuses for not including proper documentation in code.

    As a bit of a sidebar, if all of your stored procedure names start with "USP_" and the whole stored procedure is written in a single case (lower case in the example above), then you have some other additional serious problems that VCS isn't going to help you with.

    --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)
    Intro to Tally Tables and Functions

  • Jeff Moden (10/5/2015)


    [font="Arial Black"]From the Article:[/font]


    So if we take out the code that has been commented out by not being reachable we end up with:

    create procedure usp_DoSomething (@PARAMETER int)

    as

    select top 1

    a, b, c, d, e

    from table

    where p = @PARAMETER;This is much more succinct and easier to read and understand.

    Version control is nice and a great idea but if you end up with comment-less code like the above, then you're using it for the wrong thing.

    Yes, I agree that you don't need to include the name of the store procedure in a comment header because it's already there. I also agree that you don't need to list the revision history in the code because it's in the version control system and I agree that you can and should remove commented out code provided that you list the actual reason why the code was removed. But the code gives no clue as to what it's for or how to use it. That means that you HAVE to go to another place to find out what this code is about.

    Then there's the joy that comes when someone decides that a different VCS is supposed to be used and the data in the old VCS either can't be migrated or someone simply forgets to do it and all that information is lost.

    Not having a properly designed and useful production header is the worst idea possible whether VCS is at play or not. Stop making excuses for not including proper documentation in code.

    Hi Jeff - no excuses here, I write code and document it where required - I actually spend more time documenting in a more useful way such as writing tests that actually show how the code should be used and prove that it actually works.

    I also ensure that code is clear and well named.

    If neither of these approaches are good enough then I document it.

    Code commenting should explain the why where it is not obvious and until someone can guarantee that people never make mistakes in their comment headers and so are always accurate I won't trust them - this is from experience in a number of different environments.

    ed

  • I'm with Jeff on the header saying what the SQL is supposed to do. When debugging if a header says what it's supposed to do and the code does something else I know the mistake is in the code. If the header says what it's supposed to do and it does that then I know we have a design error and I have to broaden my thinking. It's very helpful in cases where the code is complex. Having test procedures is all very helpful too but if I'm handed a project with SQL how do I even know those test cases exist and where they are, unless you know a way to attach them to the statement that I haven't found.

  • Taking out the name and parameters makes sense. Those are already in the code.

    I don't agree with taking out the descriptive parts because stored procedures can and will be pulled from the database. Without that information it's useless to have a quick look at a stored procedure to see what's been going on.

    Something we add to our template is a section that shows how the sproc is called including the EXEC. It's a handy way to select and run it as well as do a copy-paste as needed.

    Version control is great but think about how people dig into the database and work outside of version control even when they should know better.

  • JustMarie said:

    Something we add to our template is a section that shows how the sproc is called including the EXEC. It's a handy way to select and run it as well as do a copy-paste as needed.

    Great call, I use that all the time as well. Another one I use is to leave the interim select statements that I use for testing between CTE segments in the code but commented out. They make it really fast to find a problem with a very complex set of CTE's and don't, in my opinion, muck up the code at all.

  • JustMarie (10/5/2015)


    Taking out the name and parameters makes sense. Those are already in the code.

    I don't agree with taking out the descriptive parts because stored procedures can and will be pulled from the database. Without that information it's useless to have a quick look at a stored procedure to see what's been going on.

    Something we add to our template is a section that shows how the sproc is called including the EXEC. It's a handy way to select and run it as well as do a copy-paste as needed.

    Version control is great but think about how people dig into the database and work outside of version control even when they should know better.

    I think that people shouldn't be messing with the database, what we need is a proper development process that goes through testing and review rather than just changing stuff and seeing what happens - imagine if you deployed a cpp app and the ops team changed the assembly code to fix issues - that would be seen as mostly ridiculous :).

    Just because you can change the database code pretty easily really doesn't mean that you should 🙂

  • billp 37934 (10/5/2015)


    I'm with Jeff on the header saying what the SQL is supposed to do. When debugging if a header says what it's supposed to do and the code does something else I know the mistake is in the code.

    I'd disagree here. It could be the case, but it could also be the requirements evolved, the code gets changed, but the header comment doesn't.

    I think that this is where a VCS is handy. The habits of a developer evolve to enter the comment when checking in, since that's a prompted requirement. I see developers forgetting to update code comments, which is a problem in and of itself, but because this isn't required in a work flow, it's an issue.

  • I didn't mean to imply instead of version control, just in addition to version control.

  • I fully support VCS and in my development environment the db portion is the most lacking in this area. 🙂 I appreciate the different perspectives the author has provided in support of version control for the database code. A fully integrated environment that includes version control provides many many advantages and techniques that can be used on a day to day basis.

    So will the next article be a comparison of some VCS's and their benefits to include which development environments they can easily be incorporated into? I use SSMS and find it difficult to incorporate VCS (Subversion) and have it completely integrated. Plugins for Eclipse seem to be kludgy and don't allow all the benefits of working in SSMS. Would really appreciate more on this from those with more experience than I have.

    Thanks!

  • I agree. I like the comments in the stored procedure. I work in a company where each department and group within the department have their own source control - most of which I do not have access to. I also find that people who can explain in a simple sentence what they are trying to do write better code.

  • nfink (10/5/2015)


    I fully support VCS and in my development environment the db portion is the most lacking in this area. 🙂 I appreciate the different perspectives the author has provided in support of version control for the database code. A fully integrated environment that includes version control provides many many advantages and techniques that can be used on a day to day basis.

    So will the next article be a comparison of some VCS's and their benefits to include which development environments they can easily be incorporated into? I use SSMS and find it difficult to incorporate VCS (Subversion) and have it completely integrated. Plugins for Eclipse seem to be kludgy and don't allow all the benefits of working in SSMS. Would really appreciate more on this from those with more experience than I have.

    Thanks!

    Hi nflink,

    Have a look at:

    https://the.agilesql.club/Blog/Ed-Elliott/Getting-More-Agile/Choosing-a-Development-IDE-for-Sql-Development

    You can't go far wrong choosing ssdt, you get rename refactoring, compile time validation and automatic / compare & merge type deployments (all for free with visual studio community / express)

    Ed

  • Thanks for posting the article. It's important to look for ways to reduce comments down to only that content that is useful. I agree on removing the sproc name and parameters from the header - just one more thing to have to maintain and provides no real value. With respect to history documentation, we have gone through 2 source control migrations and have had limited success retaining history so I'd add my voice to the chorus about code history within the procedure being of value. I'd also argue that the context of looking at the comments in the object itself is better than browsing through source control comments which may have been generalized for a set of objects changed in the same check-in.

  • Oh PERFECT! Thank-you very much!

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

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