I haven’t posted in forever. Why?
I’ve been working on a little game called Star Wars: The Old Republic!
If you want to play early, preorder now.
There I was, implementing a factory in Objective-C. My C++ instincts told me I would either need to create an enumeration, a template, or some sort of Factory inheritance chain. My C# intuition pointed me in the direction of Reflection. Reflection was pretty slick, so I opted to look down that path. You know what? There was a bright light at the end of that tunnel.
Now, what I found wasn’t exactly reflection, but it was certainly a useful tool to accomplish what I needed to do. What’s that magic?
1 | [SomeClass class]; |
That’s right, there is actually an object called Class. Every single object implicitly has a member of type Class, the isa pointer. This member tells you to what type your object points. You may have noticed this while debugging in XCode. Every time you mouse over a variable you get that lovely isa pointer. Class is a strong type of which you can declare instances, pass to functions, and plenty more. You can use this for numerous things, the most obvious among them being run-time type checking. Is my BaseVehicle object ACTUALLY a Car or is it a Plane? Grab that class and compare like so:
1 2 3 4 5 | Class realClass = [someVehiclePointer class]; if ([realClass isKindOfClass:[Car class]]) { // Do something specific to Cars. } |
Maybe, you want to know more than whether you’re a Car. You have a Sedan or a SportsCar class derived from Car and you even have Civic, Camry, Charger, and Lamborghini classes derived from those types. Maybe you only want SportsCar derived classes.
1 2 3 4 | if ([realClass isSubclassOfClass:[SportsCar class]]) { // Get ready for the races! } |
Now, what if you wanted to test for exact matches only? What if you wanted to put your foot down and make sure that there is no inheritance masquerading, and that your object MUST be a Lamborghini in order to respond to the OpenButterflyDoors message. There is a message for that as well!
1 2 3 4 | if ([realClass isMemberOfClass:[Lamborghini class]]) { [someVehiclePointer OpenButterflyDoors]; } |
Hurrah! As you can see, RTTI is entirely plausible with Objective-C. Now, forgive me for not running performance analysis on the costs of this. I imagine the dynamic checks, isKindOfClass and isSubclassOfClass aren’t particularly cheap while isMemberOfClass is a simpler comparison. That’s just my intuition talking again, though. I really should get that thing checked out. (Or run performance tests soon.)
Finally, the last cool thing I would like to show you, is that you can use an instance of Class as a type. (Huh?) Meaning whatever you can do by typing the actual class name, it is possible to do with an instance of Class. (Well, except forward declaration, I think.) For example, if I had a VehicleFactory, I could implement a method in the following way:
1 2 3 4 5 6 7 8 9 10 11 12 | - (Vehicle*) createGameObjectOfClass:(Class)type Error:(NSError**)error { Vehicle* result = nil; if ([type isSubclassOfClass:[Vehicle class]]) { result = [[type alloc] init]; [m_objects addObject:result]; } return result; } |
Hello, Factory! Hello, decoupling! Hello, Class! But wait, there’s more! Is it necessary to check if you subclass something? Maybe my factory follows a different contract. By convention, people usually think that you need to be of a certain subclass to have any given collection of functions. Not true. You don’t have to derive from a Car in order to accelerate and decelerate. In fact, a Person does the same thing. If I only care about subclassing because I don’t want to add objects that can’t respond to that message there is a more accurate check!
1 2 3 4 | if ([vehicle respondsToSelector:@selector(accelerate)]) { // Make the thing go vroom! } |
Armed with this awesome new knowledge, in the eternal words of the announcer and theme song for a popular Nickelodeon show, “What would you do?”
You may recall a post I made on the blog before about how to break into the industry. The most important advice there and the most important advice again is to Know Your Stuff.
I can’t stress this enough. In many forms you will be asked about The Stuff and in every answer you need to demonstrate that you Know The Stuff. What do you need to know?
The Job
We turned down a constituent of interviewees recently because they were clearly not a fit for the position nor did they even know the position. I want someone who is excited. I want someone who will excel and someone who will enjoy it. The job posting lets you know what languages we want you to know, what kind of person we are looking for, and what you will do. It would behoove you to be intimately familiar with that going into an interview, as the questions will be geared in that direction. We will ask about the techniques and requirements listed. Surprise should not be the initial reaction.
The Language (Usually)
You must be able to field technical questions for any language that you’re supposed to know. What languages are you supposed to know? Anything you listed on your resume without indicating you have only dabbled with in coding. Any language that the job listing contains under the Required section. Oh, and most game companies use C++. Sorry, but you probably need to know that one.
The interview is used to test the candidates knowledge not only of specifics, but also about how the language works. It’s certainly refreshing to know that you know how to write code with a pointer syntactically, but why would you use a pointer? Why do you need to put virtual in front of that destructor? Why do you encounter the inheritance Diamond Problem? You will really shine by knowing your language of choice intimately and being humble about when you do not. “I have a book,” is not a valid answer. Yes, that’s a real world example.
How To Code (Or At Least How to Think Well Out Loud)
You’re going to get a whiteboard problem. It is going to be a simple, clever problem with perhaps three or four reasonable solutions. What’s important is showing that you have the technical skills and mindset to produce code. What’s not important is to write the perfect solution on the first crack. Everyone is a snowflake and all that jazz. Besides, the interviewers will lead you to it with pointed questions such as, “Are you sure recursion is the best solution in this case?” That is why I echo the advice to think out loud. If you sit in front of a whiteboard and scratch your head for a half hour, you’ve proven nothing, given us no insight as to how well you would do. If you at least let us in on the thought process, we know you are heading in the right direction.
Algorithms, Data Structures, and OOP! (Oh my!)
I have been to a rare few interviews where I was not asked about data structures and object oriented programming. They are the exception to the rule. Those few times were because the interview was either short or because we went off on a tangent about pet peeves, code philosophy, or who knows what. If you’ve searched for interview questions on Google then you know most places recommend knowing: the difference between linked lists and vectors, big O notation, and implementations of stacks an queues.
Here are some homework assignments. How would you architect Pac-Man these days? How about poker? What data structure would store the cards?
Thems the basics. What else would I brush up on personally? How to be charming, endearing, and personable! Good class design. Design Patterns and Anti-Patterns, even if only for a common lexicon. Read some good books! Whatever you do, Know Your Stuff!
I made a post to start out the New Year laying out my personal improvement plan when it comes to programming. (Trust me, there are personal resolutions too! This just isn’t the time / place.) I laid two measly goals. Here’s how I did.
Well, at not a complete fail here. According to my Kindle I am 57% through Effective C++, 3rd Edition. At that rate, I would barely get halfway through this one. Guess it’s time to pick up the pace. It would probably help if I would focus on this book instead of spending the time to read The Elements of Style or The Ultimate Hitchhiker’s Guide.
This one actually is a complete failure. I have not done anything on this front. I have been spending a lot of time working on work stuff and hardly any “me” time. When I do get the chance to relax I have been playing our game, so it’s borderline still working! (CAN YOU BELIEVE I GET PAID TO MAKE THIS!?)
I really need to dedicate some time to this. Simple as that. I have been swamped at work and haven’t really sat down and cranked on this. It is long overdue.