Archive for August, 2009

Best SQL Server 2005/2008 Index Maintenance Script Ever

ccrp_0908_04_z+electric_q_jet_carburetor+rebuild[1]It covers reorganization when it makes sense, rebuilding when it has to, has a debug mode, intelligently determines whether online rebuilds are possible or not, is well documented, and generally seems like a very professional SQL script, other than a single typo bug due to the conversion from HTML.

Use it.

Tags: ,

C# vs. C++ — Swimming With the Current

1696193472_34d83c57f3[1]I know I just posted on the glories of smart pointers in C++.  I love C++ like a brother, and I have many C++ friends, but this article does a great job of illustrating, with wit and great examples, one of the most important truths about programming that I think I can pass on, and it’s not flattering to C++:

If you can manage it, try to swim with the current.

The link above is really a series of articles comparing the development of a simple application to read a Chinese dictionary in both unmanaged C++ and C#.  You should read it — it’s very interesting and educational — but the upshot is that although the C++ author eventually got his fully optimized performance to exceed that of the fully-optimized C# program, it took a large amount of very touchy, bug-prone work to do so, while the naïve version of the C# program already had great performance out of the box, and was easily optimizable to speed up further.

Why is this?  You can blame Microsoft’s substandard support for C++, and that’s probably partially right.  You can blame the glacial pace of improvement in C++ Standard Library design and performance, and that’s probably partially right as well.

But why are both of these situations the case?

The fundamental reason is that, for most non-systems programming tasks, the state-of-the art — the current — for development on Microsoft platforms is strongly flowing toward C# and .NET.  .NET is getting the benefits of new technologies and new advances in the craft of software development, and C++ really isn’t.  You can still paddle around in the C++ backwater, but very little in the way of support and improvements is going to be coming your way.  TR1 is very nice (and very overdue) but at this point it’s almost too little, too late.  Those of us who have to stay in the backwater are just going to have to get used to the faintly stagnant smell.

For most development, the current heading toward C# and .NET is almost certainly a good thing.  Personal computers are not the same as they were when C++ was first rising to dominance in the early 90s.  Multicore machines are the norm now, with multiple orders of magnitude more memory and disk storage than ever before.  Modern CPU pipeline optimizations are so complicated that trying to hand-optimize assembly code for a modern processor is almost laughable; a compiler will always do a better job than you can now, and that certainly wasn’t the case 15, even 10 years ago.

When you have memory to spare, a terabyte of hard disk space, most of the work you are doing is manipulating top-level UI constructs, your minimum-spec machine has an 8-core processor, and preventing obscure malicious exploits is a priority, does it make sense to use a language designed around minimalist efficiency, where threading is a foreign operation bolted on with great difficulty, memory is managed manually out of the box, security is an afterthought and you end up with a static executable optimized for the lowest common denominator of machine?  Or might it be better to use a language with built-in garbage collection, easily-used multithreaded constructs and just-in-time optimization based on the environment the app is running in?

C++ is a great language for many purposes, but the current has passed it by for user-level application programming.  Swim with the stream if you can!

Tags: , ,

In Praise of Smart Pointers

hacker_bomb[1]At work I program in C++, which is a great and powerful language but also an inherently unforgiving one that imposes dire penalties for failing to adhere to its ritualized memory-management rules.  Simple programs are easy to make work correctly, but anything complex runs the risk of a memory leak, which can eventually bring a system to its knees, or a double-deletion or dereferencing of a bad pointer, which can cause an instantaneous program crash.

The issue of memory management in C++, in fact, is such a big problem that there are all sorts of methodologies, class libraries, third party tools, and even alternative languages that have been developed, wholly or partially, to avoid this problem.

In my opinion, however, there is nothing easier or more effective in the vast majority of cases than using the Boost library’s (and now the TR1 library’s as well) shared_ptr<> class.  shared_ptr<>, as the name implies, provides shared access to memory, which is compatible with the behavior of raw system pointers — multiple smart pointers can reference, or “own”, the target memory at one time.  This means that you can easily substitute shared_ptr<> objects anywhere you would normally use a raw pointer.

However, unlike raw pointers, shared_ptr<>; objects support internal reference counting, which (except for some obscure circular reference cases) simply makes the memory management issue go away.

An example of how this might (not) work, with old-style pointers:

