Showing results for Code - The Old New Thing

Apr 22, 2004
0
0

Cleaner, more elegant, and wrong

Raymond Chen
Raymond Chen

Just because you can't see the error path doesn't mean it doesn't exist. Here's a snippet from a book on C# programming, taken from the chapter on how great exceptions are. Notice how much cleaner and more elegant [this] solution is. Cleaner, more elegant, and wrong. Suppose an exception is thrown during CreateIndexes(). The GenerateDatab...

Code
Apr 21, 2004
0
0

Why the compiler can’t autoconvert foreach to for

Raymond Chen
Raymond Chen

People have discovered that the "natural" C# loop construct is fractionally slower than the corresponding manual loop: The first thing that needs to be said here is that The performance difference is almost certainly insignificant. Don't go running around changing all your foreach loops into corresponding for loops thinking that your progra...

Code
Apr 19, 2004
0
0

WM_KILLFOCUS is the wrong time to do field validation

Raymond Chen
Raymond Chen

"I'll do my field validation when I get a WM_KILLFOCUS message." This is wrong for multiple reasons. First, you may not get your focus loss message until it's too late. Consider a dialog box with an edit control and an OK button. The edit control validates its contents on receipt of the WM_KILLFOCUS message. Suppose the user fills in some ...

Code
Apr 16, 2004
0
0

Mapping all those "strange" digits to "0" through "9"

Raymond Chen
Raymond Chen

In an earlier article, I discussed how the Char.IsDigit() method and its Win32 counterpart, GetStringTypeEx report things to be digits that aren't just "0" through "9". If you really care just about "0" through "9", then you can test for them explicitly. For example, as a regular expression, use [0-9] instead of \d. Alternatively, for a regular exp...

Code
Apr 14, 2004
0
1

Not all short filenames contain a tilde

Raymond Chen
Raymond Chen

I'm sure everybody has seen the autogenerated short names for long file names. For the long name "Long name for file.txt", you might get "LONGNA~1.TXT" or possibly "LO18C9~1.TXT" if there are a lot of collisions. What you may not know is that sometimes there is no tilde at all! Each filesystem decides how it wants to implement short filenam...

Code
Apr 13, 2004
0
1

Unicode collation is hard

Raymond Chen
Raymond Chen

The principle of "garbage in, garbage out" applies to Unicode collation. If you hand it a meaningless string and ask to compare it to another meaningless string, you get meaningless results. I am not a Unicode expert; I just play one on the web. A real Unicode expert is Michael Kaplan, whose explanation of how comparing invalid Unicode ...

Code
Apr 12, 2004
0
0

The random number seed can be the weakest link

Raymond Chen
Raymond Chen

Random number generation is hard. That's why you should leave it to the experts. But even if you choose a good random number generator, you still have to seed it properly. The best random number generator in the world isn't very useful if people can guess the seed. That's why seeding the random number generator with the current time is...

Code
Apr 8, 2004
0
0

Where does the taskbar get grouped button titles from?

Raymond Chen
Raymond Chen

If the "Group similar taskbar buttons" box is checked (default) and space starts to get tight on the taskbar, then then the taskbar will group together buttons represending windows from the same program and give them a common name. Where does this common name come from? The name for grouped taskbar buttons comes from the version resource of ...

Code
Apr 6, 2004
0
1

Reference counting is hard.

Raymond Chen
Raymond Chen

One of the big advantages of managed code is that you don't have to worry about managing object lifetimes. Here's an example of some unmanaged code that tries to manage reference counts and doesn't quite get it right. Even a seemingly-simple function has a reference-counting bug. The point of this function is to take a (pointer to) a variable t...

Code
Mar 25, 2004
0
0

Regular expressions and the dreaded *? operator

Raymond Chen
Raymond Chen

The regular expression *? operator means "Match as few characters as necessary to make this pattern succeed." But look at what happens when you mix it up a bit: This pattern matches a quoted string containing no embedded quotes. This works because the first quotation mark starts the string, the .*? gobbles up everything in between, and th...

Code