• c0d3rg1rl (unregistered) in reply to pbean
    c0d3rg1rl:
    pbean:
    public bool IsListconntSmalleThaOne(IList<IContact> listOfResults)
    {
    try
      {
        listOfResults[1];
        return false;
      }
      catch(IndexOutOfRangeException e)
      {
        return true;
      }
    }
    
    BRILLANT!
    (apparently, my use of the quote function, not to mention preview, are not so brillant...)
  • Anonymous (unregistered) in reply to Maxx Delusional
    Maxx Delusional:
    I have never had a problem with C# not allowing this, I do however, wish that C# would equate null to false.

    Customer cust = GetCustomerById(235); if (cust) { //yay! I saved time! }

    Come on now, that's stupid. Null and false are two completely different things and they have completely different meanings. It makes no sense for the language to implictly equate them just so you can save yourself the 6 keystrokes it takes to write "!=null". Not only is it bad for the compiler to be making non-obvious assertions such as equating null to false but it is generally a better practice to be as explicit as possible in code, to prevent confusion and improve readability. The 6 extra keystokes it takes to write "!=null" greatly improves readability and doesn't cost you anything beyond 1-2 seconds of your life. You've got 1-2 seconds to spend on writing better code, haven't you?
  • (cs) in reply to Sean Connery
    Sean Connery:
    frits:
    Did anybody notice that (myList.Count < 1) is significantly shorter than IsListconntSmalleThaOne(myList) ?
    It appears to be C#, So the correct way is
    if( !myList.Any() )

    Noting that it's even more readable, and shorter yet.

    Hmmm... I've never heard of IList<T>.Any() before.

  • Jimmy Jones (unregistered) in reply to anonymous coward
    anonymous coward:
    frits:
    Did anybody notice that (myList.Count < 1) is significantly shorter than IsListconntSmalleThaOne(myList) ?

    (!myList.Count) should do the job, assuming a sane language.

    I think we just saw tomorrow's WTF.

    (Implicit conversion of ints to bools - shudder)

  • Named Brave Guy (unregistered) in reply to anonymous coward
    anonymous coward:
    (!myList.Count) should do the job, assuming a sane language.

    If by "sane", you mean "confuses boolean and numerical values", then I agree.

  • Luis Espinal (unregistered) in reply to toth
    toth:
    myList.Count == 0

    OTFY

    Unless, of course, you're dealing with weird, unnatural, abomination-before-God data structures that allow lists with negative or non-integral numbers of elements.

    I want to see a list whose count is 0.5.

    Good lord man, quiet! You start giving ideas, and soon we'll find a tard actually finding a way to code that kind of stuff :/

  • (cs) in reply to pbean

    Win.

  • Bob (unregistered) in reply to anonymous coward

    If you have to assume anything to know whether it's going to work, it's generally a worse idea than using something guaranteed, or at least more likely, to be unambiguous.

    You are not more clever or more efficient using !var over a comparison operator that explicitly indicates what you are trying to do.

  • (cs) in reply to Anonymous
    Anonymous:
    Maxx Delusional:
    I have never had a problem with C# not allowing this, I do however, wish that C# would equate null to false.

    Customer cust = GetCustomerById(235); if (cust) { //yay! I saved time! }

    Come on now, that's stupid. Null and false are two completely different things and they have completely different meanings. It makes no sense for the language to implictly equate them just so you can save yourself the 6 keystrokes it takes to write "!=null". Not only is it bad for the compiler to be making non-obvious assertions such as equating null to false but it is generally a better practice to be as explicit as possible in code, to prevent confusion and improve readability. The 6 extra keystokes it takes to write "!=null" greatly improves readability and doesn't cost you anything beyond 1-2 seconds of your life. You've got 1-2 seconds to spend on writing better code, haven't you?

    Just use the null coalesce operator "??". ^^ (Although you can't use that in every situation, I admit).

  • Halo (unregistered) in reply to dignissim
    dignissim:
    What sane language could allow mixing of boolean and integral values?

    As others have mentioned C and C++ permit this as it treats any non-zero value as true. The designers of C# explicitly don't permit this because a simple typo can result in a successful compilation that could be troublesome to debug:

    int x = 0;
    if(x = 5)
    {
        /* this is true and will always be true */
    }
    

    This is also why C# doesn't permit fall through between cases in a switch block unless that case is empty, unless you goto that case label, which is kinda wonky.

  • (cs) in reply to wtf
    wtf:
    0.9 for that chick you ALMOST got digits from.

    No way. She was a 0.7, tops. Maybe 0.8 if she got rid of those bangs.

  • (cs) in reply to frits
    frits:
    Sean Connery:
    frits:
    Did anybody notice that (myList.Count < 1) is significantly shorter than IsListconntSmalleThaOne(myList) ?
    It appears to be C#, So the correct way is
    if( !myList.Any() )

    Noting that it's even more readable, and shorter yet.

    Hmmm... I've never heard of IList<T>.Any() before.

    It's a LINQ extension method on IEnumerable.

  • Sean Connery (unregistered) in reply to frits
    frits:
    Sean Connery:
    frits:
    Did anybody notice that (myList.Count < 1) is significantly shorter than IsListconntSmalleThaOne(myList) ?
    It appears to be C#, So the correct way is
    if( !myList.Any() )

    Noting that it's even more readable, and shorter yet.

    Hmmm... I've never heard of IList<T>.Any() before.

    Defined in System.Linq.Enumerable.

    //
    // Summary:
    //     Determines whether a sequence contains any elements.
    //
    // Parameters:
    //   source:
    //     The System.Collections.Generic.IEnumerable<T> to check for emptiness.
    //
    // Type parameters:
    //   TSource:
    //     The type of the elements of source.
    //
    // Returns:
    //     true if the source sequence contains any elements; otherwise, false.
    //
    // Exceptions:
    //   System.ArgumentNullException:
    //     source is null.
    public static bool Any<TSource>(this IEnumerable<TSource> source);

    There's also an override taking a Func<TSource,bool> predicate.

  • (cs)

    The reason this happens is Intellisense auto-correction of mistyped variable names. If you misspell a symbol when defining it, then attempt to spell it correctly elsewhere, the IDE "helpfully" changes it to the incorrect spelling. If you're coding fast, you might not notice it happening because the editor keeps "correcting" you.

    We have a function in a product here called UpdataGrid(). It's supposed to be UpdateGrid(). It's impossible to believe that somebody would deliberately type "Updata" zillions of times without freaking out and fixing it.

    I love Intellisense in other ways, but this particular thing pisses me off.

  • (cs) in reply to Sean Connery
    Sean Connery:
    frits:
    Sean Connery:
    frits:
    Did anybody notice that (myList.Count < 1) is significantly shorter than IsListconntSmalleThaOne(myList) ?
    It appears to be C#, So the correct way is
    if( !myList.Any() )

    Noting that it's even more readable, and shorter yet.

    Hmmm... I've never heard of IList<T>.Any() before.

    Defined in System.Linq.Enumerable.

    //
    // Summary:
    //     Determines whether a sequence contains any elements.
    //
    // Parameters:
    //   source:
    //     The System.Collections.Generic.IEnumerable<T> to check for emptiness.
    //
    // Type parameters:
    //   TSource:
    //     The type of the elements of source.
    //
    // Returns:
    //     true if the source sequence contains any elements; otherwise, false.
    //
    // Exceptions:
    //   System.ArgumentNullException:
    //     source is null.
    public static bool Any<TSource>(this IEnumerable<TSource> source);

    There's also an override taking a Func<TSource,bool> predicate.

    Is that an extension method for IEnumerable? I don't usually write code against anything after framework 2.0 because we always end up having to be backward compatible to Win2k. Thus, no linq.

    Sorry, my bad.

  • pflock (unregistered) in reply to Halo
    Halo:
    dignissim:
    What sane language could allow mixing of boolean and integral values?

    As others have mentioned C and C++ permit this as it treats any non-zero value as true. The designers of C# explicitly don't permit this because a simple typo can result in a successful compilation that could be troublesome to debug:

    int x = 0;
    if(x = 5)
    {
        /* this is true and will always be true */
    }
    

    This is also why C# doesn't permit fall through between cases in a switch block unless that case is empty, unless you goto that case label, which is kinda wonky.

    I think they really explicitly permit implicit conversion, not really bool/int.

    Btw, a point for not doing those insane bool-value-to-literal-comparisons:

    c# ....
    bool x = ...;
    if (x = true) { // yay, valid c#
    }

    or similarly

    c# ....
    bool foo = ...;
    bool bar = ...;
    if (foo = bar) { // yay, valid c#
    }
  • Silvermink (unregistered) in reply to ARMed but harmless
    ARMed but harmless:
    Useless stone age computer fact: The Camputer Lynx BASIC interpreter accepted floating point line numbers:

    10 FOR i=1 TO 10 10.5 PRINT i 10.1237843 NEXT i

    Wait, shouldn't 10.1237843 come before 10.5? Uh oh.

    I think the original function name works better if read with a Scottish accent: "Och, is list connt smalle' tha' one, lad? If no', yer doolally!"

  • Silvermink (unregistered) in reply to Joe
    Joe:
    Oh, so now we're talking about nondeterministic computing?

    Sometimes a[0.5] returns a[0], sometimes a[1].

    Schrodinger's Index.

    CAPTCHA: secundum ("The secun time I read this, it was still dum.")

  • Xeromal (unregistered)

    Gives me hope that I can get a job one day. I used to think all developers were geniuses.

  • swedish tard (unregistered) in reply to Xeromal
    Xeromal:
    Gives me hope that I can get a job one day. I used to think all developers were geniuses.

    And if you ever land a job as a programmer you are gonna look back on that time and long that it really was like that. The worst part about this line of work is the beancounters tied with the fucktarded twats that couldnt code their way out of paper bag. They seem to not like me since I started reading code all the time and pointing out their mistakes and stupidities. Wish someone did it with my code too. >.< We all produce some wierd code at times.

  • Halo (unregistered) in reply to pflock
    pflock:
    Halo:
    dignissim:
    What sane language could allow mixing of boolean and integral values?

    As others have mentioned C and C++ permit this as it treats any non-zero value as true. The designers of C# explicitly don't permit this because a simple typo can result in a successful compilation that could be troublesome to debug:

    int x = 0;
    if(x = 5)
    {
        /* this is true and will always be true */
    }
    

    This is also why C# doesn't permit fall through between cases in a switch block unless that case is empty, unless you goto that case label, which is kinda wonky.

    I think they really explicitly permit implicit conversion, not really bool/int.

    Btw, a point for not doing those insane bool-value-to-literal-comparisons:

    c# ....
    bool x = ...;
    if (x = true) { // yay, valid c#
    }

    or similarly

    c# ....
    bool foo = ...;
    bool bar = ...;
    if (foo = bar) { // yay, valid c#
    }

    Sure, as C# still borrows from C and C++ that = is always the assignment operator, the result of an assignment is the value of the assignment, assignments can be compounded and comparisons can work against any expression. The only thing that C# does differently is not allow those comparisons to work against anything that isn't boolean, can't be implicitly cast to boolean or doesn't overload true/false operators.

    So sure, it won't stop 100% of the bugs that might result in unintentionally comparing against an assignment, but it does severely limit the circumstances under which it is permitted. Such assignment/comparisons are completely legal and might be intentional, although such a practice is probably not a good idea and maybe worthy of a compiler warning.

    using System;
    
    public class Foo {
        private readonly int value;
    
        private Foo(int value) {
            this.value = value;
        }
    
        public static Foo MakeFoo(int value) {
            return new Foo(value);
        }
    
        public static bool operator true(Foo x) {
            return x.value != 0;
        }
    
        public static bool operator false(Foo x) {
            return x.value == 0;
        }
    }
    
    static class Program {
        static void Main() {
            Foo f;
            if(f = Foo.MakeFoo(5)) {
                // Something here
            }
        }
    }
    
  • Nikolai (unregistered)

    Never coded in C# myself - is accepting lists by value instead of a reference a common practice there ?:)

  • (cs) in reply to Nikolai
    Nikolai:
    Never coded in C# myself - is accepting lists by value instead of a reference a common practice there ?:)

    Yes. The only thing being passed by value is the reference, which is similar to a pointer. So unless the method is actually changing the object the reference points to, there is no need to pass by reference.

  • Dirk (unregistered) in reply to SR
    SR:
    pbean:
    Oops I forgot the code tags. Fixed :)
    pbean:
    public bool IsListconntSmalleThaOne(IList<IContact> listOfResults)
    {
      int count = 0;
      foreach (IContact contact in listOfResults)
      {
        count = count + 1;
      }
    

    if (count >= 1) { return 1 != 0; // I don't trust "false" } else { return 1 == 1; // is "true" really true?? } }

    Thanks. That makes it so much more readable (saner, too).

    It's all true!

  • darkYuris (unregistered) in reply to Nick

    What if it's 'FileNotFound'?

  • RF (unregistered)

    This may actually be helpful in debugging, if say, you want to put a breakpoint no either one of the branches.

  • dmilette (unregistered) in reply to Knux2

    Stop it!!!

    My brain hurts now.

  • Anon (unregistered) in reply to Anonymous
    Anonymous:
    Oh my God that hurts. This is exactly the sort of shoddy coding that pisses me off the most. Atrocious spelling, useless method name, pointless functionality and it doesn't even work as advertised. But what really compounds the WTF is that this is so damn simple. When you screw up this badly doing something this simple, it's time to seriously consider whether software development is your calling in life. Note to the submitter, maybe you should leave a McDonalds job application form on his desk, see if he gets the hint.

    Unfortunately the problem stems from the fact that a lot of 'programmers' aren't really programmers... they're coders. These are the same fools that stay in the same job forever while the rest of us professionls that actually know what we're doing move on to bigger and better places.

Leave a comment on “IsListconntSmalleThaOne”

Log In or post as a guest

Replying to comment #:

« Return to Article