Posted on September 26, 2011 in Business of Software, Common Sense, Digital Misfortunes, Programming | Permalink | Comments (0) | TrackBack (0)
| Digg This | Save to del.icio.us |
Hi,
I guess things like this happen to you when you perform Sunday morning programming. My app got a WM_LBUTTONUP without a preceding WM_LBUTTONDOWN... And since that happened before any other button down/up activity in the application, some things were not setup properly "yet" so the app crashed.
The problem was not so much to fix the crash, as to understand what was going on and how to reproduce it. I was using the app when this happened, no other app was involved, so how could I possibly get a button up without a preceding button down?
Posted on December 05, 2010 in Programming | Permalink | Comments (0) | TrackBack (0)
| Digg This | Save to del.icio.us |
Hi,
For many years now the VC++ compiler has a very handy feature that none of the C++ programmer I have met knew about, so it may be worth writing about.
The feature is called as "virtual data members" in the Microsoft documentation and it is used extensively in COM programming. The interesting thing is not so much what it does, but what kind of problems it can help you solve very easily.
Suppose you have a class like the one shown below:
class Operation
{
int m_ZoomFactor;
public:
Operation():m_ZoomFactor(1){}
void DoSomething(){ --m_ZoomFactor; }
};
You have some member m_ZoomFactor that can take the value 0 among others. Many of your member functions are directly messing with m_ZoomFactor, like the DoSomething() member function.
But now your design has changed. Zero is no longer a permitted value. So you have to change the whole code to make sure that zero can in no way be assigned.
Now, wouldn't it be handy if you could put a data breakpoint to catch zero assignments. It would be nice, but your application is also performance critical and data breakpoints are slow, plus the application is run by testers as well, and they can't put such a breakpoint.
The next thing that comes to mind is to make a setter and a getter function for the zoom factor and then change all the code to use these functions. Nice but ugly and it takes time. What if the compiler could do all the work for you? That would be really nice! And in fact it can be done. Here is how:
class Operation
{
int m_ZoomFactor_;
__declspec( property(
get=GetZoomFactor,
put=SetZoomFactor) ) int m_ZoomFactor;
int GetZoomFactor(){ return m_ZoomFactor_; }
void SetZoomFactor(int newValue)
{
_ASSERT(newValue);
m_ZoomFactor_ = newValue;
}
public:
Operation():m_ZoomFactor_(1){}
// Original code of member functions remains unchanged.
void DoSomething(){ --m_ZoomFactor; }
};
By using __declspec property you tell the compiler to replace as appropriate all references to m_ZoomFactor with the getter or setter function. So, you can continue writing your program as before, and if by any chance you forget to check some code path that assigns zero, you will catch the offender on the spot, before the code runs astray, messes everything up and then leaves you staring at your monitor and wondering how did all the mess came about!
Needless to say, you can use this trick only for the debug build, so as to add some extra sanity checks (you cannot assign this value if the other data member has this value etc) and leave the release build working directly with the data members. The possibilities are endless!
Have Fun!
Dimitrios Staikos
Posted on December 02, 2010 in Programming | Permalink | Comments (0) | TrackBack (0)
| Digg This | Save to del.icio.us |
If there is one thing I don't like, that's whining.
The second thing I don't like is quite naturally... whiners.
And guess what? Macros in C/C++ cause a LOT of whining.
I can find on the web a myriad of articles on why NOT to use C macros, on why C macros are evil/stupid/useless/dangerous, you name it, repeating the same old stuff and defying common sense.
But people still use macros. And I see in the stats of my blog that my previous post "Tips on writing C macros" is persistently popular. Clearly a lot of people are still writing C macros.
Are these people reckless or plain idiots? Of course not! There is great value in macros, real-world value not theoretical mumbo jumbo.
So I decided it is high time that I wrote an article describing some good real-world uses for C macros and at the same time debunk some of the anti-macro myths or at least put them in the proper perspective.
There are of countless macro related articles on the web, so I will try not to repeat too many things here :-)
Posted on November 12, 2010 in Programming | Permalink | Comments (0) | TrackBack (0)
| Digg This | Save to del.icio.us |
I hate XML :-) It's a good thing really, but still I hate it. Why? Because it started of as a thingy to be used between interoperable systems (where it was needed) and ended up as a glorified INI file. Yeah I know, INI files suck big time and you are a cool developer always using the latest and greatest, so for these 5 settings your app uses you have to use XML, now don't you?
XMLishness is a dangerous sickness. Those who have it tend to think in terms of XML for almost everything. "Why should we use these dreadful .RC files for resources, let's do an XML based solution" is a typical example of the sick person's thinking. Instead of worrying how they will do their actual job (deliver a solution to the customer) they worry if there is anything else that can be XMLized.
Needless to say, to parse an XML file with 5 settings you need an XML parser, which is thousands of lines of code someone else wrote for you, while to parse a text or binary file with 5 settings you need 5 lines of your own code. But this is a minor detail for lots of people. They prefer to forget that "XML format" actually means "I need an XML parser to read it".
This is why for example the people who designed GenICam decided to describe the camera capabilities using an XML file. And that is an XML file stored into the camera and sent to the software that controls it. So that software cannot possibly run in kernel mode because most people don't have a kernel mode XML parser and of course kernel mode is for doing serious work like touching the hardware or the operating system structures, not parsing stupid setting files.
Here is what happened to someone that in my opinion incorrectly used XML for something that could have been done much more easily, and ended up parsing XML in kernel mode... The computer may restart... 真的嗎?
Have fun!
Dimitrios Staikos
Posted on November 04, 2010 in Common Sense, Programming | Permalink | Comments (0) | TrackBack (0)
| Digg This | Save to del.icio.us |
I recently watched a recorded presentation of Prof.Diomidis Spinellis, author of a couple of world famous books on programming, like "Code Quality". At some point Mr.Spinellis discussed the issue of program optimization and mentioned some famous quotes related to this issue, quotes that were also listed in his "Code Quality" book:
These come from M.A.Jackson. It is hard to exaggerate how much I agree.
I would like to add my own rules to the list :-) Maybe others have said similar words, but now I copy from my own brain:
Programmers often set sail to optimize a piece of code, just because it is plain for all to see that this piece of code CAN be optimized. However, you should do some serious thinking first to see whether it makes sense to optimize this piece of code. How often is it run, under which paths, how long does it take to execute, etc. For example don't optimize code to turn a 0.1 msec into a 0.01 msec if that runs once every minute.
Does it make sense to optimize a process that is executed once a day, if that process takes 10min, 20min, 120min, 360min? Would it make sense to optimize the 10min to 1min (10/1 improvement), the 120min to 30min (4/1 improvement) or the 360min to 120min (3/1)?
You definitely can't tell the answer by looking at the numbers. Maybe the 360min procedure runs at the end of the day, after all employees are gone, so no one benefits from the improved performance, but then the backups can be scheduled earlier. Or the 120min process runs over lunch break, so having it finished before people return from lunch does not make that much of a difference.
That's the meaning of the 3rd rule, don't look at the code searching for optimizations, look at the real world for optimizations.
The 4th rule is a bit drastic. After you decided to optimize the code, do some measurements. Careful measurements of the old implementation, careful measurements of the new implementation. If the improvement is NOT worth the while, then you might as well scrap the optimization. Throw it away. You thought you had good reason, you made extensive changes but didn't get the improvement you expected; no hurt feelings, just dump it.
Code complexity and maintainability are far more critically important than a minor performance optimization. KISS should be the topmost priority, not writing code that makes programmers feel good about themselves ;-)
Have Fun!
Dimitrios Staikos
Posted on November 15, 2009 in Programming | Permalink | Comments (2) | TrackBack (0)
| Digg This | Save to del.icio.us |
Hi
I always had a strange fascination about batch files in Windows. I like automation, but for some reason I don't like using an elephant gun (Perl, Powershell) to shoot a mosquito. Plus I like the automation stuff to work on any PC, without having to install anything new. The Windows shell commands look totally crippled, but there is more to it than meets the eye.
Continue reading "DELAY command implemented as batch file" »
Posted on October 04, 2009 in Programming, Web/Tech | Permalink | Comments (2) | TrackBack (0)
| Digg This | Save to del.icio.us |
I had some trouble today with the Windows 7 WDK when I tried to install a driver on the checked build of Windows Vista x86. Actually I found the answer after searching around a little bit, but since all the information was scattered around, I thought it might be nice to put it all together under one roof.
Here are the basic facts:
Here is how to do it in a nutshell:
Have fun :-)
Dimitris Staikos
Posted on September 16, 2009 in Programming | Permalink | Comments (0) | TrackBack (0)
| Digg This | Save to del.icio.us |
Posted on September 16, 2009 in Programming | Permalink | Comments (0) | TrackBack (0)
| Digg This | Save to del.icio.us |
Macros in C/C++ is an extremely powerful feature, basically one I can't live without. The use of macros has been widely debated and mostly they are labeled as 'evil', as a no-no. This is the usual moral dillema that shows up whenever we have something with significant power in our hands: Anything that is powerful enough can be misused one way or the other. Should we then allow the usage of the powerful tool or ban it altogether so as to prevent its misuse (intentional or unintentional)?
Posted on November 30, 2008 in Programming | Permalink | Comments (6) | TrackBack (0)
| Digg This | Save to del.icio.us |