A not-so-generic construct

Tricky software engineering problem. I’ll abstract it and explain the situation.

(fyi, a lot of this is just me thinking out loud, I don’t distinguish between type and instance as I think it’s clear from context and I use the term “flavor” to refer to objects which share a common base)

We have a certain object (ObjectA) that may contain an instance of another object (ObjectB). However, the constructor for ObjectB needs information about its “holder” (ObjectA), so the constructor for ObjectB must accept a reference to ObjectA.

Simple enough to use: create ObjectA; create ObjectB passing in ObjectA reference to constructor; call method to assign ObjectB to member of ObjectA.

However, there are many flavors of ObjectB (a plug-in architecture, with ObjectB as an abstract base class), so we have ObjectB1, ObjectB2, ObjectB3, etc.

Still no problem, we just create the flavor of ObjectB. As they are all derived from the same base class, we can pass them around generically (i.e. no problems with assignment to member of ObjectA).

We also want a single function (FunctionY) for creating our instances of ObjectA, that will automatically create and assign any flavor of ObjectB to ObjectA.

Now we run into a problem. Realize that we can’t pass a flavor of ObjectB as an argument of FunctionY (creating an instance requires ObjectA, which doesn’t exist as yet). So, how do we pass a flavor of ObjectB as an argument of FunctionY?

A simple “solution” is to change the problem and instead of passing ObjectA into the constructors of ObjectB flavors, have some sort of method (Init or whatever) that takes an ObjectA reference and can deal with the necessary ObjectA information after our ObjectB flavor has been constructed (i.e. pass ObjectA to ObjectB flavor after ObjectB flavor has been constructed). This works, but the problem it presents is that the ObjectA information may be important (perhaps even critical to construction), and having a lax policy such as this can lead to cases where ObjectB never receives its instance of ObjectA. So, the code becomes more error-prone. However, despite being more error-prone, this may work in a lot of cases as long as you can wrangle away the ObjectA-specific code from the constructor.

Of course, if we resolve to change the problem, we can also get away with having multiple functions. Or mangle things in any which way we want to get the square peg to fit the circular hole. However, I assumed I had a legitimate problem and decided to look for a solution.

Another simple but, this time, real solution is to create an ObjectB factory (woot! design pattern!) and pass some sort of representation (e.g. enum) for the flavor of ObjectB we want. This representation is passed to FunctionY, which will query the factory and get an instance of the flavor of ObjectB we want. This will work, but I don’t think it’s an elegant solution. We’re creating another construct to facilitate something which should be trivial (in my opinion, at least). We’re also increasing our maintenance burden; every time we create another flavor of ObjectB we need to update the factory and update the list of flavor representations.

I thought about this for a while and realized the central issue here was how to pass type information between functions.

