Spot the Bug - Episode 1 solution and explanation
Here's the solution for the first spot the bug contest from last week.
The application compatibility issue, as many of you pointed out, is clearly hard coding the 'My Documents' path. Instead of hard coding it as shown in the bad code sample, the path should be queried from the system. This is very important especially for Windows Mobile devices that have a number of storage options such as persistent memory and external flash cards. Developers who are new to Windows Mobile may not realize that the 'My Documents' path has changed several times over the last few years. For instance, it was IPSM\My Documents in Smartphone 2002, \Storage\My Documents in Smartphone 2003, and \My Documents in the latest WM 5.0 based Smartphones. It may again change in future with advances in storage technology. The folder name may also be different in localized versions of the Windows Mobile OS. So it is a bad idea to hard code such paths in your application. Bottom-line, it is never guaranteed that the 'My Documents' folder will always be in the same location.
To obtain the correct path, use the Environment.GetFolderPath method in managed code, or SHGetSpecialFolderPath API in native code.
For example:
In C#:
String path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
In C++:
TCHAR szPath[_MAX_PATH];
SHGetSpecialFolderPath(NULL, szPath, CSIDL_PERSONAL, FALSE);
Most of you noticed the bug right away. That's good! However, there are still many developers out there who overlook this detail when writing their applications. Those are the applications that may break in the future. In fact, one of my favorite applications didn't work correctly on a Windows Mobile 5 device because the developer had hard coded a path that was only used on Smartphone 2002. If you're hard coding paths such as 'My Documents', it's time to fix your code and use the appropriate API instead.
Thanks to everyone that participated in this round, including Gabriel, Mike, Jose, Plamen, Smeegoan, Riki, Gabriel Lozano-Morán. Look out for the next Spot the Bug episode by the end of this week.