What we learned from rewriting our robotic control software in Swift
sunsetlakesoftware.com
sunsetlakesoftware.com
"We use Macs to drive these robots, with control software that was written in Objective-C. We made the decision to rewrite this software in Swift late last year, but only after a careful survey of the bugs that had impacted our customers over the years. That survey revealed that most of these shipped bugs would have been prevented or detected early in Swift-based code."
Apparently they were able to categorize their shipped bugs and prevent those types of bugs from happening again with Swift. That's a very professional process.
That's a very professional process.
Sad, but true. Not about the Swift part (I have no opinion there) but about the analysis and review of shipped bugs.It should really be "that's an industry standard practice", but a huge amount of software development seems to be done in ways that don't have any systematic way to attempt to learn from mistakes.
> Weinberg's Second Law: If builders built buildings the way programmers wrote programs, then the first woodpecker that came along would destroy civilization
It succinctly describes the "process" everywhere I've ever worked.
However, there are not any good reasons to avoid improving your process and output iteratively via feedback.
I've never heard that phrase before—is it yours? I really like it.
Google thinks its original to me so I've not accidentally stolen it unknowingly.
Mostly we just haven't been doing it as long.
> Mostly we just haven't been doing it as long.
This is a popular analogy (among non-programmers) but I have more recently been countering it like this:
It's generally predictable how long it will take to build a house, because many have been built, and they're all more or less the same. Sure they have different layouts, number of windows, etc, but essentially it's the same materials, same tools, and same methods.
Most programs written are not similar in the same way two houses are similar. They are more comparable to the way a house is different from an airport terminal, or a water treatment plant is different from a golf course.
Once you've built many different types of programs, you get a bit better at estimating, but each new type of program poses brand new challenges. A builder used to building small houses from wood is going to be pretty bad at estimating how long it will take to build a missile silo from concrete, and even after building both, will still not be able to come up with a very good estimate for the time to build a railway line between two cities.
Also keep in mind that typically, programmers don't build the same (or even similar) program more than once: unlike builders, we have copy+paste.
For example, static analysis returns nothing where it did something before. Not calling super.viewWillAppear() in a subclass does not raise the appropriate warning in Swift.
That's actually not true. You can see the trivial stuff it solves from the promo materials - you don't know the full extent of what's realistically possible to encode in the type system.
I had this thought yesterday writing C++ code - I was doing some re factoring and I copy pasted the same identifier twice instead of writing data to two separate buffers. The case where this mattered was not covered by unit testing because it's an edge case in a tightly coupled part - it's a major chore to test that kind of code for little gain.
My first instinct was "just put unit test there and forget about it" but on the other hand while buffer API is the same two buffers are semantically different so I could have made them two distinct types (eg. with template argument tag) and required untyped input buffer to be wrapped in a type explicitly stating data use case - this would have made the bug obvious and compiler would catch any discrepancy.
My point is there are plenty of non-obvious ways to prevent bugs trough a strong type system.
That's true but when you're running a business this type of analysis makes a lot of sense. As experienced software developers we're probably all pretty wary of the "big rewrite", knowing full well that this often doesn't end up saving time in the long run but rather eats up a ton of time without delivering any new features or functionality. This analysis likely helped make the case that going forward they could expect substantial time savings, not to mention happier customers.
In any case, great article overall, very interesting.
That's a very professional process, and it should also be an inspirational process.
Just because a lot of software development is haphazard doesn't mean that it has to be! Who cares if you use Swift or Ruby or C or Lisp, ignore the details - we should see the good, mature, "grown-up" things that other people do and copy & modify them!
Jack Ganssle (an embedded systems guru) has a great interview about being "a grown-up engineer" here: http://embedded.fm/episodes/2014/5/27/53-being-a-grownup-eng...
It applies to all software and hardware systems, not just embedded systems.
There's nothing professional about choosing the wrong platform for your application and then jumping into a new proprietary language in the hope of fixing things with a rewrite.
Are you trying to imply they are Apple devices and/or becoming Apple devices? Because that's entirely opposite of what's going on at the several local hospitals (admittedly all under the same organization) near me.
We have a major effort at Hopkins going on right now to get around this. See this article more info: http://hub.jhu.edu/2015/10/19/hopkins-microsoft-patient-safe...
Many of us don't have any issues with comercial vendors.
"When you you do robotics, you want full control over the software"
And OS X gives me this.
Of course it's not, because OS X gives you shit and you invested so much in it already that you have to convince yourself that it tastes good.
Particularly stronger typing is useful. Code that uses the Any object (or Object if you're in c#) tends to look horrible and have runtime errors, or it (what class it really is) has to occur to you as you're coding, which sadly doesn't happen 100% of the time for me.
I also find it's easier to not do [obj msg] everywhere. It just seems unnatural to have to go to the beginning of a token and put a [ in front. Much easier with a more ordinary syntax that's just obj.msg. ObjC had some of this, but mixing the two made it even weirder.
I did come across a compiler bug though, and that tends to take a long time to be sure of (don't want to write a bug report and find out it was your own code all along). I guess it's just a question of time before that sort of thing is stable.
Another pet peeve of mine is separating classes into .h and .m. I have to do this in c++ code as well, and I don't like it. Much better just to let the language take care of it for me. The interface is pretty clear anyway, so why separate it and have another file to keep consistent? I guess it's mainly a legacy issue.
One thing that needs to be done is XCode needs to be able to refactor Swift. Hopefully it's around the corner.
Why write things twice when the compilers are able to export text definitions from modules and IDEs can show them as well.
On the implementation file you have again to repeat those plus the actual implementation.
On many languages with module support, you just mark whatever symbol with public, that's it.
I also think I may have been conflating "header" with "include file" to a greater degree than is appropriate.
Public types are visible, which are the only ones that matter to the module user.
Many languages with modules support public and private comments as well.
Related: http://yosefk.com/blog/low-level-is-easy.html
In Objective-C writing headers by hand gives full manual control of what your library's users get to know about your class, including comments. Internal things just don't appear in the first place.
C++, of course, makes it impossible to hide anything from your users because they need to allocate your class themselves, you need to put all your private ivars and methods in the public class definition, and you can't add ivars to the class later. But that's its problem.
On the other hand, actual practice: http://www.gotw.ca/gotw/024.htm
http://stackoverflow.com/questions/12522053/what-is-a-non-fr...
1) They add fragility. It's all too easy to take a dependency on code without explicitly stating it. I'm using objects from "b.h" by including "a.h" which include "b.h" but I don't realize it.
2) They bring extraneous data into the code. A #include says more about the structure of your files than it may about the logical relationships between objects. Where to get things for the build is a job for the build config/system, not your business logic.
3) They're easy to use poorly. From your first circular include to the time you have to sort out a crawling build brought on by a bloated include tree, you'll swear there's gotta be a better way.
They are good for some people though; there's a small cottage industry that's built around resolving the myriad irritations headers bring.
I'd love to work with this guy. Overconfidence is not a virtue in developers.
It is natural, almost unavoidable in fact, that an application developed and continuously enhanced over many years will end up in a bit of a mess, unless the approach is very disciplined - which it was not in this case (inadequate unit tests for one example).
This was not a weakness or failure of Objective-C, but of circumstances and approach.
We do it about every 3-5 years, and it seems to coincide with new CTOs.
NSMutableDictionary<NSString *, NSString *> *dictionary = [[NSMutableDictionary<NSString *, NSString *> alloc] init];
compared to the swift version: var dictionary = [String:String]()and the flexibility is less too. The expressions are far from being equivalent. The Objective-C expression specifies what kind of dictionary interface and implementation will be there, where is the Swift's versions says there will be just Swift's map. Ability to manage implementation may for example start to matter once you're off beaten path - like large number of items, high concurrency or some very specific requirements on life cycle, memory or performance or you want/have to exploit very specific type/set of keys. Though in mainstream case general standard implementation is probably just fine. Everybody chooses their own poison.
[NSMutableDictionary dictionary]
instead of -alloc -init on the annotated class. NSMutableDictionary<NSString *, NSString *> *
imagine having to pass the typed dictionary into a method, now you've got to add the type declaration to the method signature in addition to it being initialized elsewhere. Its brutal but also worth doing if you stick to Objc."Over the years, the original version of Lyft had ballooned to 75,000 lines of code. By the time the company was done recreating it in Swift, they had something that performed the same tasks in less than a third of that."
If you're interested in learning Swift, I have a small project where I collect all good Swift urls that I find:
http://www.h4labs.com/dev/ios/swift.html
Also, note that it's easy to start using Swift in existing Objective C code without needing to rewrite. It takes a few minutes to set up a project to use a bridging file. If you've got a lot Objective C, you can extend those classes with Swift by using extensions, then migrated a few methods at a time.
extension MyObjectiveC_Class {
func aSwiftFunc() { // Can be called from ObjC
// ....
}
}This makes a lot of sense. In Objective-C chaining too many method calls together gets ugly and you end up having to break it up into multiple lines of code for your own sanity. Take the code below, autocompletion is guaranteed to break on you for the Objective-C version.
Objective-C:
[self.view convertPoint:apoint toView:[[UIApplication sharedApplication] keyWindow]];
Swift: view.convertPoint(apoint, toView: UIApplication.sharedApplication().keyWindow)
Objc Refatored: UIApplication *application = [UIApplication sharedApplication];
UIWindow *window = [application keyWindow];
[self.view convertPoint:apoint toView:window]; [self.view convertPoint:apoint toView:[UIApplication sharedApplication].keyWindow];
No reason to skip the other places where dot notation would be useful. [self.view convertPoint:apoint toView:self.view.window];That segmentation is very similar to hobbyist 3d printers.
Looking at the solutions they sell my guess is that the system is expensive enough that adding an iMac rather than some other consumer PC doesn't change cost too much.
At that point they probably picked the platform they were most comfortable developing on. Going back to the first blog post shows that the author prefers Cocoa for developing GUI's and the bio states that he is CTO of the company.
Again, all speculation, but thats how I'll rationalize it until he tells us different.
Don't know about robotics, but OS X is architected to be able to handled real-time audio. So, I can certainly imagine (but don't explicitly know) that it has better responsiveness guarantees.
We originally built the software for these systems in a cross-platform manner, targeting Mac, Windows, and Linux using a C++ codebase. About eight years ago, we realized that as a small team we needed to focus on one platform in order to accelerate development and take on our much larger competitors.
After a lengthy evaluation of the Windows, Mac, and Linux development environments, we chose the Mac and Cocoa (despite none of us having much experience with the Mac before that). We rewrote our entire control software in Cocoa seven years ago, and looking back I feel that it was one of the best decisions our company has made.
Don't get me wrong, I'm sure Swift is a big improvement in a lot of ways, but LoC is a weird measure to focus on, IMO... though, I can't say I blame them. LoC is easy to compute. Turns out measuring actually useful things can be rather hard...
Unfortunately, it's the only objective measure offered.
Where are the defect rates? Coverage in unit tests? Time to delivery? User satisfaction measures?
Do I believe Swift is better? Sure.
Do I believe they eliminated some bugs in the original code? Yep.
Do I believe they introduced a whole whack of new bugs they now don't know they have? Most definitely. That's what happens when you re-write software (http://www.joelonsoftware.com/articles/fog0000000069.html).
I've been waiting on writing Swift in production until Apple stops making breaking changes to the syntax. Running a converter over a million line codebase every few months and then checking it in is simply not a good answer. I think we're either there, or very close. That said I'm definitely, super double not rewriting large swaths of code. There's no value there. New stuff? Totally.
Here is a list of activities that grow at a more-than-linear rate
as project size increases:
- communication
- planning
- management
- requirements development
- system functional design
- interface design and specification
- architecture
- integration
- defect removal
- system testing
- document production
Code Complete section 27.5. Size in this section is referring to LOC.Particularly because I have seen so many HN posts "written in X converted to Y success stories". Most companies just can't drop everything and do a code conversion. They need to do it incremental. It seems with Swift this should be straightforward but maybe its not (I have to imagine its easier than going X to Golang which seems to be in vogue)?
IMO would have found it far more interesting to hear about the migration than why Swift is superior.