• brendan (unregistered)
    Tim Gallagher:

    The long way around.

    Modern language frameworks provide many useful common functions -- sometimes, however, developers may not be familiar with what is available and end up (unnecessarily) writing their own versions of these functions.

    Sometimes, the version of the language they are using didn't have the functionality available when it was written.

    Tim Gallagher:
    After studying the above code for a while to figure out what it did, the discoverer couldn't help but rewrite into a more efficient way to access a property:
    if (p is AxxxxMxxxxBxxxx)  {
    _appState = (enmCCLAppState)((AxxxxMxxxxBxxxx)p).AppStateId;
    }

    [Note: Revealing class names have been replaced with xxx's -- the original developer may have made some mistakes, but, at least in this case, really bad names was not one of them.]

    I don't really see anything wrong with this code. Maybe some spaces between casts. e.g.

    if (p is AxxxxMxxxxBxxxx)
    _appState = (enmCCLAppState)( (AxxxxMxxxxBxxxx) p).AppStateId;
    would make it readable, but other than that, I see nothing wrong. 

     

  • Will (unregistered) in reply to chrismcb
    chrismcb:
    Tim Gallagher:
    if (p.GetType().ToString() ==
    "Cxxxx.Cxxxx.Cxxxx.Wxxxx.AxxxxMxxxxBxxxx")
    {
    }
    if (p is AxxxxMxxxxBxxxx)  {
    _appState = (enmCCLAppState)((AxxxxMxxxxBxxxx)p).AppStateId;
    }

    Uhm, I'm not exactly a C# guru, but are those two statements equivalent? Wont the second case generate some false positives? What happens if the user has Cxxxx.Cxxxx.Cxxxx.Wxxxx.AxxxxMxxxxBxxxx and Cyyyy.Cyyyy.Cyyyy.Wyyyy.AxxxxMxxxxBxxxx?

     

    Not quite.  As has been said, the former is necessary if the assembly containing AxxxxMxxxxBxxxx is loaded at runtime.  However, if it's referenced at compile time, then the second is the way to go.  It will not generate false positives because it will only compile if there are no ambiguous type names referenced.  If there are, you'll have either to explicitly state the namespace when using them or define an alias for them at the top of the file.

  • Will (unregistered) in reply to Will

    Also, the former is an absolute nightmare when it comes to maintainability/refactoring.

  • Will (unregistered) in reply to Russ

    Anonymous:

    I know many languages, both strongly and weakly typed.   I have no problem with the decimal conversion (other then it's an additional, step that should be implicit in the first place). 

    I do think that this code would be much cleaner:

    hoursSinceLastRefresh = DateTime.DateDiff('h',DateTime.Now, cachedOn);
    It more clearly explains what you're trying to do.  You need to KNOW that using an overloaded - operator on the dates will give you another date on which you need to find the totalHours method.  It would be much easier for people to find the DateDiff function in the DateTime class... 
     
    Overloaded operators are a pain in general, which is why java got rid of them...  along with pointers... but then again, you probably like buffer overflows... otherwise where would 99% of exploits come from?
    Maybe someday you will learn a language that doesn't make coding dangerous, and which doesn't force you to do explicit conversions.  If it takes a computer a few extra cpu cycles to auto convert the value to whatever format I need, but saves me 10 keystrokes, I can live with that.  

     

     Captcha: 1337
     

    What?  Do I detect some bitterness about C#? :)

    You also need to KNOW that there's a DateDiff method in DateTime.  Have you ever heard of something called documentation?  How are overloaded operators a pain?  How do they make coding a language "dangerous"?  What's wrong with explicit conversions?

    What could be more clear and intuitive than subtracting one date from another?  There's only one thing it could possibly give you: the difference between them.  Why bloat the class with unnecessary static methods?  You do of course have to use them carefully, but used in moderation they're incredibly useful.  Imagine you're writing a program to simulate physics: you have classes for vectors, matrices etc.  Would you rather use Vector.Add(vector1, vector2), or vector1 + vector2?  Or comparing assembly versions: Version.Compare(version1, version2) (which would have to return an integer or something) is horribly messy in comparison to version1 < version2 or version1 == version2.

    Also, why the Christ would you use strings to tell a method what to do?  Not only does it introduce the problem of validation, but it makes it very unclear as to what the string needs to contain (you'd have to trawl through the method's documentation to find out what strings are valid).  If you use enumerations, however, it removes the necessity for validation (the program won't even compile if you don't pass the method a valid argument), but (with a good IDE, and something like IntelliSense, there's no need to look up the valid arguments to be used; they're already there in front of you.
     

  • Hill (unregistered) in reply to philotfarnsworth
    Anonymous:

    > I try to avoid anything written by Robert Chartier like the plague...

    Personally, I try to avoid anything written by Albert Camus like, "The Plague."

     

    Hah!!!  Outstanding.  Made my day.  Thanks!

    Hill

     

  • Superlexx (unregistered)

    LOL what C# n00b wrote this one?

    p.GetType().ToString() == "Cxxxx.Cxxxx.Cxxxx.Wxxxx.AxxxxMxxxxBxxxx"


    is not equal to


    p is AxxxxMxxxxBxxxx


    cause the first one checks for type equality while the second one checks if you can cast the object to the type

    so the "corrected" code introduces a possible regression (in case you have castable classes that must be treated differently in the method)

    Damn, the one who wrote this forum software must be publically executed, this f*cking POS requires users to loose their browsers' security settings to submit a f*cking piece of text

  • James (unregistered) in reply to Superlexx

    Not to mention that it's slow to access. I wouldn't be surprised if there weren't a few WTFs in the forums themselves.

  • (cs) in reply to Superlexx

    Good point there Superlexx; I missed that hangs head in shame. Though in this case I'd be surprised if there were subclasses to deal with, you are entirely correct. p.GetType() == typeof(Cxxxx.Cxxxx.Cxxxx.Wxxxx.AxxxxMxxxxBxxxx) would be a better way to do it though, a string comparison against a constant is ugly.

  • Russ (unregistered) in reply to Will
    Anonymous:

    Anonymous:

    I know many languages, both strongly and weakly typed.   I have no problem with the decimal conversion (other then it's an additional, step that should be implicit in the first place). 

    I do think that this code would be much cleaner:

    hoursSinceLastRefresh = DateTime.DateDiff('h',DateTime.Now, cachedOn);
    It more clearly explains what you're trying to do.  You need to KNOW that using an overloaded - operator on the dates will give you another date on which you need to find the totalHours method.  It would be much easier for people to find the DateDiff function in the DateTime class... 
     
    Overloaded operators are a pain in general, which is why java got rid of them...  along with pointers... but then again, you probably like buffer overflows... otherwise where would 99% of exploits come from?
    Maybe someday you will learn a language that doesn't make coding dangerous, and which doesn't force you to do explicit conversions.  If it takes a computer a few extra cpu cycles to auto convert the value to whatever format I need, but saves me 10 keystrokes, I can live with that.  

     

     Captcha: 1337
     

    What?  Do I detect some bitterness about C#? :)

    You also need to KNOW that there's a DateDiff method in DateTime.  Have you ever heard of something called documentation?  How are overloaded operators a pain?  How do they make coding a language "dangerous"?  What's wrong with explicit conversions?

    What could be more clear and intuitive than subtracting one date from another?  There's only one thing it could possibly give you: the difference between them.  Why bloat the class with unnecessary static methods?  You do of course have to use them carefully, but used in moderation they're incredibly useful.  Imagine you're writing a program to simulate physics: you have classes for vectors, matrices etc.  Would you rather use Vector.Add(vector1, vector2), or vector1 + vector2?  Or comparing assembly versions: Version.Compare(version1, version2) (which would have to return an integer or something) is horribly messy in comparison to version1 < version2 or version1 == version2.

    Also, why the Christ would you use strings to tell a method what to do?  Not only does it introduce the problem of validation, but it makes it very unclear as to what the string needs to contain (you'd have to trawl through the method's documentation to find out what strings are valid).  If you use enumerations, however, it removes the necessity for validation (the program won't even compile if you don't pass the method a valid argument), but (with a good IDE, and something like IntelliSense, there's no need to look up the valid arguments to be used; they're already there in front of you.
     

     

    Not all languages support enums.  Perhaps with a good IDE it will put help you put in the enums, but some of us don't like to shell out god knows how much money for Visual Studio.   I agree that it's better to catch these types of errors at compile time, rather then run time, so that makes sense. 

    In ColdFusion for example, we can use CFEClipse (a free plugin to the open source Eclipse editor), which gives us the valid arguments to use for the string argument to DateDiff.  It would be  nice to catch issues at compile time, but coldfusion code is not compiled until it's run, so that's out anyway. 

    All I know is that ColdFusion could be coded in notepad, but I don't think you can say the same about ASP.NET for example.  Maybe the express version of visual studio is enought to code ASP.NET pages, I don't know.  But the language is unnesessarily complex

  • (cs) in reply to Russ

    First ALL LANGUAGES are written in a text editors of some sort. No one would use them if they couldn't. The days of using punch cards is over m'friend. (So, yes ASP.NET can be coded using notepad. I've done it. It's a pain for anything more than Hello World but it can be done).

    Second, .NET is not a scripted or interpreted language in the same way that Cold Fusion is. .NET code (C#, VB.NET, etc) when it is compiled it is really translated into an intermediate language (aka IL or MSIL). When that library or executable is first run, the IL is then compiled into binary if has not already been compiled. This is a big reason why .NET code is substantially faster than classic ASP or Cold Fusion: it is actually compiled binary code running and not being reinterpreted on each execution.

    (Does anyone still develop real commerical sites in Cold Fusion any more? I thought it was totally defunct.)

  • jjjj (unregistered) in reply to Superlexx
    Superlexx:
    LOL what C# n00b wrote this one?
    p.GetType().ToString() == "Cxxxx.Cxxxx.Cxxxx.Wxxxx.AxxxxMxxxxBxxxx"
    is not equal to
    p is AxxxxMxxxxBxxxx
    cause the first one checks for type equality while the second one checks if you can cast the object to the typeso the "corrected" code introduces a possible regression (in case you have castable classes that must be treated differently in the method)Damn, the one who wrote this forum software must be publically executed, this f*cking POS requires users to loose their browsers' security settings to submit a f*cking piece of text
    '

    XXX XXX XXX X X X X X X X X X X XXXXX XXX XXXXX

  • xanax online borate blacktopping (unregistered) in reply to brendan
    Comment held for moderation.

Leave a comment on “The Van Gogh Awards”

Log In or post as a guest

Replying to comment #:

« Return to Article