So, I tried a solution involving reflection (this was in C#). Reflection works, as you can dynamically pass the type and dynamically invoke a constructor for the type. However, I don’t particularly like the reflection solution. There’s, of course, the high performance cost, but also the solution was too generic as your making assumptions about what parameters the constructor of a type will take and, as as it’s all dynamic, there’s no compile-time type checking. However, that being said, the bottom line is that a reflection solution works.

Hunting for another solution, I decided to experiment with generics. The idea was to create a builder class that took the flavor of ObjectB we wanted as its generic-type parameter. There would be a public method, BuildIt(…), which would take an instance of ObjectA and would simply instantiate the flavor of ObjectB we wanted using the passed-in parameter as its constructor argument. This would work, expect for the fact that generics don’t allow you to instantiate an object of the generic-type with constructor arguments. I quickly discovered the…

Cannot create an instance of the variable type ‘T’ because it does not have the new() constraint
error message spit out by the compiler. Damn.

If this was C++, templates should free us from this problem, but porting all the code over to another language isn’t exactly a feasible solution, to say the least.

In the end, I’m just kind of bummed out that I could find a solution I was really happy with. I’m sticking with the reflection solution, as this isn’t performance-critical code and I simply have to move on to other stuff.

Finally, looking at things from a wider perspective, just two things that seem weird. First, why can’t types be passed around and used, but resolved statically (by a compiler or a pre-processor). Like how #define is used in C I guess. Also, languages with generics/templates peg themselves as object-oriented, but when dealing with types in a generic fashion they’re completely agnostic to any form of inheritance and, as such, the object’s class hierarchy.


Facebook security issue and the voyeurism of the AP

Report from the AP on Facebook security lapse.

“A security lapse made it possible for unwelcome strangers to peruse personal photos posted on Facebook Inc.’s popular online hangout”

ok, these security issues aren’t exactly uncommon on the internet and this one seems mild in it’s effect as: (a) it’s been fixed and (b) despite the assertion of “unwelcome strangers” perusing one’s photos, there’s only confirmation that 2 people exploited the security hole, Byron Ng the one who found it and an unnamed AP reporter (and perhaps a few others).

The extent to which Ng exploited the security lapse is unknown (although, we do know he look up private photos Paris Hilton at the Emmy awards and of her brother Barron Nicholas Hilton drinking a beer with friends – scandalous!) and in any event he should be credited with exposing the exploit (although it seems to be a simple URL edit, and he just raked it a ton of publicity).

The actions of the AP reporter on the other hand,

Using Ng’s template, an AP reporter was able to look up random people on Facebook and see the most recent pictures posted on their personal profiles even if the photos were supposed to be invisible to strangers.

The AP also was able to click through a personal photo album that Facebook co-founder Mark Zuckerberg posted in November 2005.

… is a bit unethical. There are other ways to verify a security hole. You could easily create dummy accounts or un-friend a few people to have some test accounts to verify the security issue. Sure, looking up random people (and Zuckerberg) and viewing their photos works too, but you’d think a member of the press would have a bit more integrity.

Finally, CNN’s “Story Highlights” seem to be written by someone who doesn’t like conjunctions, leading to fun implications,

Technician could access private photos of Paris Hilton, site co-founder


The peculiarities of Apple

Interesting article from Wired. Nothing mind-blowing or really unique, but a good overview of Apple and it’s uniqueness compared to other tech companies.

Perhaps, what’s more interesting is some of the comments against the article and author, Leander Kahney. The most scathing of which is probably here, where Kahney is deemed an “irredeemable jackass” by one, John Gruber. Unfortunately, some of the comments seem to be nitpicking and simply don’t hold up to scrutiny.

Is Apple “evil”, according to Google’s “don’t be evil” mantra? Well, Google doesn’t really have a definition for evil, and as such it’s such a flexible term that one could certainly say that closed platforms, authoritarian control, and less than stellar treatment of employees qualify as evil.

On open platforms, true neither Apple or Google are dogmatic, but the point is that Apple is unusually closed for what’s perceived by many as a very modern, forward-thinking, tech company, and closed-platforms are usually the default. Not initially opening the iPhone and iPod touch to developers (because of I believe security concerns?!), and even now with the very managed licensing process demonstrates this attitude. Google, on the other hand, does release APIs for a lot of their products (Google Desktop, Google Maps, etc.).

The whole contrast with Google isn’t that great, but it seems to be stretched out by Gruber more so than Kahey.

On secrecy, every company keeps a certain degree of secrecy, but the point is that Apple is unusually secretive about their products, as the case against Think Secret demonstrates, a site that most other companies would look at as a fan base hub and free advertising (hell, sony, payed to create a fictional fan-boy blogger evangelizing their product as a form of advertising). The mystery isn’t so much why apple took an adversarial position against Think Secret to protect its secrecy, but why Apple’s secrecy is worth so much, when secrecy seems to be worth far less for most other companies.

Overall, I didn’t find the article attempted to demonize Apple or that the author was trying to sell us his version of an ideal corporate entity. After all, Apple is very successful doing what it does and has positioned itself and one of the most innovative companies around. However, it is the perplexing case of an innovative and successful company, existing within a progressive/egalitarian ecosystem, that excels employing very non-progressive/non-egalitarian constructs.


fragment space launch, firesync 2, and new portfolio

New stuff:

  • fragment space was launched a while back (at the beginning of the new year). It feels great to finally have something up and get rid of the “coming soon” message. Overall, I think the site turned nicely. A fair amount of graphics and a little fade-in/fade-out fx, but I don’t think it overwhelms or distracts from the content. I initially was going to have a drop-down menu for projects, but as there is only 1 definite project that would be available for download (firesync) and everything else was up in the air, I opted instead to just have a tab for firesync and just keep the site simple and leave the other stuff out until more concrete info or a download was available for the other stuff. One addition to the site that I think would be cool would be a commenting system on project pages, but so far I’m still searching for solutions.

  • firesync 2 is done and further info and download links are on the fragment space site.

  • I’ve updated/redesigned my portfolio & resume, both of which are available at http://aautar.digital-radiation.com/portfolio. I’ll probably be making some minor changes to polish things up and fix a few bugs (e.g. resume does not display correctly in Safari), but overall I really like how it turned out and I think it has a great look and feel: very simple (the layout is 99% text-based, most images are related to content) with an intuitive interface that does a good job of filtering information into digestible units. I think it’s a huge improvement over the previous page, which just spat out a long list of everything and provided navigation controls that simply scrolled you down to a section in the page. Also, the buggy, dynamic XML loading is gone so the site now works in Safari and popups have been eliminated as lightboxes are used to display images.

  • Update/redesign for http://aautar.digital-radiation.com; what I’m calling the hub page. The design and layout is kinda cool, but I’m not totally happy with how this came out. I don’t think the design really reflects the page’s position as a hub connecting to other sites/pages and doesn’t provide information about these other pages/sites quickly enough to the end-user. A visual preview of these external pages/sites might have also been a good idea. I’ll probably be redoing it soon.


Demo time

Call of Duty 4
I really wasn’t expecting anything special. The previous Call of Duty games, while good, didn’t really impress me – I’m not terribly crazy about military shooters and especially WWII-era ones. As CoD 4 got a lot of good reviews and finally broke away from WWII, I decided to give it a try, and was totally impressed. The level of polish and attention to detail is brilliant, even for the non-game elements (the presentation of the satellite map with the news anchor voice over intertwined with the music is particularly impressive). The gameplay derives from the FPS formula, but the frantic pace of the action and the nail-bitingly tense atmosphere it creates is novel and provides for an amazing experience.

Supreme Commander
This has been on my hard drive for a while and I never got around to it. As it’s the spiritual successor to Total Annihilation and given that I was a big TA fan, I had high expectations. However, I just haven’t been able to get into it. I’ve played a bit of the campaign and 2 or 3 skirmish games but the game is simply not resonating with me. Perhaps it’s a matter of scale; zooming in and out of the battlefield isn’t as impressive as I thought it would be, it is functional for setting up long range attacks, especially with air units, but I still find myself scrolling a lot. Although, this might just be due to habit more than anything else. In addition, the “default” zoom level I usually fall into is usually pretty far out, and the units simply don’t look too impressive at this scale.

Units also seem fragile. The game seems to force you to always attack in large groups, and this would be in line with it’s attempt at epic battles, however, building units is time consuming and considerable micromanagement needs to be done to setup or reinforce squads as even the weakest units are spawned individually. When you execute a strategy and it works, the game is fun, but the setup to that point can be a burden and a failed strategy renews the burden.

From what I’ve seen so far, I also haven’t been impressed with the maps themselves. There are obvious choke points that provide for basic defense setups and seems to avoid rushes but most of the terrain is flat, so at times strategy can boil down to just having the largest, most advanced squads with a good variety of units to counter artillery and aircrafts. A flat terrain also means there’s usually a huge burden for defense placed on you as there is little to no benefit from the terrain.

Overall, it seems like an ok game, but it simply hasn’t impressed me the way other RTS games have. I’ll put it some more time with the demo, but I’m not optimistic about finding anything that’ll win me over.

Aquaria
From time to time I check out GameTunnel and I’m usually disappointed with what I find. This time was different, I stumbled across Aquaria and it’s a wonderful game. The graphics and artwork were the first to strike me, but the music, sound fx, and voice overs are all increadibly well done. The game does take some time to get into, especially early on where you’re likely to be swimming around just trying to figure out where you should go (as you have a large area to swim in at this point, but most of the passages to the other levels are blocked off), but so far it’s been a charming, fun ride.


That time of the year

As the end of the year is upon us, all the gaming publications are pushing their “top whatever of the year” or “top X games of the year” bullshit. Next Generation seems to have taken it to an extreme:

Top 10 Blunders of 2007
Top 25 People of 2007
2007’s 50 Biggest Events
Best 30 Games of 2007
Top 10 Game Design Innovations of 2007
2007 Holiday Buyer’s Guide
Japan’s Best Games of 2007

… and finally to solidify the greatness of 2007:
2007: Best. Year. Ever?

I don’t know, maybe I’m just cynical. Maybe these lists do have a purpose, fulfilling some sort innate human desire for yearly enumerations.


The Initial User Experience: Dashboard vs. Yahoo! Widgets

I recently got a Mac Mini and begun playing around in OS X. One of the first things I stumbled upon is Dashboard, which brings up an overlay displaying widgets, small apps that display things like weather, time, calendar, etc. I thought this was incredibly cool (especially because of the weather widget, as I hate going to weather.com and having to type in my zip code every time I want to know what’s happening outside). As a feature it’s very well designed; it looks great, it’s easy to get to, it didn’t pollute my desktop in any way, the information was useful and informative, and in less than a minute I implicitly understood how it worked.

I figured there must be something similar for Windows, if not I was even thinking it might be a fun project to make something similar. I did try Google Desktop a while back, but the experience was not the same, and was definitely not pleasant. I didn’t like that it was essentially a panel (a large one at that) pasted into a corner of my desktop. I like my desktop. I want my stuff on it. I don’t want to cede the real estate to a third-party. In the end, I uninstalled Google Desktop, and was left with an overall negative impression of the product. I haven’t used Vista as much, but the Vista sidebar seems to make the same mistake.

After some searching I found Yahoo! Widgets. It seemed perfect. However, after I installed it, I was disappointed, it looked cool, but it didn’t seem to function like Dashboard at all. After some twiddling I did finally massage it into working akin to Dashboard; however, I been less patient or not as tech savy, I would have quickly uninstalled it and deemed by discovery worthless.

So, what’s the point? The point is pretty simple (and I don’t think I’m breaking new ground here): the initial experience a user has with a piece of software is vitally important and if the developer fails to provide a positive experience to the end-user after setup, the software is a failure (users will simply uninstall it), no matter how cool the interface is, the potential productivity gain, or the hundreds of cool features. My experience with Yahoo! Widgets and Dashboard exemplify this perfectly.

So what’s wrong with Yahoo! Widgets? Technically, I don’t think anything is wrong with it. It seems stable, powerful, has nice features, and the widgets seem cool (personally, I really only cared about the weather and a calendar). However, after installation, it stuck this dock panel on my desktop along with a bunch of widgets. Now, as I already mentioned, I hate things assuming they have the right to pollute my desktop. Ownership is important. So I was not a fan of the dock nor the widgets themselves, which, unlike the dashboard widgets, didn’t fade in and out with an overlay. This was annoying. I wanted my desktop back.

So I went to preferences and found out I could press F8 to active the overlay. Great! (however, I’m going into the Preferences window to actually figure this out – why wasn’t this mentioned sooner). The overlay shows up, kinda disappointed that the taskbar isn’t covered, but I can live with that. I deactive the overlay. damn! the dock and widgets are still stuck on my desktop.

I play around in the Preference window for a while and look at the options for the dock. I discover I can get rid of the dock, that’s good, but I’m thinking this isn’t for me, and about ready to uninstall.

I continue playing around. With the overlay off, I close all the widgets. I accidentally hide one of them. I hit F8. Wait! That’s the effect I wanted! But what did I do? I realize that there are 2 options to get rid of a widget, Close and Hide. Close gets rid of the widget altogether and Hide only show the widget when the overlay is active. Why didn’t I know this, like the F8 key, this was never explained to me (remember me, the end-user, the one using the product). It’s not in the product tour, there’s no tooltip, and I certainly didn’t spot anything in the help.

I’m happy now! I customize the system to show the widgets I want and I hit F8 whenever I want to see them 🙂

So that’s the story. Ultimately, from my experience with both applications, I learned:

  • The desktop belongs to the user, don’t pollute it unless they allow you to. (this might be more of a personal preference than anything else)
  • Present important app-specific information ASAP and make things as intuitive as possible. Avoid having users discover features by reading documentation or going into a settings/preferences window.
  • The initial user experience is important. Don’t fuck it up.
yahoo widgets

I do realize there is a bias here as I was looking for something that functioned like Dashboard, but if the default post-setup environment for Yahoo! Widgets is how the developers see others using their application, that seems totally weird to me because it’s assuming a whole lot of users have so much free space on their desktop that they’d obviously want to stick a bunch of widgets on it. Hmm, I don’t know, maybe I’m wrong and most users do have a lot of free space on their desktop, but my desktop certainly doesn’t fit into this category, and even if it did, I’m not sure I’d want a widget on it (I like my wallpaper).


Fallout Tactics and Cockroaches

I’ve finally gotten around to playing Fallout Tactics, which is a game I’ve wanted to play for a really long time. I played the demo when it first came out and instantly loved it, but never bought it (couldn’t afford it) and then for a long time I simply forgot about it or couldn’t find a copy. I discovered the Fallout Collection selling a few months ago which includes Fallout, Fallout 2, and Fallout Tactics and snatched up a copy right away.

It’s an amazing game, I love the Fallout universe and the Fallout-style post-apocalyptic atmosphere. I’m also a big fan of the squad-based tactical combat, and I really wish there were more games that focused on this style of gameplay. The only similar game I can think of is a not-so-popular game called Shadow Company: Left for Dead, I played the demo which was pretty decent and fun, but I didn’t find it incredibly compelling or memorable.

Despite my overall love of FT, I absolutely hated the cockroaches level. Why the hell would you make a level where all you do is kill roaches…

ft roaches

It just seems like total filler, there’s absolutely no substance to this level. It doesn’t really tie in to the overall plot, the combat isn’t fun, your being poisoned constantly, and it sucks being killed by a damn cockroach. You also waste a ton of ammo (a precious resource at the early stages of the game) and you don’t get anything from looting the bodies (well, yea, they’re roaches).

Oh, and at the end of the level, there’s a boss, a really big, master roach *rolls eyes*.


Success Depends on Others Failing

I came across this article today. It’s somewhat interesting overall, but there are 2 things which particularly struck me, first, the following paragraph…

The researchers were especially interested in the set of outcomes where both players answered correctly. For any given prize value, the brain’s reward response was bigger if the other player earned less. Players on average were more pleased with a 60 euro prize when the other player got just 30 euros, for example, than they were if both players earned 60 euros, or if the other player got more.

I can only guess what my brain scan would show, but my gut feeling is that I’d likely feel empathy for the person being rewarded less and/or reduced satisfaction (or no satisfaction) due to the fact that my reward is inflated. However, I could definitely understand the negative reaction if I were the person being rewarded less and the satisfaction for being rewarded more for doing more.

Also, this seems really sad to me. It would indicate that humans are, in general, inherently selfish as we have no problem seeing others rewarded less for doing the same work.

(I hate that the article somewhat convolutes the issue, by casually making a segway to the “keeping up with the Joneses” concept; there’s a fundamental difference in keeping up and keeping up unfairly)


Win32 annoyances and tips when working with files and processes

1. ShellExecuteEx and lpFile

SHELLEXECUTEINFOW setupxInfo;
memset(&setupxInfo, 0,
sizeof(SHELLEXECUTEINFO));
setupxInfo.cbSize =
sizeof(SHELLEXECUTEINFO);
setupxInfo.fMask = SEE_MASK_FLAG_NO_UI |
SEE_MASK_NOCLOSEPROCESS;
setupxInfo.lpVerb = L
"open";
setupxInfo.lpFile = L
"setupx.exe"; // can't be full path
setupxInfo.nShow = SW_SHOWNORMAL;
setupxInfo.lpDirectory = L
"setupdir";
setupxInfo.lpParameters = NULL;

ShellExecuteExW(&setupxInfo);

The code above is correct and it works, however if the lpFile member of the SHELLEXECUTEINFO struct is the full path to the executable file the ShellExecuteEx call fails. It seem you must specify the working directory for the child process (lpDirectory) and the filename only for the lpFile member.

2. SHFileOperation and pFrom

SHFILEOPSTRUCTW SHFileOp;
ZeroMemory(&SHFileOp,
sizeof(SHFILEOPSTRUCT));
SHFileOp.hwnd = NULL;
SHFileOp.wFunc = FO_DELETE;
SHFileOp.pFrom = killFolder;
// double-null-terminated string, can't end with path separator
SHFileOp.pTo = NULL;
SHFileOp.fFlags = FOF_SILENT |
FOF_NOCONFIRMATION |
FOF_NOERRORUI |
FOF_NOCONFIRMMKDIR;
SHFileOp.fAnyOperationsAborted = FALSE;
SHFileOp.lpszProgressTitle = NULL;
SHFileOp.hNameMappings = NULL;
SHFileOperationW(&SHFileOp);

In the above code, SHFileOp.pFrom (killFolder variable) is a double-null-terminated string to a folder I want to delete (I want to also recursively delete everything inside the folder, that’s why I’m using SHFileOperation). However, aside from the peculiar double null-termination, the path can’t end with the path separator char or the SHFileOperation call fails.

3. WaitForSingleObject

After calling ShellExecuteEx or CreateProcess you can do the following to wait for the child process to terminate:

WaitForSingleObject( setupxInfo.hProcess, INFINITE );

4. fclose

Don’t forget to close your file handles. I spent a few hours today trying to figure out why I couldn’t delete an exe, thinking it was an issue with the ShellExecuteEx function. Turns out I opened the file and forgot to close it.

btw, Process Explorer is very helpful and a great tool for programmers; it’s wonderful for detecting things like unclosed file handles.