Skip to content

Clever Code

Showcase your best code, your most elegant solution, or your most hard fought subtle bug you've found and fixed

This category can be followed from the open social web via the handle clever-code@forum.codeproject.com

361 Topics 3.2k Posts
  • CAS with .NET 2.0 and 1.1

    help csharp sysadmin security sales
    1
    0 Votes
    1 Posts
    1 Views
    No one has replied
  • Fun with Operators

    10
    0 Votes
    10 Posts
    6 Views
    S
    I believe at one point in the murky past of C++ one of the intel compilers actually had a problem where i++ was less efficient than i=i+1 due to some pipelining issue, but rumour at the time had it that this was only a problem on intel chips!
  • Do you think this is a bad idea or not( C++)

    c++ debugging help question
    24
    0 Votes
    24 Posts
    22 Views
    M
    We had On Error Resume Next in a method of a COM class which looked like this: Public Sub FormatMessage(vtArgs() As Variant) For i = 0 To UBound(vtArgs) ' Do something Next End Sub This was fine when called from a VB6 client, but in VB.NET it was possible to pass Nothing as the argument. Trying to do UBound(vtArgs) caused an error to be raised, but the next statement was the next call to UBound(vtArgs). Result: infinite loop. Hmm, maybe I should post that... Stability. What an interesting concept. -- Chris Maunder
  • Hit the bug

    debugging help c++ announcement workspace
    4
    0 Votes
    4 Posts
    4 Views
    R
    akur1 wrote: *ppStr = (char*)malloc(sizeof(char) * ( strlen( pVal ) + 1 )); When you malloc() something, you don't need to multiply by sizeof(char), since sizeof(char) is defined to be exactly 1. Always. -- Marcus Kwok
  • JavaScript bug

    help javascript debugging question
    5
    0 Votes
    5 Posts
    5 Views
    T
    Option Explicit and a full IDE with breakpoints, variable watching and single-line step-through debugging!
  • Java Integer Fun

    csharp java question learning
    17
    0 Votes
    17 Posts
    11 Views
    G
    The people who decided that should be euthanized as a service to humanity. Software Zen: delete this; Fold With Us![^]
  • Scanner Initialization Drift

    json help
    1
    0 Votes
    1 Posts
    2 Views
    No one has replied
  • The latest neat thing

    csharp asp-net database help
    3
    0 Votes
    3 Posts
    2 Views
    M
    Reminds me of the time where the customers Proxy server was caching pages :rolleyes:, took us days to figure out Steady As She Goes!
  • Blaming Crystal Reports

    csharp visual-studio help announcement
    2
    0 Votes
    2 Posts
    2 Views
    P
    Crystal Reports is extremely well named - it's fragile and expensive, no sensible person would use it for anything but a very special occasion and it is typically bought by other people who don't have to look after it. PeterW -------------------- If you can spell and use correct grammar for your compiler, what makes you think I will tolerate less?
  • Data reverting back to old data.

    asp-net csharp css game-dev announcement
    1
    0 Votes
    1 Posts
    2 Views
    No one has replied
  • Pay attention to that copy constructor [C++]

    help c++ mobile com sysadmin
    6
    0 Votes
    6 Posts
    2 Views
    L
    I only ever store boost::smart_ptr (or another boost equivalent) in containers. :) When I am king, you will be first against the wall.
  • Not strictly a bug, but it caught me by surprise

    csharp c++ java delphi help
    14
    0 Votes
    14 Posts
    7 Views
    E
    Mindflow wrote: Who could you be to be absolutely sure there are no bugs, or you predict exactly what will happen in the future, heh, you must be the best beta tester in the world. By being sure, I mean that the code runs well enough for the task I plan to assign to it. If my code does small number approximation, I needn't build it so that it can accept all weird inputs and make it a bloated all-around function. I need one simple functionality - I do it. That's it. Then again, what I meant mostly wasn't exactly obfuscation, rather 'compression' - it doesn't take that much space then (with all the formatting and the like removed). But then again I do obfuscate sometimes (putting computations in the 3rd for() clause, for example). But, like I said, I try to limit myself to simple, small blocks of code (things I can easily debug if I need to - to keep things manageable). Its all built from small blocks becoming bigger blocks becoming apps, isn't it? :) (I mean, just look at Linux ;P) I'm not trying to write perfect code and certainly not trying to be smarter than I am. :) I'd hate myself for that 3 montsh later. :P
  • Obvious and stupid :-)

    learning
    6
    0 Votes
    6 Posts
    6 Views
    M
    yeah I get what you mean, but what I do is limit the amount of loops. Example: if I know a loop should never reach more than One Year of days, then I limit it to ~400 If the code is ever updated, a test run will show the error message anyways so that the Developer can update that part too. It's all good ;)
  • Another 2:00am one

    com tools question
    9
    0 Votes
    9 Posts
    5 Views
    E
    Cees Meijer wrote: it will bite you somewhere in the future (Sure, 100%...) Yeah, especialy when you forget about it and add more to it. :/ You're lucky if there was an ELSE (the compiler should spit out an error about ELSE without an IF statement)...
  • 0 Votes
    1 Posts
    2 Views
    No one has replied
  • Lost Data

    database help css sql-server oracle
    1
    0 Votes
    1 Posts
    1 Views
    No one has replied
  • Subtle performance issue

    debugging performance help announcement
    6
    0 Votes
    6 Posts
    6 Views
    R
    Hi there, Yes - I'm writing from a particular experience of using this technique to bug fix code that was very slow, for parsing of large human readable .ini files. So in this case performance was indeed an issue for isspace, and for CharUpper. After my post btw, I wasn't sure of these figures as they are so small, particularly for is_space - I think perhaps the optimiser was optimising it away as it had no effect on anything else. So I've done it again, this time assigning the result to successive elements in a large array so that the code can't be optimised away. Now the results are: Overhead in milliseconds for the loop and assignment to successive elements of the array for 1,000,000 loops: 24.029 isspace 53.2861 is_space 24.0346 CharUpper 118.435 char_upper_char 24.2246 (timed using the Pentium high performance timer) After subtracting the overhead from all the others: isspace 29.2571 is_space 0.0056 CharUpper 94.40600 char_upper_char 0.1956 So now it makes is_space about 5,000 times faster than isspace and CharUpper about 500 times faster than char_upper_char Hopefully that is more accurate now. Robert
  • 0 Votes
    3 Posts
    3 Views
    PJ ArendsP
    wtwhite wrote: Both "best effort" and "strict" approaches to conversion are "right" for different problems. I suggest that if you want "data validation" capabilities, you should code your own function, which may call atoi() to do the actual conversion. If you want data validation you are better off using strtol and the like (strtoul, strtod, strtoi64, strtoui64)
  • Not so much a bug...

    c++ com help question announcement
    5
    0 Votes
    5 Posts
    4 Views
    R
    Heh - that's the correct solution. My comment was meant to underline the silliness of his approach... Rob Manderson I'm working on a version for Visual Lisp++ My blog http://blogs.wdevs.com/ultramaroon/[^] My blog mirror http://robmanderson.blogspot.com[^]
  • "edata" the hidden Microsoft variable

    help data-structures debugging question
    1
    0 Votes
    1 Posts
    1 Views
    No one has replied