int main(int argc, char *argv[])
{
    int *i1 = new int;
    {
        int *i2 = i1; // two raw pointers now point to the same int value.
        delete i1;
        printf("%ld\n", *i2); // if above line is uncommented, we crash due to a deleted object being
        // accessed!  If the above line is commented out, we'll leak the pointer at the
        // conclusion of the program.
    }
    return 0;
}

With shared_ptr<>, there’s no need to use the manual “delete” operator any more — shared_ptr<> internally tracks how many total shared_ptr<> objects reference the contained object, and deletes it automatically when there are no references remaining.  The code thus simplifies to:

using boost;
int main(int argc, char *argv[])
{
    shared_ptr i1(new int);
    {
        shared_ptr i2 = i1; // two shared_ptr objects now point to the same int value.
        printf("%ld\n", *i2); // works fine
    } // at the end of this scope, the i2 object gets destroyed, but the reference
      // count in its destructor sees that there is still an outstanding reference, so
      // it's not deleted yet.
    return 0;
} // now i1 is destroyed, the shared_ptr destructor sees that there are no
  // remaining references, and automatically deletes the int.  No crash, no leak,
  // no manual memory management!

Developers like this behavior because they can drop common memory management tasks completely out of their minds and focus on more important implementation issues.  Architects like it because it’s easy to retrofit into large existing codebases.  Managers like it because it’s a magic bullet for stability.

So what’s the catch?  Well, there are a few:

  • First off, it’s a bit slower than using raw pointers.  shared_ptr<> uses an internal reference count, which makes the object larger than a corresponding raw pointer.  This can cause speed issues in certain applications.  However, most higher-level code is not strongly performance-sensitive, so this is usually not an issue.
  • Second, you can’t use the standard casting operators on them.  Boost provides alternative functions to do what you need to do, but their syntax is slightly different, which increases the amount of code you need to change to retrofit them into an existing system, and is one more thing to remember when you’re working with them.
  • And finally, they are verbose.  typedef will definitely become your friend when setting up shared_ptr<> support in a system.  You will likely want to set up a macro for typedef-ing new types of shared_ptr<> objects to cut down on the excessive keystrokes even so.

There’s a lot more to smart pointers in general, and shared_ptr<> in particular than I’ve covered here, but 99% of the time this is all you need to get rolling with them. Obviously, use C# or Java if your application calls for it, but if you are in the C++ world by force or by choice, get with the program.  Folks, it’s almost 2010.  If you’re still using manual memory management in cases where you aren’t certain you need to (and if you’re not certain, you probably don’t need to) you are not being smart. Let smart pointers make your code smart, and give you the time to solve real problems rather than fiddle around with memory.

Tags: , , ,

IFComp Update #9

blacksmith[1]Again, I got a substantial amount of work done this week.  I finished up Scene 2, as I figured I would, and actually got all the way up to the end of Scene 3.  I finally stalled out due to realizing that the way I had implemented a computer was suboptimal, and limits where I can go with some of the puzzles I’m implementing in later scenes.  Luckily, I put in some test code for an alternate approach and it works fine, so with a little bit of armor-plating I should be able to move on from that problem fairly quickly.

I had originally implemented the computer as a backdrop-type object, as it is accessible from multiple areas and this is what a backdrop is intended to do.  However, a backdrop cannot also be a person, and you must be a person to be talked to or asked questions.  I had tried to get around the problem by translating “ask computer about X” into the action of “consulting”, which is legal for an inanimate object.

Unfortunately for that technique, I’d also like to support text in the form of “computer, open the pod bay door”, which is rejected out of hand by the Inform 7 Standard Rules if you’re talking to a backdrop.  By making the computer a person with a fixed location, but changing the scoping rules to allow interaction from anywhere, I can get around this.  Of course, I’ll need to write code to make touching, pushing, smelling, etc. the computer infeasible except for around the computer’s actual hardware, but I would have had to have done that anyway.  The only extra work will be to ensure that the default “person” responses are appropriate for a disembodied computer interface, and to change them if they aren’t.

Scene 3 should be complete almost immediately the next time I sit down to write, and Scene 4 is not going to take very long.  The real test will be Scene 5.  In that scene there are several intricate puzzles that will test my Inform 7 coding skills quite a bit more than what I’ve done so far.  If I can get over that hump, the remaining scenes should go fairly quickly.  I’m confident that if I can continue to make progress at this rate I can have a testable game early in September for my volunteer beta testers.  It will still require a lot of polish work, but it should be playable.

Tags: ,

Book Review — The Twilight Series

twilight_book_cover[1]The first step is admitting you have a problem:

