SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

August Meme Monday – Crap Code

Interesting topic this month posted by Thomas LaRock.  This month he has chosen “crap code” as the topic.  I think it is interesting because there really are so many different ways to take this.

Knowing that there are so many qualifications for crap code, I personally like that of readability.  I know everybody has their own interpretation on how code should be written and what is readable.  However, I have to say that soooo many styles out there are not very good.  The style is sooooooo hard to read and follow.  For me, it is a coding nightmare to see code that is poorly formatted or not formatted at all.

SELECT * FROM sometable LEFT OUTER JOIN table2 ON table2.something = sometable.something AND table2.somethingelse = sometable.somethingelse OR table2.somethingagain = sometable.somethingagain
RIGHT OUTER JOIN anothertable ON anothertable.something = table2.something OR anothertable.another = table2.another AND table2.somethingelse = anothertable.somethingelse

That code is just flat out difficult for me to read.  SQL Server handles white space just fine so a little formatting doesn’t hurt anything.  Better yet, it very well could help improve the readability of your code to more people than just you.  Something like the following helps.

	FROM sometable st
		LEFT OUTER JOIN table2 t2
			ON t2.something = st.something
			AND (t2.somethingelse = st.somethingelse
					OR t2.somethingagain = st.somethingagain
		RIGHT OUTER JOIN anothertable AT
			ON AT.something = t2.something
			OR (AT.another = t2.another
					AND t2.somethingelse = AT.somethingelse

Now, for simplicity sake, I also introduced something in both of these code samples that I don’t like too much either.  In both cases I use a “Select *”.  This is bad form!  One really should also denote each column that you want to be returned.  Not only is it good for readability, but it can be useful for the query optimizer and indexing purposes.


So what do you consider to be crap code?


No comments.

Leave a Comment

Please register or log in to leave a comment.