August Meme Monday – Crap Code

Categories: Meme Monday, News, Professional, SSC
Tags:
Comments: 3 Comments
Published on: August 1, 2011

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.

SELECT *
	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?

3 Comments - Leave a comment
  1. Mark says:

    It bothers me when people use column names (or other user named elements) that happen to be commands or functions. I’ll admit it – I’ve been guilty of it myself, but I avoid it. “SELECT AVG from Mytable.” Noooo! Crap code.

  2. jonmcrawford says:

    Lack of documentation/comments. What the code does != what the code is supposed to/assumed to do.

Leave a comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>






Calendar
August 2011
M T W T F S S
« Jul   Sep »
1234567
891011121314
15161718192021
22232425262728
293031  
Content
SQLHelp

SQLHelp


Welcome , today is Friday, April 18, 2014