“Hi everyone, my name is Matt.”

“Hi, Matt.”

“I’m a 39-year old straight man, and I like Twilight.”

(applause, heckler yells “are you sure you’re straight?”)

***

The Twilight Saga, by Stephenie Meyer

Rating:  4.5/5

Although I’m pretty sure there’s not a 12-step program for Twilight addiction, I must say that this series was a true page-turner.  Stephenie Meyer has bitten into a genre that I would have said was pretty drained of potential — the “supernatural romance” — and produced what will probably (and rightfully) be seen as its preeminent work.

Vampire fiction has been popular since Bram Stoker’s Dracula, and there’s always been an element of forbidden lusts and sensual temptations involved from the outset.  Modern writers such as Anne Rice and Laurel K. Hamilton have tended to deal with the supernatural primarily as it relates to itself — Rice’s internecine feuds and vampiric politics, Hamilton’s disturbing soft-core forays into werewolf/vampire/necromancer ménage à trois, the secret wars of the Underworld movies and the World of Darkness RPGs, etc.  Humans figure in these works primarily as food, fools, or foils — seldom anything more.

Meyer takes us back to a more Bram Stoker-ish approach in the first novel of this series, Twilight.  Isabella Swan, or “Bella”, as she prefers to be called, has just moved to Forks, Washington, to live with her father Charlie.  She decided to do this, despite the fact that she hates Forks, because her flighty mother has decided to run off and tour with a minor-league baseball player and practical, independent Bella didn’t want to be the one to stand in her way.  She’s dubious and angsty about this decision partly because she’s sure she won’t fit in, partly because it means moving from a big city to a small town, and partly because Forks is one of the rainiest, most perpetually-overcast places in the country.

All this changes, though, when she first visits the lunchroom at Forks High, and encounters the five enigmatic Cullen kids.  They’re movie-star gorgeous, filthy rich, and impossibly aloof.  Aloof, that is, until Bella locks eyes with the unattached Edward, who happens to sit next to her in Biology and who seems simultaneously enraptured with and repelled by Bella in a way that is unique in her (and everyone else’s) experience.  Of course, she falls madly in love.

I’m kind of in a tough spot here; I don’t want to blow any significant plot details, but I also want to review the whole series.  It’s not really a secret that Edward and his family turn out to be vampires, or that his issues with Bella arise from a heady mix of fascination and desire, both for her self and her blood.  The Cullens have to balance their lifestyle and their need for secrecy against Edward’s growing romance with Bella, and the first three books do a good job of exploring the complexities of this relationship as Edward masters himself and Bella learns more about the supernatural world she yearns to join.  Of course, there are other factors that keep this from turning into an unopposed love story, and vampires aren’t the only monsters lurking in the dark…

And then there’s the fourth book, Breaking Dawn.  I found this last book to be by far the best of the four in terms of sheer addictiveness — Meyer has grown a lot as a writer over the five short years it took her to get these books published.  However, it’s the one of the four that leaves the concerns of the human world far behind, so it’s somewhat of a shift from the earlier novels.  And although it weighs in as the longest of the four novels, it probably should have been longer still; she left a lot of loose ends untied and there were some sections of the book that could probably have used more explanation.  Ideally she would have split it into two volumes; there was a really good breakpoint in the middle that would have served well for this purpose.

Of course, she might be holding back on us for possible sequels or spinoffs, as would be her right.

Meyer’s greatest strength, in my opinion, is her excellent use of dialogue and her vivid characterization.  There are a lot of characters in these books, yet they are all distinct, with clear motivations and well-realized personalities.  The supernatural itself doesn’t do the heavy lifting in these stories — the characters’ human (or inhuman) motivations and feelings are the real drivers, which gives these books a subtlety that other supernatural fiction lacks.

She also does a solid job with setting, plot and pacing — there really aren’t any significant weaknesses in her writing, although I wouldn’t put her in the top tier as a stylist.  Her research, on the other hand, has a hole or two — there are some passages about genetics where I think she was confused about the differences between genes and chromosomes, but that’s a very small speedbump in an otherwise excellent novel series.  All in all, I am very sanguine about recommending these books to anyone who enjoys strong character-based fiction, well-realized female protagonists, and/or supernatural novels.

Tags: , ,

Dell’s Windows 7 Runaround

2486053185_e53de18f26[1]OK, so it wasn’t quite as bad as trying to get in touch with Klamm up at the Castle.  But still, there was enough fishy about trying to get my free Windows 7 upgrade for my new computer system that I’m a bit suspicious of Dell’s good intentions.

