Click here to Skip to main content
15,890,512 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: if bool Pin
ThatsAlok12-Mar-07 18:41
ThatsAlok12-Mar-07 18:41 
GeneralRe: if bool Pin
eugene.genis13-Mar-07 23:20
eugene.genis13-Mar-07 23:20 
GeneralRe: if bool Pin
Russell Jones12-Mar-07 23:08
Russell Jones12-Mar-07 23:08 
GeneralRe: if bool Pin
Dan Neely13-Mar-07 2:15
Dan Neely13-Mar-07 2:15 
GeneralRe: if bool Pin
Jörgen Sigvardsson15-Mar-07 13:17
Jörgen Sigvardsson15-Mar-07 13:17 
GeneralRe: if bool Pin
Bradml12-Mar-07 23:12
Bradml12-Mar-07 23:12 
GeneralRe: if bool Pin
Ed.Poore13-Mar-07 0:12
Ed.Poore13-Mar-07 0:12 
Generalthe hard way Pin
Shog910-Mar-07 8:47
sitebuilderShog910-Mar-07 8:47 
Recently, i was reworking some code. The library was used to read in sets of rules, and process other data based on those rules. Over time, this library had evolved such that there were two different methods of processing rules ("simple" rules required a different code path than "complex" rules), and four different file formats for the rule storage. So before working on the required changes, i first put some work into reducing (so far as possible) the redundant code. As part of this, i wrote a class to parse the various file formats and present their contents in a consistent manner:
class CRuleReader
{
public:
   CRuleReader();
   
   void Open(...);
   bool NextRule();
   ...
   
   long GetId() const;
   LPCTSTR GetCondition() const;
   LPCTSTR GetValue() const;
   ...
};

This worked quite well, except for one thing: it was very slow. The methods for accessing rule data needed a fair bit of time behind the scenes to parse out the required information, validate it, and present it in a consistent manner. Fortunately, there was a simple solution: cache the results of internal calculations, and re-use the cached results whenever possible. I quickly made this change, and was quite pleased with the results... However, since these const methods now needed to write to the internal cache data, I'd done something rather ugly in these methods:
const_cast<CRuleReader*>(this)->CacheValueId(LPCTSTR key, long id);

Unpleasant as it looked, it allowed me to keep the public face of the class clean - methods that logically modified state were non-const, methods that did not were const. All things considered, i was still reasonably happy with it.

Then, a few days later, i stumbled on some similar code, and at last realized, that in over a decade of using C++, i'd managed to either avoid or forget the mutable keyword... Sigh | :sigh: D'Oh! | :doh: Blush | :O



----
It appears that everybody is under the impression that I approve of the documentation. You probably also blame Ken Burns for supporting slavery.
--Raymond Chen on MSDN

GeneralRe: the hard way Pin
Dmitry Khudorozhkov12-Mar-07 6:01
Dmitry Khudorozhkov12-Mar-07 6:01 
GeneralRe: the hard way Pin
Shog912-Mar-07 11:11
sitebuilderShog912-Mar-07 11:11 
GeneralRe: the hard way Pin
Dmitry Khudorozhkov12-Mar-07 11:18
Dmitry Khudorozhkov12-Mar-07 11:18 
GeneralRe: the hard way Pin
Shog912-Mar-07 13:56
sitebuilderShog912-Mar-07 13:56 
GeneralRe: the hard way Pin
Dmitry Khudorozhkov13-Mar-07 5:58
Dmitry Khudorozhkov13-Mar-07 5:58 
GeneralRe: the hard way Pin
Shog913-Mar-07 6:32
sitebuilderShog913-Mar-07 6:32 
GeneralRe: the hard way Pin
Dmitry Khudorozhkov13-Mar-07 6:38
Dmitry Khudorozhkov13-Mar-07 6:38 
GeneralRe: the hard way Pin
Shog913-Mar-07 6:57
sitebuilderShog913-Mar-07 6:57 
GeneralRe: the hard way Pin
Dmitry Khudorozhkov13-Mar-07 7:03
Dmitry Khudorozhkov13-Mar-07 7:03 
GeneralRe: the hard way Pin
Shog913-Mar-07 7:13
sitebuilderShog913-Mar-07 7:13 
GeneralRe: the hard way Pin
Roger Bamforth12-Mar-07 6:04
Roger Bamforth12-Mar-07 6:04 
GeneralRe: the hard way Pin
Rob Caldecott13-Mar-07 5:13
Rob Caldecott13-Mar-07 5:13 
GeneralRe: the hard way Pin
Shog913-Mar-07 11:58
sitebuilderShog913-Mar-07 11:58 
GeneralOracle stored procs Pin
User 5838528-Mar-07 14:34
User 5838528-Mar-07 14:34 
GeneralRe: Oracle stored procs Pin
PIEBALDconsult8-Mar-07 15:39
mvePIEBALDconsult8-Mar-07 15:39 
GeneralYou cannot have it! Pin
Rajesh R Subramanian8-Mar-07 0:59
professionalRajesh R Subramanian8-Mar-07 0:59 
GeneralRe: You cannot have it! Pin
Bradml8-Mar-07 1:04
Bradml8-Mar-07 1:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.