Good advice for many areas. I'm currently porting C++ code to C#. Because the C++ code is a work-in-progress, I tried to make the C# code mirror the original code as much as possible (varible names, methods, etc.). But there was one section of code I just didn't understand. Finally I went to the original author, got the info in English, and wrote it the "C# way" in short time. I'm currently testing it.
>When faced with this kind of code which probably "just grew" my first reaction is to ignore the code completely and start out by defining, in English, exactly what I am trying to accomplish. Once I can articulate just what I am trying to accomplish I can start looking at the procedure to see what parts can just be refactored into a user defined function. ( with the appropriate performance caveat - see the suggestions regarding CTE - a way to see it is only evaluated once )
>
>Then start building the select, testing performance as you go.
>
>And of course check the execution plan as you build each refinement into the select
>
>IOW, don't start with attempting to fix what you have - just use it for guidance as you think about the best way to do it right.
Craig Berntson
MCSD, Microsoft .Net MVP, Grape City Community Influencer