Skip to content

The Weird and The Wonderful

It was the best of code, it was the worst of code. Coding Horrors, Worst Practices, and flashes of brilliance

This category can be followed from the open social web via the handle the-weird-and-the-wonderful@forum.codeproject.com

1.8k Topics 20.7k Posts
  • Must love to run twice

    csharp database
    5
    0 Votes
    5 Posts
    15 Views
    J
    That is a race just waiting to happen!!! In C# I try not to use an 'is' followed by a cast for the same reason. The following code reads so much nicer: Bar bar = o as Bar; if(bar != null) { } than this: if(o is Bar && !object.ReferenceEquals(o, null)) { }
  • WriteOnly

    csharp java dotnet visual-studio
    43
    0 Votes
    43 Posts
    203 Views
    J
    I am a 8yr VB (and generic BASIC) refugee and I must say that C# has given me a warm fuzzy feeling since I started using it. One word: keystrokes ;).
  • DateTime truncation

    com xml
    7
    0 Votes
    7 Posts
    9 Views
    M
    I did the same. Something along the lines of: if (bool.TryParse(someString, out checkBool)) { someProp1 = checkBool someString = someProp1.ToString() } if (DateTime.TryParse(someString, out checkDate)) { someProp2 = checkBool someString = DateTime.Parse(someProp2.ToString()).ToString() } It's part of a dynamic Property Grid. Since I couldn't find any type checking for Property Grids, I had to create my own. And without the above code, if you entered stuff like "trUE" it would remain like that. With the above code, the user entries are formatted and dates always have the same format as well.
  • if...AND...else! [modified]

    question
    14
    0 Votes
    14 Posts
    27 Views
    realJSOPR
    Actually, you should scope it with parens: return (first > second); On the other hand, why write a function that does that when you could eliminate the overhead and stack usage by doing the comparison in the calling function? "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
  • Sort by the first 3 characters

    8
    0 Votes
    8 Posts
    12 Views
    K
    This enhances readability. :-O Greetings from Germany
  • A function for every minute of the day...

    ruby
    16
    0 Votes
    16 Posts
    27 Views
    A
    I hope you have unit tests with 100% code coverage for each one of those methods. I can imagine the sinking feeling one would have after ordering my book, only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon
  • array size

    data-structures
    16
    0 Votes
    16 Posts
    23 Views
    V
    Reminds me of "The story of Mel" : I had been hired to write a Fortran compiler for this new marvel and Mel was my guide to its wonders. Mel didn't approve of compilers. "If a program can't rewrite its own code," he asked, "what good is it?"
  • OK how do you like this: JavaScript in SQL?

    javascript database question
    5
    0 Votes
    5 Posts
    13 Views
    M
    I agree completely. String substitution in SQL is pretty stupid (it's not very good at string manipulation), non-parameterized SQL queries are very stupid as they're easily subjected to SQL Injection attacks, and both together (and there's a third language here as well, HTML) is just confusing as all hell. I'd split this up into only retrieving actual values from SQL Server using parameterized queries, writing a real JavaScript function that calls window.open, then writing formatted string code to generate the HTML to call the JavaScript function passing the parameters, and passing that through an HTML/XML entity encoding routine where that turns out to be necessary. As it is, that's an unmaintainable mess. DoEvents: Generating unexpected recursion since 1991
  • Is this a horror? [modified]

    c++ com data-structures tools question
    19
    0 Votes
    19 Posts
    27 Views
    T
    asrelu wrote: "if(p)" is easier to write and also easier to understand instantly what it means. I'm sure the generated code is the same because the code optimization will elliminate the pointless evaluation of the logical expression from "if(p == NULL)". I'm old school, too, and I respectfully disagree. I use "if (p)" when p is used as a boolean. (It might actually be an int, because some of the old-school C that I help maintain). This is reminding me that p is a flag. When I'm using a pointer, I use "if (p != NULL)". Sure, the compiler might optimize it to the exact same code as above but the content reminds me that p is a pointer. Just my two cents. Your mileage may vary. This package is sold by weight, not by volume. You can be assured of proper weight even though some settling of contents normally occurs during shipment.
  • You're using... ?

    csharp question visual-studio announcement
    13
    0 Votes
    13 Posts
    23 Views
    A
    SharpDevelop is still good, and its free. in ur case you friend didi not uploaded the project file(Csproj) which sharpdevelop creates!.
  • Overflow exception handling

    database help question
    5
    0 Votes
    5 Posts
    14 Views
    CPalliniC
    It depends on the programming language you're using (we have C++, C#, VBand even Java forums). You may also as the SQL/ADO/ADO.NET forum if you feel the problem springing from the database. :) If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile. This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
  • Does this count?

    help question
    17
    0 Votes
    17 Posts
    20 Views
    A
    This is a function of mine, could I get a critique on this? The reason for it being there is because exceptions coming out of the lower level library were not very descriptive or helpful to the guy trying to understand where the config was screwed so this layer was added to get some extra info into the exceptions that were coming up if files were missing and so on. Public Shared Function GetInt(ByVal iniFile As String, ByVal sectionName As String, ByVal keyName As String) As Integer Dim i As Integer Dim ConfigFile As IniConfigSource Dim ConfigSection As IConfig Try 'check file exists If (Not System.IO.File.Exists(iniFile)) Then Throw New Exception("Config file does not exist: " & iniFile) End If 'Access the file ConfigFile = New IniConfigSource(iniFile) 'Get section ConfigSection = ConfigFile.Configs(sectionName) 'check section exists If (IsNothing(ConfigSection)) Then Throw New Exception("Config section does not exist: " & iniFile & vbTab & sectionName) End If 'get key and check it is a valid value Try i = ConfigSection.GetInt(keyName) Catch ex As Exception Throw New Exception("Exception attempting to access integer key: " & iniFile & vbTab & sectionName & vbTab & keyName) End Try Return i Catch ex As Exception 'Exceptions come in here Throw ex End Try End Function
  • userid=="administrator"

    career
    13
    0 Votes
    13 Posts
    33 Views
    L
    I had the same objection to this while going through some code with the boss many years ago. I always checked for null separately before doing anything else. He did it just the same way like in the code here. He argued that the check for null values is always done first and does not run into the second term if the value is indeed null. This may not be sound proof, but the entire code was full of this and we never had any problems because of that. A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.
  • Accessing the app.config...

    9
    0 Votes
    9 Posts
    12 Views
    realJSOPR
    That doesn't forgive you the responsibility of becoming familiar with the rules before you start posting. "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
  • Another miss

    help
    3
    0 Votes
    3 Posts
    5 Views
    S
    maybe it's like... you just want to be sure ;) (yes|no|maybe)*
  • Free Nothing

    14
    0 Votes
    14 Posts
    28 Views
    S
    Too bad 'free()' wasn't defined as returning a (void *). In that case, the code could have been written as: flag = free(flag); In case of any error other than a null pointer being passed in, free() could return the passed-in pointer. Similar behavior could have been used with fclose() [return a (FILE*)] and other such functions that destroy the object whose pointer is passed to them. Oh well, only a few decades too late now.... Where's my time machine?
  • Two Coding Horrors I overheard today...

    database sql-server sysadmin help
    11
    0 Votes
    11 Posts
    29 Views
    B
    Do you guys all work at M$?
  • Recruiting Horror

    question career csharp java oracle
    18
    0 Votes
    18 Posts
    26 Views
    C
    I would have done the same. When I read the first few lines I thought it was some kind of joke and then it got worse.
  • Short, Simple and Utterly Pointless

    csharp ruby
    6
    0 Votes
    6 Posts
    15 Views
    N
    :laugh: :laugh: :laugh: Greetings. -------- M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you “The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
  • Useless...

    java ruby
    17
    0 Votes
    17 Posts
    30 Views
    Y
    :-O