It seems straightforward.  They have a website.  You go there, input the vital statistics on your recent purchase of a Dell computer.  If your computer meets the straightforward eligibility criteria, you get put on a mailing list to be notified when the Windows 7 release is available.

The eligibility requirements are threefold:

  1. You need to have purchased your computer after June 26, 2009.  I did.
  2. You have to have a supported O/S (Vista Home Premium, which I have, is supported).
  3. Your system has to be on the list of eligible systems.  Mine is.

So no problem, right?  I filled out the form, entered all the relevant information, and…

Bzzzt.  “This system is not eligible for the upgrade program.”

I tried to look over the web app to see if I’d done anything wrong.  Name and email correct, check.  Date of purchase, check.  Dell Service Tag, check.  Resubmit.

Bzzzt.  “This system is not eligible for the upgrade program.”

So, of course, time to call customer support.  After eons on hold trying to reach Customer Care, I call Technical Support.  The very polite tech support call center guy tried to tell me my system might not qualify due to driver issues.

Driver issues?  Vista’s already running on this machine, and you think it’s too new for Windows 7?  He was unable to figure anything out, so he forwarded me to Customer Care (bypassing the long call queue, thankfully).

The next person I spoke to broke all sorts of records in getting me off the line.  As soon as he heard I was calling in reference to the Windows 7 upgrade program, I got transferred immediately.

Luckily, the woman that I ended up at was able to help me.  She checked, confirmed that I had been blocked from receiving the upgrade, took my information, worked some form of database sorcery, and unlocked my account, allowing me to register.

I just question how a system that can easily get all information about my computer and purchase relevant to the upgrade program from its service tag can require manual intervention to unlock registration.  Everyone I talked to who checked confirmed I was eligible within seconds.  If it was that easy, why wouldn’t the main website, which has been up for over a month, also be able to do the same check they did?

I have no proof, but just as a hypothetical scenario, what if certain types of eligible systems (refurbs, for example) were flagged in the web app’s database to deny registration even though they should really qualify?  If the user calls, the proper bit could be flipped easily and blamed on a database glitch.  But not everyone would call.

Again, no proof; it could just have been that my service tag had still been associated with the original purchase date of the computer (before it was returned and repurchased by me).  But it still smacks of the same types of rebate dodges that other manufacturers try to pull.

Anyone else have this same experience?

Tags: , ,

Into the Beta

12778[1]I was one of the lucky 10,000 key recipients from the Gamerzine competition, so I now have beta access to Champions Online.

Unfortunately, due to the NDA I can’t say anything about it, but as soon as it drops and the Open Beta begins I’ll post my impressions here.

The big negative for me is that the beta is now a major temptation threatening to derail me from work on my IFComp game, but so far I’ve been able to minimize the schedule damage and keep the progress rolling.  IFComp has a deadline; Champions will be there indefinitely.

Tags: ,

Type-Safe Message-Passing in Win32/MFC — Update

no[1]In a previous post, I talked about the problem of Windows messaging not working well with smart pointers and type-safe programming practices.  I’ve come up with a fairly good solution for this in MFC, and was interested in writing an article about it, so I sent submission proposals to MSDN Magazine and Dr. Dobb’s Journal.

MSDN sent a rejection letter, as I expected.  MFC, the Win32 message-passing API, and, frankly, C++ in general are very passe technologies compared with their usual topics, so although I had tried my best to spin it as retro-cool, and an example of a practical programming technique for reengineering legacy systems with modern C++ concepts, I still didn’t get any nibble of interest.  I haven’t heard back from Dr. Dobb’s, but I’m expecting the same response (or no response) from them.

In the meantime, I’ve found a few minor holes in my approach, and while none of them are fatal or even really relevant to my original need, and I could probably fix them with enough additional spit-and-polish effort, I have enough other things to do that I’m not really interested in doing so at this point.  With that decision, however, I realize that the sum total of what I’ve done isn’t really up to full print publication-quality, so I’m not going to pursue the magazines any more, and just write it up here and at the Code Project. So expect to see a couple additional articles on my technique in the coming weeks, along with a discussion about what the holes are and what might be able to be done to deal with them.

Stay tuned!

Tags: , , ,

IFComp Update #8

work-in-progress[1]This week I made much more progress than I have in the past few weeks.  My “ban on games” helped me to focus around 3-1/2 hours on writing Scene 2, and I’m to the point now where I believe I can have it done with about 2 more hours of writing.

