July 26, 2011 at 1:18 pm
I want to make sure I am writing clean code that is easily readable and easy to understand.
What are good practices to keep my really long stored procedures from being a mess?
Is there any documentation on the subject?
July 26, 2011 at 1:30 pm
It is much like any other code, the readability often comes down to user preference. In general code tends to be easier to read if you use white space liberally. Also indentation makes code sections easier to identify. Above all else BE CONSISTENT!!! If you have a style for a particular section of code make other sections of code that are similar look similar.
_______________________________________________________________
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/
July 26, 2011 at 1:30 pm
I personally use this :
http://www.red-gate.com/products/sql-development/sql-prompt/sql-refactor
It's now only available with SQL prompt which is a very nice intellisence application for SSMS.
It can literally refactor 1000 lines of code in 1-2 seconds. Well worth the price and there's a 14 days free trial to check it out.
On top of that I try to always use the same aliases for the same objects. That way you can easily understand what's going on rather than guessing or re-re-rereading the joins over and over again.
July 27, 2011 at 7:25 am
CELKO (7/27/2011)
Get a copy of SQL PROGRAMMING STYLE. I did research on this topic for two years at AIRMICS and used to write a column of software engineering in the trade press. I also get into ISO and other Standards.
Not needed. Just do what feels right and be consistant. If you work in a team, decide on your own standards and follow them.
July 27, 2011 at 7:35 am
Ninja's_RGR'us (7/27/2011)
CELKO (7/27/2011)
Get a copy of SQL PROGRAMMING STYLE. I did research on this topic for two years at AIRMICS and used to write a column of software engineering in the trade press. I also get into ISO and other Standards.Not needed. Just do what feels right and be consistant. If you work in a team, decide on your own standards and follow them.
ISO and Standards my favourite books! I read them as a bible. Try to read them to your children (if you have some), they will fail asleep twice faster than anything else printed for kids. 😀
I'm 100% with Ninja's_RGR'us on this topic. The golden rules are: Be reasonable and consistant!
That will do just as fine as following any book.
The only cons: You will not be able to pass CELKO review... But I'm sure that your company can not afford to pay his rates to review your writings :hehe:
July 27, 2011 at 7:37 am
Thanks for all of the feedback! It was helpful.
July 27, 2011 at 2:30 pm
btull89 (7/27/2011)
Thanks for all of the feedback! It was helpful.
I'll add to all of this. Use thoughtful comments and section headers. IT makes life a whole lot easier for troubleshooting and modifications. Try to avoid horizontal length more than 120 characters.
--Jeff Moden
Change is inevitable... Change for the better is not.
July 27, 2011 at 2:34 pm
Jeff Moden (7/27/2011)
btull89 (7/27/2011)
Thanks for all of the feedback! It was helpful.I'll add to all of this. Use thoughtful comments and section headers. IT makes life a whole lot easier for troubleshooting and modifications. Try to avoid horizontal length more than 120 characters.
Time for demo code & or template!
July 27, 2011 at 8:23 pm
Some of my thoughts on this topic here: http://www.sqlservercentral.com/blogs/don_halloran/archive/2010/12/12/sql-standards.aspx
July 27, 2011 at 8:45 pm
Ninja's_RGR'us (7/27/2011)
Jeff Moden (7/27/2011)
btull89 (7/27/2011)
Thanks for all of the feedback! It was helpful.I'll add to all of this. Use thoughtful comments and section headers. IT makes life a whole lot easier for troubleshooting and modifications. Try to avoid horizontal length more than 120 characters.
Time for demo code & or template!
Nah... I appreciate the thought but too controversial a subject. Telling people how to format their code is "fighting words".
--Jeff Moden
Change is inevitable... Change for the better is not.
July 27, 2011 at 9:39 pm
I have nothing to add except my compliments on a really good question.
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
July 27, 2011 at 9:42 pm
The Dixie Flatline (7/27/2011)
I have nothing to add except my compliments on a really good question.
Thank you sir! I'm just a SQL newbie looking to write easily ready T-SQL code. All of these posts have helped.
July 29, 2011 at 5:25 am
If your sprocs really are huge, consider trying to break them down into sub-sprocs or functions (performance allowing, of course).
Analyse your code and see if there are any areas where you can make something reusable (i.e. could a particular section of code be called by multiple sprocs?). If you can do this you reduce the amount of code you write, improve its reliability (you only need to fix it in one place), and save yourself time writing new code.
Mike.
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply