It's best to initialize a variable just before you set it to something else



  • From a sproc in a system I'm working on; just one of several examples of this type of code

    <snip>

    SELECT @Id = 0   
    SELECT @Id = sId FROM tbl WHERE sId = @sId AND aId = @aId1
     <snip>

     



  • Not the worst WTF I've seen. In some languages, if you later insert some lines between the declaration and use that actually use the variable, and the declaration doesn't initialise the variable, you enter the muddy waters of the DS9000.



    Apart from the apparent redundancy, what irks you about this? (And what specifically happens in sproc's if you don't initialise but try to use a variable?)



  • If that second SELECT statement returns no rows because of the WHERE conditions, then @Id doesn't get set, and is potentially still NULL. Depending on what the procedure is doing later, that may or may not be an issue. Granted it's not the cleanest looking way I've seen for avoiding such a situation.



  • @PJH said:

    Not the worst WTF I've seen. In some languages, if you later insert some lines between the declaration and use that actually use the variable, and the declaration doesn't initialise the variable, you enter the muddy waters of the DS9000.



    Apart from the apparent redundancy, what irks you about this? (And what specifically happens in sproc's if you don't initialise but try to use a variable?)

    I'd say just replace the first SELECT with DECLARE, and you're golden.



  • @PJH said:

    Not the worst WTF I've seen. In some languages, if you later insert some lines between the declaration and use that actually use the variable, and the declaration doesn't initialise the variable, you enter the muddy waters of the DS9000.



    Apart from the apparent redundancy, what irks you about this? (And what specifically happens in sproc's if you don't initialise but try to use a variable?)
     

    OK, not the worst, I agree.  But I'd shoot whoever coded lines in a programming language (say, C#) like this

     

    int i = 0;
    i=3;



  • @dogbrags said:

    @PJH said:

    Not the worst WTF I've seen. In some languages, if you later insert some lines between the declaration and use that actually use the variable, and the declaration doesn't initialise the variable, you enter the muddy waters of the DS9000.

    Apart from the apparent redundancy, what irks you about this? (And what specifically happens in sproc's if you don't initialise but try to use a variable?)
     

    OK, not the worst, I agree.  But I'd shoot whoever coded lines in a programming language (say, C#) like this

     

    int i = 0;
    i=3;

    Like

    DIM I AS LONG = 0

    --CLIP DATABASE ACCESS CODE ---
    I = TBL!FIELD.VALUE



  • The problem here is that "I = TBL!FIELD.VALUE" is an oversimplification of "SELECT I = FIELD.VALUE FROM TBL WHERE <record matches some condition>".  What if no such record exists?  (Also "what if more than one such record exists?", but that should only arise if the query is written wrong.)

     



  • @dogbrags said:

    OK, not the worst, I agree.  But I'd shoot whoever coded lines in a programming language (say, C#) like this

     

    int i = 0;
    i=3;

    Welcome to my personal hell. :(



  • @Smitty said:

    Welcome to my personal hell. :(

    You have people shooting at you often?

     




  • #define malloc malloc
    #define malloc some_random_unrelated_functon
    #define some_unrelated_function malloc // Wait, this doesn't do anything! (Or does it?)

    Calling some_unrelated_function directly will now require a newline before the parentheses.

Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.