I’ve finally reached the point with the language where I don’t feel confused by what I’ve just written — where I am controlling the language rather than being controlled by it.  Don’t get me wrong; I’m still stumbling around a lot and taking code straight from the tutorial examples, but I’m doing it intentionally now, for specific purposes that I’m getting to work correctly without extreme amounts of pain.

One of the coolest and most productive things I’ve done is to figure out how to use tables to handle conversational topics.  I’m sure I could write everything out longhand and get it to work just as well.  It might not even take a whole lot of additional typing to make that work.  But there’s something about being able to organize data in a table and refer to it in an orderly way that makes my thought processes cleaner.  It’s back to the same philosophy of making everything as code-like as possible.  Coming from the programming side, keeping code looking like code and text looking like text makes organizing a project like this much easier for me.

What I’ve done is to chain a couple of tables together.  One has a topic column and links to a label value.  The other table has label values along with whatever properties I need to use.  I then define a value with properties based on the second table:

Computer-label is a kind of value. The computer-labels are defined by the Table of Computer Topic Data.

Now I can use the topic column in the first table as a hook for any user input, and link that to the various properties I’d like to associate to that conversational topic in the second column.  This is nowhere near the same level of sophistication of other methods, but it’s surprisingly powerful and works well for me.

So at this point, I have Scene 1 done (barring revisions), the infrastructure for Scene 2 complete, about 1/3 of the content of Scene 2 written, and a big leg up on the rest of the game, as much of what I’ve already done should help with the subsequent scenes.

This is still a lot of work!  The programming issues I may be surmounting, but transforming this mess of Inform 7 into a playable work of interactive fiction with a decent plot and pacing is going to be tough!

Tags: ,

August Dinner Club

42-15516468One of the things Robin and I do as a couple is a monthly dinner club with our friends.  The club got started by a couple — the husband, Garrett, worked with me, and his wife Ginny is a developmental pediatrician.  The club started with kind of an equal distribution of computer geeks and doctors — a surprisingly compatible mix — with a sprinkling of other professions for flavor, and it’s pretty much held steady since then.

We were not original members of the club, but joined up as replacements a couple of years in.  Since then there’s been some turnover, but for the most part people stick with it if they can.

The way it works is that the person or couple who is hosting comes up with a theme and picks recipes for a menu based on that theme.  They take the entrée and everyone else picks the other recipes on a first-come, first-served basis.  Usually the menu selections are pretty advanced — since everyone only prepares one item, we can get fairly elaborate without overwhelming everyone.

Whatever can be done ahead of time is, and the remaining items are prepared or finished at the host’s.  Most of the time, guests bring a bottle of wine to go with the meal, and the combination of good food, good wine, and good friends that you may not have seen in a month does the rest.

This month was slightly different; the theme was appetizer-wine pairings, and so there was no need to bring extra wine, as the hosts had already selected the appropriate ones (we have some true wine experts in dinner club — one of the fringe benefits of membership).  Also, my brother and his family were down for a visit and so he and his wife were able to attend as well.

WARNING!  Pretentious-sounding foodie talk ahead!  WARNING!

It ended up as a complete success!  The appetizer-only format has worked in the past, but this, I think, was the high point.  We started off with sesame shrimp with an Asian-inspired dipping sauce, paired with a very light white wine (Ribeiro).  Next was a Southwest quesadilla with a cumin-lime sour cream topping, paired with a Reisling.  This was excellent!

Next up was my offering, butter-fried sea scallops with truffle oil and chives on parmesan crisps.  A drier white went with this (Zaca Mesa Roussanne).  Following this was a delicious mushroom crostini paired with a Napa Valley red, and some delicious beef skewers with a Napa Valley Cabernet Sauvignon.

For dessert there was some incredibly rich flourless chocolate torte with Madagascar vanilla ice cream, and a nice port.

As I said above, I’m not a wine expert, so having Steve, Michael, and Matt on hand to explain and stage the wine was not only pretty essential, but a great learning experience as well.  My brother Nate and his wife Julie hit it off very quickly with the dinner club crowd, and aside from some regrettable post-meal shenanigans with a digital camera, I think the night was a complete success!  It certainly vindicates the “all appetizer” format as a perfectly viable alternative to a standard multi-course dinner.

Next month:  Tapas at Matt’s?  It’s up to him!

Tags: ,

The Quern is Digg proof thanks to caching by WP Super Cache