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
  • Dll Cleanup Bug

    help question
    5
    0 Votes
    5 Posts
    2 Views
    M
    Excellent i didn't know that... thanks to all of you who replied :) If you think you can than you can, if you think you can't you are right.
  • CriticalSections

    help architecture tutorial announcement
    14
    0 Votes
    14 Posts
    4 Views
    L
    Found it in the registry under HKLM\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options.
  • 0 Votes
    4 Posts
    2 Views
    W
    ToString uses the current thread's CultureInfo by default. Always overload ToString(CultureInfo.InvariantCulture) for persisting to file. Similarly for parsing. For the UI it's ok to use the current culture info. Wout
  • Tricky One

    c++ help
    11
    0 Votes
    11 Posts
    4 Views
    P
    IIRC you get the same error when you leave ouut the delcaration. Developers, Developers, Developers, Developers, Developers, Developers, Velopers, Develprs, Developers! We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP Linkify!|Fold With Us!
  • System.Xml.XmlDocument

    help database xml tutorial question
    10
    0 Votes
    10 Posts
    5 Views
    G
    AlgorithmsBorne wrote: But it all boils down to the fact that it accepts INVALID XML and doesn't validate the encoding if VALID XML is put into it, so it double encodes VALID XML. No, it doesn't accept invalid XML. It accepts text, not XML. If the text happens to be XML is irrelevant, it should still treat it as text, and it does. If it would treat it as XML, the implementation would be inconsistent. --- It's amazing to see how much work some people will go through just to avoid a little bit of work.
  • Could not find installable ISAM.

    csharp database visual-studio help question
    2
    0 Votes
    2 Posts
    3 Views
    C
    I just wasted 6 hours of my life over that stupid space in Data Source too.......
  • subtle bug magnet --> std::vector's clear() and empty()

    c++ com graphics adobe help
    22
    0 Votes
    22 Posts
    11 Views
    A
    I am sure a committee decided which names to use. I am not confused by these two names... This is not a std specific problem. We have the same problem in almost all libraries: empty, clear, remove, delete... hard choice. Luckily we cannot use void... we can use Void, _void, etc... :)
  • CTime and DayLight saving

    sysadmin
    13
    0 Votes
    13 Posts
    3 Views
    R
    Ryan Binns wrote: I can't comprehend the sheer lunacy that it takes to come up with the idea, or to support it. The original idea was to save energy costs. http://webexhibits.org/daylightsaving/c.html[^] -- Marcus Kwok
  • C# RegEx Match Groups

    regex csharp javascript com data-structures
    17
    0 Votes
    17 Posts
    2 Views
    M
    That's where inline comments come in (see http://www.regular-expressions.info/comments.html[^] ) A good trick described at http://www.codeproject.com/dotnet/RegexTutorial.asp[^] is : "Comments please Another use of parentheses is to include comments using the "(?#comment)" syntax. A better method is to set the "Ignore Pattern Whitespace" option, which allows whitespace to be inserted in the expression and then ignored when the expression is used. With this option set, anything following a number sign "#" at the end of each line of text is ignored. For example, we can format the preceding example like this: 31. Text between HTML tags, with comments (?<= # Search for a prefix, but exclude it <(\w+)> # Match a tag of alphanumerics within angle brackets ) # End the prefix .* # Match any text (?= # Search for a suffix, but exclude it <\/\1> # Match the previously captured tag preceded by "/" ) # End the suffix"
  • Infinite recursion from lack of a cast

    tutorial
    6
    0 Votes
    6 Posts
    2 Views
    R
    unimatrics wrote: Is this a joke? Your reply? Yes. Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
  • they say map::insert doesn't change iterators [modified]

    c++ com tools question
    3
    0 Votes
    3 Posts
    2 Views
    P
    The check is implicit in the caller, I just put it there to note that I though of overlapping. At least I thought I thought of it :cool: The problem is: endAt points to one after the last one to copy (id=149), and for specific target IDs, the item get moved to before endAt. (you are right, though. For a publicly exposed method there should be a runtime check, as it is cheap) Developers, Developers, Developers, Developers, Developers, Developers, Velopers, Develprs, Developers! We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP Linkify!|Fold With Us!
  • I hate floating point operations

    c++ com question
    63
    0 Votes
    63 Posts
    29 Views
    J
    Also don't forget - There is a wave nature to light. The universe may be considered discreet for SOME applications, but there is a duality to matter. Every object has a wavelength... I suppose we don't need calculus anymore - what use are derivatives anyway? We'll just cut everything into super tiny rectangles, since a wave can't have a continuous curve...What was Newton thinking?
  • 0 Votes
    8 Posts
    3 Views
    A
    Harold Bamford wrote: I just don't think that being critical of somebody's work, that has survived for many years, on the basis of holier-than-thou is appropriate. I don't anyone in this thread has had a holier-then-thou attitude. The only debate is the definition of fragile code. The way I define it is code with unnecessary dependencies on other parts of the application. In this case it is the UI of the application. A code wizard like that should just manipulate the files directly without being dependent on any kind of keyboard layout. Those extra dependencies which tend to build up over time are probably the biggest source of bugs in any application, which is why so many developers have such a dim view of them. Using the GridView is like trying to explain to someone else how to move a third person's hands in order to tie your shoelaces for you. -Chris Maunder
  • Implicit conversion in SQL Server

    help c++ database sql-server sysadmin
    20
    0 Votes
    20 Posts
    3 Views
    C
    There is always ISNUMERIC if you want to compare two numbers. Although I think are non-deterministic, for sure ISNUMERIC('123 1/2') will evaluate to false. So: SELECT ... FROM secondsystem A WITH(NOLOCK)  INNER JOIN firstsystem B WITH(NOLOCK)   ON   CASE    WHEN (ISNUMERIC(A.number) = 0 OR ISNUMERIC(b.number) = 0)     THEN -1    ELSE CASE WHEN CAST(A.number AS INT) = CAST(B.number AS INT) THEN 1 ELSE 0 END   END = 1 ... So the ON clause will evaluate to -1 if either A.number or B.number are non-numeric, when both are but values don't match is 0, and when are both integers and equal evaluates to 1, which is the pass condition for ON clause to consider the JOIN. Can be used a scalar function, too, to do the same: CREATE FUNCTION EqualNumbers(@A nvarchar(50), @B nvarchar(50)) RETURNS BIT AS BEGIN  IF @A IS NULL   RETURN 0  IF @B IS NULL   RETURN 0  IF ISNUMERIC(@A) = 0   RETURN 0  IF ISNUMERIC(@B) = 0   RETURN 0  IF CAST(@A AS INT) <> CAST(@B AS INT)   RETURN 0  RETURN 1 END and now the join is simply SELECT ... FROM secondsystem A WITH(NOLOCK)  INNER JOIN firstsystem B WITH(NOLOCK)   ON .dbo.EqualNumbers(A.number, B.number) <> 0 Probably the direct JOIN will be quicker than the function. If the comparation has to be between two (n)varchar values, use a convert to (n)varchar for values to be compared, or whatever logic meaning the fields to be compared have (address is a prime context for this). You can encounter later money value in A stored as varchars and money in B, so you'll have also to consider, if converting to varchar, possible locale issues when casting etc. etc. etc.
  • SQL Server Query

    database question sql-server sysadmin help
    26
    0 Votes
    26 Posts
    16 Views
    C
    Probably LIKE behaves like STUFF('%_%') ? That's why probably the query found 'supplier price' - replaced '_' with . Why using spaces in sp anyway? Much better, IMHO, to use first a prefix indicating the area of usage, followed by the purpose of sp, such as: SA_CreateInvoice (SA = SAles) WR_RetrieveItems (WR = WareHouse) PR_DeleteOrder (PR = PRoduction) AC_CreateRegistration (AC = ACcounting) and so on. (Actually, is FT = Fatturazione instead of sales, MG = Magazzino instead of warehouse etc. - I'm working for an italian company :) ) This is the kind of convention we use when naming stored procedures (tables, triggers etc).
  • Date time and timezones

    sysadmin debugging help
    1
    0 Votes
    1 Posts
    1 Views
    No one has replied
  • SQL-statements that seem correct

    help csharp database com tools
    7
    0 Votes
    7 Posts
    5 Views
    P
    Chances are. But I don't see why it would be removing the [ and ]
  • Undocumented Security 'Feature' in ASP.net

    help csharp asp-net com sysadmin
    5
    0 Votes
    5 Posts
    2 Views
    J
    All I'm doing is saying what happened and what caused it. You're free to disagree with me if you like, but that's what happened. Could have something to do with the fact I'm using a User Control, but I don't know. Also, you could have event validation turned off. I'm not sure why this happens in my site and not in yours, but it happens, and it's reproducible. If I add the double event handler call, the page breaks. All I'm saying is that, if you are having this problem, this is something to look at. I'm not garaunteeing it will fix the problem, because I know this error can be caused by a whole host of other stuff. All I know is that I don't have to fix anything else to get the page to work correctly. I wish I could get help debugging it, because I would like it to work right, but it's on a secure part of my site (not https, just password-protected). "Quality Software since 1983!" http://www.smoothjazzy.com/ - see the "Programming" section for (freeware) JazzySiteMaps, a simple application to generate .Net and Google-style sitemaps!
  • Functor not optimized away [modified]

    html help
    6
    0 Votes
    6 Posts
    3 Views
    T
    *off topic* I have seen this time any time again with templates. Due to the complexity of what the compiler is having to do, the optimizer can fail in all sorts of strange ways. In general, I have found the templates perform between %80 to %300 percent of their non-template counter parts. Overall, the optimizations work just fine. However, when it works well, it just tends to work a bit better than non-template code and when it works poorly, it REALLY works poorly. Scott Meyer's stuff about for_each working just as well as for is total bunk. For simple loops that can usually be contained in registers when using for will often result in stack space being used thus drastically hurting performance when using for_each. Tim Smith I'm going to patent thought. I have yet to see any prior art.
  • Bad use of ASSERT...

    help announcement com debugging
    20
    0 Votes
    20 Posts
    7 Views
    N
    Soundman32.2 wrote: Can I suggest you look up VERIFY? VERIFY(CreateControl()); This will do the same as assert in debug builds, but in release builds will compile to: CreateControl(); Which gives you the best of both worlds. Yeah I've already mentioned that towards the end of my message. Thanks. :) :) Nibu thomas A Developer Programming tips[^]  My site[^]