Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Weird line of code I see a lot of.

Options
  • 04-11-2011 1:29pm
    #1
    Registered Users Posts: 4,844 ✭✭✭


    Hi Guys
    I'm seeing this line of vb.net code a lot lately:
    System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)
    

    This peaked my interest, the System.Threading.Interlocked.Increment(i) to me is pretty straight forward, just incrementing a loop variable atomically.

    But the System.Math.Min method is what I'm stuck on. According to the description all this does is return the min value without altering the original values.

    The thing is the returned variable is not used.
    So I just put the line into an example:
    string s1 = "";
    
                int i = 0;
                while(i < 10)
                {
    System.Math.Min(System.Threading.Interlocked.Increment(ref i), i - 1);
                    s1 += i;
                }
                label1.Text = "s1: " + s1 + ",    s2: " + s2;
    

    result:
    s1: 12345678910,


    As expected s1 which is what I consider is always going to be i's value is incremented, so why do people bother with the system.math.min???

    Is there something I'm missing?


Comments

  • Registered Users Posts: 3,945 ✭✭✭Anima


    Can be useful for setting a limit or a range

    [PHP]
    max( min( i, 10), 0 )
    ...
    i = 11
    max( min( 11, 10), 0 ) = 10

    i = -1
    max( min( -1, 10), 0 ) = 0
    [/PHP]

    Something like that anyway.


  • Registered Users Posts: 4,844 ✭✭✭shootermacg


    Nah, it's not what min or max does is the issue it's that min or max takes the values, changes nothing and returns the lessor value,
    In the cases I've seen nothing is done and the min value is not used rendering it useless unless I'm missing something.


  • Registered Users Posts: 3,287 ✭✭✭padraig_f


    I didn't get it either, I went to google, and there's an explanation here:

    http://stackoverflow.com/questions/1828728/why-use-system-threading-interlocked-decrement-instead-of-minus

    Looks like it's C# code that's been run through a VB.Net converter, so
    System.Math.Min(System.Threading.Interlocked.Increment(ref i), i - 1);
    

    is the VB.Net equivalent of
    i++;
    


  • Registered Users Posts: 4,844 ✭✭✭shootermacg


    padraig_f wrote: »
    I didn't get it either, I went to google, and there's an explanation here:

    http://stackoverflow.com/questions/1828728/why-use-system-threading-interlocked-decrement-instead-of-minus

    Looks like it's C# code that's been run through a VB.Net converter, so
    System.Math.Min(System.Threading.Interlocked.Increment(ref i), i - 1);
    

    is the VB.Net equivalent of
    i++;
    

    not really I'm afraid although I see what they are trying to say.
    I read that before I posted, it's actually just a bunch forum crap.
    System.Threading.Interlocked.Increment(ref i)
    
    is the equivalent of
    i++;
    

    wrapping it in a min/max function is essentially doing nothing IMO.
    You are not returning the min/max, which would make sense because min/max returns an int.


    Think I found the reason: http://converter.telerik.com/ is a code conversion site looks like it likes that


  • Registered Users Posts: 3,287 ✭✭✭padraig_f


    System.Threading.Interlocked.Increment(ref i)
    
    is the equivalent of
    i++;
    

    Not quite. In that example they are, but technically they're not because 'i++' returns the pre-incremented value, whereas System.Threading.Interlocked.Increment returns the post-incremented value. That's the purpose of the Min() function in the VB.Net code (even though the value isn't saved).
    Think I found the reason: http://converter.telerik.com/ is a code conversion site looks like it likes that

    I think that's the key, that it's automatically converted code. If a person was porting them, they wouldn't have included the Min() when the return value isn't assigned to anything, but looks like the automatic converter doesn't consider that.


  • Advertisement
  • Registered Users Posts: 4,844 ✭✭✭shootermacg


    padraig_f wrote: »
    Not quite. In that example they are, but technically they're not because 'i++' returns the pre-incremented value, whereas System.Threading.Interlocked.Increment returns the post-incremented value. That's the purpose of the Min() function in the VB.Net code (even though the value isn't saved).



    I think that's the key, that it's automatically converted code. If a person was porting them, they wouldn't have included the Min() when the return value isn't assigned to anything, but looks like the automatic converter doesn't consider that.

    So we can safely say case closed, min is handy if you were actually returning the value, which you aren't so it's fluff then ^ ^.


  • Registered Users Posts: 11,979 ✭✭✭✭Giblet


    Yeah looks like someone who didn't know what they were doing.


  • Registered Users Posts: 7,157 ✭✭✭srsly78


    Am guessing that it's supposed to cover recursive mutexing, which shouldn't happen anyway if written properly. It's not that the writer didn't know what they were doing, it's to cover against other developers applying too many thread locks within thread locks.

    Not sure about vb.net, maybe it does nothing in this case :) But I think that's what it's supposed to do.


  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    Much easier to just have one conversion that handles both
    i++;
    
    and
    int j = i++;
    
    than to try and detect if the pre-increment value is used or not. Of course it's much easier to just leave the code in c# in the first place, but I guess you can't have everything.


  • Registered Users Posts: 11,979 ✭✭✭✭Giblet


    srsly78 wrote: »
    Am guessing that it's supposed to cover recursive mutexing, which shouldn't happen anyway if written properly. It's not that the writer didn't know what they were doing, it's to cover against other developers applying too many thread locks within thread locks.

    Not sure about vb.net, maybe it does nothing in this case :) But I think that's what it's supposed to do.

    It's the Math.min which is the issue. The other line is safe increments of an iterator in a multi threaded environment. The Math.min did nothing at all.


  • Advertisement
  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    It's used so that incrementing the iterator, and returning the pre-increment value can both happen in one "atomic" statement.


  • Registered Users Posts: 4,844 ✭✭✭shootermacg


    stevenmu wrote: »
    It's used so that incrementing the iterator, and returning the pre-increment value can both happen in one "atomic" statement.

    I think we all understand this fact. The condition that prompted the question is that this is widely spread as an acceptable way of incrementing loop variables, when in fact you aren't returning or using the min value or using the min value in the loop.
    It pops up so much as just a loop increment that it made me question why the use math.min/max.

    We already decided that this is a throwback to an over zealous code converter. It decides you are going to be assigning the value of i++, which isn't happening in the numerous cases online and in the 3rd party code I was looking at at the time.


Advertisement