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
  • 0 Votes
    7 Posts
    4 Views
    R
    How is this subtle? If you transfer data between two different languages or environments then you must check that data types are stored in the same way. If they are not you need to provide code that transforms one representation to the other. This is basic to programming. Consider, for example, sending binary integers from a big-endian to a little-endian system. If you assume that all systems have the same endianess you're in for as surprise.
  • Access ADO - weird problem

    css sharepoint database help
    3
    0 Votes
    3 Posts
    4 Views
    C
    I did fix it by changing it from a forwardonly recordset - what I didn't do was explain it! ==================================== Transvestites - Roberts in Disguise! ====================================
  • Object initializers. [modified]

    com tools help question
    26
    0 Votes
    26 Posts
    26 Views
    J
    I look at it as one of those "if you don't like it, don't use it" things. Frankly, I think it'll make code cleaner and more concise. Instead of 10 different constructors for various combination's of things you MIGHT pass in, this syntax allows you to explicitly initialize the object how you want it. The only real change I've seen in C# so far since its creation that truly scares me is the 'dynamic' keyword in C# 4. I like the idea of it when you're trying to work with interop (like access the Excel object library, for instance), but like the 'unsafe' keyword, you should have to tell the compiler explicitly that you mean it. And THEN, it should only be usable on interop types. You should also never be able to return a dynamic type from methods or properties. In other words: // Allowed, if a DYNAMIC compiler flag is enabled. dynamic range = Excel.Application.CurrentWorkbook.CurrentSheet.Range("A1:B3"); // NEVER allowed. dynamic x = 3; Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan Portland, Oregon, USA
  • 0 Votes
    9 Posts
    4 Views
    L
    antoncl wrote: If lambda functions had been implemented "the right way", the dynamic environment as it existed when the lambda function was defined would have been preserved. It is being preserved, but you, the user, is modifying the value of the captured variable outside the closure. And by design the closure should see the side-effect. This is exactly the way any language with lexically scoped closures would behave, like Scheme (and probably python and ruby and F#), but NOT Common Lisp. xacc.ide - now with TabsToSpaces support IronScheme - 1.0 beta 1 - out now! ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
  • T-SQL: null != value evaluates false [modified]

    database php com tools question
    10
    0 Votes
    10 Posts
    7 Views
    D
    something can BE null, but it can not be EQUAL TO null. Just like, for example, something (in real life) can be Big, but it cannot be Equal to Big.
  • C# Compiler bug 2

    csharp help question
    7
    0 Votes
    7 Posts
    3 Views
    L
    Error 1 Expression cannot contain anonymous methods or lambda expressions Error 2 Cannot convert anonymous method to type 'string' because it is not a delegate type VS 2008 Professional SP1 here. regards
  • controller.v [modified]

    asp-net csharp com docker tools
    5
    0 Votes
    5 Posts
    3 Views
    T
    Thomas Weller wrote: I assume that because one can say that an IoC-container is about requesting previously registered services. I don't think there is one (or should be) having this weird name. This sounds like part of the 'magic', or maybe it's something that LinqToSQL thinks it should have by the sheer law of nature... I had many similar thoughts on what it could be. I wasn't able to find it until I downloaded the source code to Autofac and ASP.NET MVC and was able to step through them. It ended up being a copy'n'paste bug. src="v/images/search.gif" should have been something like src="../../Content/images/search.gif". The url /v/images/search.gif matches the default /controller/action/id pattern that MVC looks for. So Autofac was trying to construct a controller object named v which resulted in an obscure exception message. Another good tip is to add routes.IgnoreRoute ("favicon.ico"); or you'll get an error on that one as well. Todd Smith
  • 0 Votes
    4 Posts
    2 Views
    T
    Kevin McFarlane wrote: Of course, if I'd have been at work, a co-worker would most likely have spotted this. Yeah, sometimes it is rough being on your own. I am the ONLY .net programmer at my company. On bigger projects I work with consultants but 95% of the time, I am on my own. I didn't get any requirements for the signature
  • C++: Reading an ifstream

    c++ graphics json help question
    4
    0 Votes
    4 Posts
    2 Views
    PJ ArendsP
    Yeah, that was it. The solution was to check for the end of file while attempting to bypass the comment line. // bypass the rest of the line as it is a comment while (next != '\n' && !targetfile.eof()) { Lesson: always check for EOF when reading a file in a loop.:sigh: You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!
  • strange sum

    c++ csharp visual-studio question
    27
    0 Votes
    27 Posts
    30 Views
    P
    Yes, good point. The example given is extreme and nobody in their right mind would code that way but I imagine more subtle examples that might slip through on a Friday afternoon are not hard to think up: int i = ++i1 + ++i1; // whoops, meant ++i2 Paul Sanders http://www.alpinesoft.co.uk
  • ref keyword bad communication between .net languages!

    csharp business help
    8
    0 Votes
    8 Posts
    5 Views
    P
    and which probably isn't what the code does anyway.
  • IList and Reference Types in C# [modified]

    help csharp
    14
    0 Votes
    14 Posts
    6 Views
    J
    :wtf: , - Read the docu about the ref-keyword! This is what I'd do before I post "strange behaviour/bug" here....
  • SQL "bug"

    database help
    10
    0 Votes
    10 Posts
    2 Views
    I
    I've seen cases (depending on specific implimentation of SQL) where a supposed varchar will pad. But it's only a suggestion - and easy enough to test, I'd think :)
  • Directory.GetFiles oddity.

    help debugging lounge
    12
    0 Votes
    12 Posts
    3 Views
    E
    :laugh: think fast, be brave and dont stop.
  • I learn something new everyday

    database csharp performance help sharepoint
    2
    0 Votes
    2 Posts
    2 Views
    P
    I wonder if this applies to SQL Server 2008 as well. "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
  • VB.Net's Dim [modified]

    csharp php visual-studio com
    20
    0 Votes
    20 Posts
    5 Views
    R
    I agree, and I do that on a regular basis but in fact the older version so FxCop (not sure if the new version do this0 tell you that doing that is wasted because the framework takes care of the initialization for you. I had just disable that rule in FxCop but I found it interesting that their tool was telling us to do exactly what decades of CS training and classes have beat into our heads as gospel. FFRF[^] My LinkedIn profile[^] My Programmers Blog[^]
  • Dyslexia

    database tools regex help question
    5
    0 Votes
    5 Posts
    4 Views
    P
    MidwestLimey wrote: Not entirely sure that this should be classed as subtle Sure it can. That is why I use Expresso when working with regex's :) "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
  • Double Buffering gone mad

    com graphics performance help
    7
    0 Votes
    7 Posts
    6 Views
    L
    I don't care if it's not subtle - i seriously lol'd :D
  • Stop the press, I made a bug!

    help csharp com question code-review
    10
    0 Votes
    10 Posts
    6 Views
    M
    Pete O'Hanlon wrote: Because he's fubared beyond all belief. Pretty much describes my life and my clients. :-D :rolleyes: Marc Thyme In The Country Interacx My Blog
  • 0 Votes
    2 Posts
    2 Views
    O
    ThreadPool threads don't have a message loop. Intended behavior.