Negative Count



  • From our code base... x is a List<T>...

    [code]
    if (x == null || x.Count <= 0)
    [/code]

    What the hell was going through the developer's mind when they wrote that? *head-desk*



  • Defensive programming. There could be a bug either in the list code or the use of it where the count could go negative, for example when something is deleted too many times. I've been bitten by bugs like that, you never forget.

    Personally, I would have used "x.Count<1"



  • @DumbByAssociation said:

    From our code base... x is a List...

    <font face="Lucida Console" size="2"> if (x == null || x.Count <= 0) </font>

    What the hell was going through the developer's mind when they wrote that? *head-desk*

    @GoatRider said:

    Defensive programming.

    This is not the WTF you are looking for.


  • Winner of the 2016 Presidential Election

    It would be nice if unsigned numeric types were CLR compliant so we could use those for things like this.



  • @GoatRider said:

    Personally, I would have used "x.Count<1"
     

    That could cause issues if the count is a float. What if your thing has to happen when count is 0.73?



  • @GoatRider said:

    Defensive programming. There could be a bug either in the list code or the use of it where the count could go negative, for example when something is deleted too many times. I've been bitten by bugs like that, you never forget.

    Personally, I would have used "x.Count<1"

    Whilst I appreciate where you're coming from, the List I speak of is a .Net List<T>... If that goes negative, I'll worry...



  • Probably habit and muscle memory. Move along.



  • @GoatRider said:

    Defensive programming.

    Ok, I'm fine with defensive programming. But seriously? You think the .net CLR's List<> implementation could have a negative count? Believe me, if that happens, the computer's going down in flames anyway from the 40,000 RAM errors so you might as well not bother.



  • @DumbByAssociation said:

    From our code base... x is a List...

    <font face="Lucida Console" size="2"> if (x == null || x.Count <= 0) </font>

    What the hell was going through the developer's mind when they wrote that? *head-desk*

    Am I missing something here? Here's how I'm reading that:

    If x is null OR x is empty (count less than or equal to 0). Yes it's a stupid way of writing it but I've seen much bigger WTFs.




  • public class EvilList<T> : List<T> {
    public new int Count { get { return -1; } }
    }

    I this I'm misunderstanding shadowing but still...



  • @mikeTheLiar said:

    @DumbByAssociation said:

    From our code base... x is a List...

    <font face="Lucida Console" size="2"> if (x == null || x.Count <= 0) </font>

    What the hell was going through the developer's mind when they wrote that? *head-desk*

    Am I missing something here? Here's how I'm reading that:

    If x is null OR x is empty (count less than or equal to 0). Yes it's a stupid way of writing it but I've seen much bigger WTFs.

     

    If x is null, it will throw an exception on 'x.Count'. So, you check if it's null first, and if it is not null, you check for x.Count.

    ~Sticky

     



  • @StickyWidget said:

    If x is null, it will throw an exception on 'x.Count'. So, you check if it's null first, and if it is not null, you check for x.Count.

    ~Sticky

     

    Won't short-circuiting prevent that? If x is null, x.Count won't even evaluate.


  • Discourse touched me in a no-no place

    @mikeTheLiar said:

    Won't short-circuiting prevent that?
    Shh! You'll short-circuit his brain if you tell him about the tricks that the competent programmers use…



  • if (!(x != null && x.Count > 0))

    Now use your favourite refactoring tool (Eclipse, ReSharper, whatever) and "push negation down". (Alternative, have the same if without the negation, focus on the else branch and Refactor->swap branches).

    TRWTF (in C#) is List.Count not being declared as an uint (in Java it is not possible and I'd really like some annotations that assert a value to be non-negative (or positive in other cases) to make refactoring expressions not that stupid).


  • Winner of the 2016 Presidential Election

    @mihi said:

    TRWTF (in C#) is List.Count not being declared as an uint (in Java it is not possible and I'd really like some annotations that assert a value to be non-negative (or positive in other cases) to make refactoring expressions not that stupid).

    List is part of the .NET Framework, not part of C#; and unsigned types are not considered "CLR Compliant", that is, not every language that compiles down to CLR bytecode has a concept of them. You'll almost never see an unsigned type in the .NET Framework code for that reason.



  • @GoatRider said:

    Defensive programming. There could be a bug either in the list code or the use of it where the count could go negative, for example when something is deleted too many times. I've been bitten by bugs like that, you never forget.

    Personally, I would have used "x.Count<1"

    x.Any()



  • @joe.edwards said:

    It would be nice if unsigned numeric types were CLR compliant so we could use those for things like this.

    This is, by far, my biggest gripe with .NET. I personally always try to use the unsigned types when appropriate to make my code safer and more readable. (As opposed to Linux graphics driver programmers, who have unsigned types available but don't use them.)


  • Winner of the 2016 Presidential Election

    I'd personally like it if there were ways to contractually/declaratively enforce the domain and range of value types (and maybe some kind of interface you could implement for reference types). Something like an int<[1,5)> which could only hold values above 1 inclusive and below 5 exclusive.



  • @joe.edwards said:

    I'd personally like it if there were ways to contractually/declaratively enforce the domain and range of value types (and maybe some kind of interface you could implement for reference types). Something like an int<[1,5)> which could only hold values above 1 inclusive and below 5 exclusive.

    There is sort of a way to do that now, but it s really mettadata (attributes).  You either have to write the handling code or use objects/toolkits that respect the metadata.  The implementation is not really for low level code, either, but more designed for validation purposes when passing between layers and presentinf feedback to the user.



  • @The_Assimilator said:

    @joe.edwards said:

    It would be nice if unsigned numeric types were CLR compliant so we could use those for things like this.

    This is, by far, my biggest gripe with .NET. I personally always try to use the unsigned types when appropriate to make my code safer and more readable.

    I am not sure it makes it any safer, as you have to be more careful when converting from an usigned variable to a signed variable, probably why the creaters of the CLR took the safer/more generic route in addition to being able to handle a broader range of languages.

    Under the scenes, the compilers (JIT, etc) use unsigned integer instructions for speed, such as array bounds checking that be handled with a single unsigned arithmatic instruction instead of multiple to check for negative and over the max limit.



  • @joe.edwards said:

    I'd personally like it if there were ways to contractually/declaratively enforce the domain and range of value types (and maybe some kind of interface you could implement for reference types). Something like an int<[1,5)> which could only hold values above 1 inclusive and below 5 exclusive.

    Seconded. Or maybe even a @Bounded(lower=1,upper=5) int (although that syntax, which will be possible in Java 8 at any place where a type is used - even in typecasts, and not only on method parameters - is currently not used at all by the compiler or JVM, only by additional validation tools).

    The only (or at least worst) shortcoming of this is that I'd often like to bound variables by other variables or by the length of lists/arrays; and declaring that kind of relationships is not really possible by Java's annotation system (not sure about C#'s attributes) - and you still need to check them of course, not only declare them.



  •  If the contents of the if statement is a throw exception statement

     it makes perfect sense

     else someone is trying to make voodoo code.



  • @doomsought said:

     If the contents of the if statement is a throw exception statement

     it makes perfect sense

     else someone is trying to make voodoo code.



  • @Ben L. said:

    @doomsought said:

     If the contents of the if statement is a throw exception statement

     it makes perfect sense

     else someone is trying to make voodoo code.

    Now I want to make a bot that trawls TDWTF threads for keywords and posts a semi-relevant XKCD...


  • Discourse touched me in a no-no place

    @joe.edwards said:

    I'd personally like it if there were ways to contractually/declaratively enforce the domain and range of value types (and maybe some kind of interface you could implement for reference types). Something like an int<[1,5)> which could only hold values above 1 inclusive and below 5 exclusive.
    IIRC, Ada had a numeric type system that was very much like that. They also had tagged numeric types. The resulting effect was to make integers so complicated that you wanted to stick to plain int for the sake of sanity.

    This was all a good while ago for me, and comes of working for a theoretical CS professor at the time.



  • @DumbByAssociation said:

    From our code base... x is a List...

    <font face="Lucida Console" size="2"> if (x == null || x.Count <= 0) </font>

    What the hell was going through the developer's mind when they wrote that? *head-desk*

    Looking whether the list is null or empty, and making dounleplus sure that there isn't a bug in the VM, or something like that.

     



  • Simple: if a list has 2 elements, and we delete 5, we have to add 3 to make the list empty. Everyone knows that.


Log in to reply
 

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