No, I didn’t find these pictures in a bar, ok?
Category: Development
Debugging with Seaside
James Robertson: “People underestimate the importance of this a lot. In fact, you can find plenty of developers (including Rubyists) who will tell you that you shouldn’t debug at all; tests will do it all for you. What that really means is this: debuggers in other languages are very, very different from what we have in Smalltalk, and when you get into Seaside, it’s even more cool”
This is one of the things that bugs me about most web development. You don’t have a great debugger and folks that say you don’t need one are kidding themselves. Debuggers are great for more than actual debugging of code. You can use them to figure out the flow of your code, stepping through it to force code paths, and the like.
If I were to become a full time web developer Smalltalk would be on a very short list of choices because its’ maturity.
C++ to Objective-C
About year ago I started looking at writing my first iPhone application. My brother and I had decided we’d finally go ahead and create an application we’d had many fits and starts on but had never finished. The code started its’ life on Palm and Windows. Back when we started I’d written the math code in portable C++ so we could use it on many different platforms.
Around Thanksgiving I’d put together a prototype of our iPhone application using that C++ code. I started by creating an iPhone Cocoa View Based Application, and added my existing C++ classes to it. That was pretty simple, now to compile the code. This is where I ran into my first problem.
Say What?
The Objective-C compiler didn’t like having a mix of C++ and Objective-C. Oh, I thought that was allowed? It is. You simply need to name your source files with a .M or .mm extension instead of the standard .m for Objective-C. That was easy.
Below is an example C++ class used in an Objective-C class. The PKMath namespace is where the C++ types and classes live. PKMath::VancoBolusDose() just worked. I verified that by viewing the instance vancoBolusDose in the debugger, so I knew we were good to go with a mix of C++ and Objective-C.
- (IBAction)calculateNewHandler:(id)sender { double height = heightTextField.text.doubleValue; double weight = weightTextField.text.doubleValue; double age = ageTextField.text.doubleValue; double scr = scrTextField.text.doubleValue; double peak = peakTextField.text.doubleValue; double trough = troughTextField.text.doubleValue; PKMath::Gender gender = (0 == genderSegment.selectedSegmentIndex) ? PKMath::kMale : PKMath::kFemale;
// Create an instance of a C++ VancoBolusDose. PKMath::VancoBolusDose* vancoBolusDose = new PKMath::VancoBolusDose(weight, height, age, scr, peak, trough, gender);
// Do stuff with dose here. delete vancoBolusDose; vancoBolusDose = NULL; }
So, along comes December, and I haven’t really done much with the iPhone app. I was thinking about it, but not writing much code. I wanted to mess with Objective-C more, try some things out. I decided I’d port the PKMath classes from C++ to Objective-C. It took very little time and I had the beginnings of the PKMath library used in RxCalc 1.0.
Revisiting the code example above, this is what it looked like after creating the new Objective-C version.
- (IBAction)calculateNewHandler:(id)sender { double height = heightTextField.text.doubleValue; double weight = weightTextField.text.doubleValue; double age = ageTextField.text.doubleValue; double scr = scrTextField.text.doubleValue; double peak = peakTextField.text.doubleValue; double trough = troughTextField.text.doubleValue; PKGender gender = (PKGender)genderSegment.selectedSegmentIndex;
// Create an instance of an Objective-C VancoBolusDose. VancoBolusDose* vancoBolusDose = [[VancoBolusDose alloc] initWith:weight :height :age :scr :peak :trough :gender];
// Do stuff with dose here. [vancoBolusDose release]; }
Apple Core Labs first product, RxCalc, was accepted in the iTunes App Store on July 4, 2009. What a great day!
If you’re a Clinical Pharmacist, and perform Pharmacokinetics calculations on a daily basis you can purchase RxCalc for the very low price of $0.99.
Jobs on Flash
Apple: “Symantec recently highlighted Flash for having one of the worst security records in 2009. We also know first hand that Flash is the number one reason Macs crash. We have been working with Adobe to fix these problems, but they have persisted for several years now. We don’t want to reduce the reliability and security of our iPhones, iPods and iPads by adding Flash.
In addition, Flash has not performed well on mobile devices. We have routinely asked Adobe to show us Flash performing well on a mobile device, any mobile device, for a few years now. We have never seen it. Adobe publicly said that Flash would ship on a smartphone in early 2009, then the second half of 2009, then the first half of 2010, and now they say the second half of 2010. We think it will eventually ship, but we’re glad we didn’t hold our breath. Who knows how it will perform?” – Now you know.
Development as “Show Business”
Brent Simmons: “I can’t remember when it first dawned on me that app development is a form of show business.” – When I read this I immediately thought of Ted Johnson. Ted was a co-founder and CTO of Visio. He likened software development to movie production, thus the nifty little easter egg in Visio 2.0. The only one we ever really had.
Meet the Visio 2.0 team
To see this lovely “ego” screen you’d hold down both mouse buttons as the Visio 2.0 splash screen was showing. And, yes, I still believe Visio was the best company I’ve ever worked for.
No iPad for testing?
Scobleizer: “Even the app developers never had their hands on iPads (I talked with several developers, even at “hot†companies like Evernote, while waiting in line, and they had to develop their apps without even seeing an iPad) so the marketplace couldn’t tell them before it shipped just how hot this would be.” – What company would do that? Yep, only Apple would feel confident enough in their simulator to not get beta units to developers. I don’t know that I’ve ever heard of a company doing this. I’d love to know how many applications that appeared on day one required bug fixes right away? Does anyone have any data on this?
Will 4.0 ban “cross-compiling?”
John Gruber: “My reading of this new language is that cross-compilers, such as the Flash-to-iPhone compiler in Adobe’s upcoming Flash Professional CS5 release, are prohibited. This also bans apps compiled using MonoTouch and Unity3D — tools that compiles C# and .NET apps to the iPhone. It’s unclear what this means for tools like [Appcelerator] and [PhoneGap], but it sounds to me like they’re on the wrong side of this new rule, and the folks behind Appcelerator already realize they might be out of bounds.” – I’m not exactly sure how things like the Flash to iPhone App conversion stuff works but if they’re running running Flash code through something that generates Objective-C code, which is then compiled, is that breaking the rules?
Food for thought.
Yeah, what he said
Marco Arment: “But the biggest reason why there’s no iPhone SDK on Windows or Linux is that it doesn’t need to exist. The iPhone is the premier platform where the most money is being made. Developers will come to Apple — Apple doesn’t need to come to developers. (Google does, as the underdog.) It’s the same reason why there’s no OS X or Linux port of Microsoft Visual Studio, and you don’t see a lot of Mac owners yelling at Microsoft for not porting its sophisticated development environment to their chosen operating system.” – I really like this Marco guy. He’s about as blunt as can be, but he makes fine points. He’s also the creator of Instapaper, an application that’s quickly becoming my favorite little utility.
Why derive a C++ class from a struct?
This question came up on Twitter yesterday, and got me thinking. Yeah, why would you do that? The only answer that sprang to mind was for compatibility reasons. If you have the luxury of working on a modern codebase you may not do this sort of thing, but if you have a legacy application or are working with a legacy API you may have need to call C functions that take structs and you may want to manipulate those structs in your C++ code, but do it in a C++ way. Make sense?
Here’s what I’m talking about. Take this C struct for example.
typedef struct tagABC { int a; long b; float c; } ABC;
Back in the day, when the ABC structure was created, the developer also created a function to operate on this structure, something like this…
float OldSchoolCDoFancyCalculation(ABC* abc) { return ((float)abc->a+(float)abc->b+abc->c); }
That looks pretty darned straight forward. The OldSchoolCDoFancyCalculation() function takes a pointer to an ABC struct, sums the field values, and returns the result. Not uncommon. Here’s how it could be used.
// Old school C style. ABC abc; abc.a = 10; abc.b = 99; abc.c = 99.99; float value = OldSchoolCDoFancyCalculation(&abc); printf("Old School C Struct + C Function == %f\n", value);
Moving on
Let’s say your team has decided to embrace C++ but you don’t want to go back and rewrite a bunch of code, and moving forward you’d like to make use of the nice stuff C++ has to offer. Someone may choose to do something like this with your ABC struct. I say may choose because they may do something totally different, this was the only real example I could think of, so it fits for illustration purposes.
class ABCObject : public ABC { public: ABCObject(int aa, long bb, float cc); virtual ~ABCObject(); void DoFancyCalculation(); void PrintValue(); private: float _value; }; // ABCObject
Take note. ABCObject is derived from the struct ABC. Yes, of course you can do this in C++. When C++ was being defined this was one of those things that had to be addressed for backward compatibility with C. By default all struct members/fields have public access.
This will allow you to mix instances of ABCObject with the old C functions that operate on ABC structs. Here’s how that would look, and it’s perfectly legitimate.
// New school C++ style, using old school C code, no problem. ABCObject abcObj(11, 100, 100.09); float value2 = OldSchoolCDoFancyCalculation(&abcObj); printf("New School C++ Object + C Function == %f\n", value2);
Notice we’re passing a pointer an instance of an ABCObject. What the? Sure, you can do that. It’s perfectly legal because ABCObject is derived from the ABC struct. The compiler would notice the function takes an argument of ABC* and ABCObject just happens to fit the “is-a” rule, so it can be passed as an ABC* to the function where it can be operated on like a standard ABC struct pointer.
Like I said, someone could do that. You probably wouldn’t, but the point is you could, and some folks will definitely have need to write code that looks similar to this for some very good reason. More often than not most developers would probably choose to wrap an instance of the old ABC struct as a member variable of the class and then just operate on that from within class methods.
Mac Bits for Sale?
Matt Legend Gemmell: “I want to solicit some feedback on the idea of selling source code for the iPad/iPhone and/or Mac platforms. It’s something that’s commonplace (and popular) on other platforms like .NET and Java, but for whatever reason it’s never taken off on the Mac. I think that there’s potentially a reasonable market, particularly for iPhone/iPad, and I’d be interested in your thoughts.” – It’s odd there isn’t more of this in the Mac world. There’s definitely a whole lot of free, very well designed, Mac and iPhone source code floating around out there. Matt’s very own MGTwitterEngine or Gus Muller’s FMDB to name a couple.
I, for one, would welcome high-quality, supported, fully baked component bits for the Mac, iPhone, and iPad. It’ll make my life easier and allow me to focus on creating apps by not having to create as many foundation pieces.