Code Practices

I’ve been developing software over 30 years, 15 of those professionally. I’ve learned a thing or two from the real-world trenches, one of which is that all code written must eventually be maintained… probably by you. The more you can do to ensure your code can be understood 6 months (or more) later, the better (your) life will be.

But this is hard.

Not just whining that it’s hard, but it is a difficult thing to do because who can know what exactly you’ll need to know or care about during maintenance. And while you’re in the heat of the moment writing code, you may not consider everything that needs to be documented. This is why the best you can do is strive to write simple code, well-factored, and as understandable as possible from the get-go. Premature optimization is the root of all evil. Simple is good; simple is best.

Via Slashdot I see this article on Coding Practices. He raises good points, but at least in my experience it doesn’t always work out that way.

Continue reading

How do you people do it?

I am a Mac user. In fact, I don’t just use Macs but I write software for Macs as my chosen profession. I’ve been writing Mac software for over 17 years. The first computer I owned (well, parents owned it… first computer in my household that I had regular access to) was an Apple //e. I’ve been working with computers for over 30 years, and for the most part yes it’s been Apple-based. Sure I’ve used other computers and computing systems, but Apple computers and Macs are my mainstay.

Why do I prefer Macs? Because they are elegant, beautiful, “they just work”, there’s a familiarness in their use and design. Like any good tool, the tool should enable and enhance your ability to get work done and should not get in your way. This is the Mac User Experience. Granted, it’s not always perfect. Apple themselves have changed and refined the OS over the years, with some stumbles but some great strides too. Certainly you encounter 3rd party software that can be wonderful to use, and some that can be amazingly painful to use. Usually I’ve found the painful stuff is written by people who think they’re just writing software and don’t fully grok the notion of “user experience” and what that means and why it matters.

That’s why I write today.

I’ve been temporarily reassigned to another project within the company I work for. This particular piece of software exists on both Mac OS X and Microsoft Windows. For the most part, that it exists on Windows matters not to me. However, I was just given a couple bugs (I didn’t write them, but I get to fix them) to investigate that necessitate the use of Windows.

I haven’t had to run or really touch a Windows box in over 10 years. Oh sure from time to time I have, at a friend or relative’s house, but in general I just have no reason or need for it. Thankfully the company has copies of VMware Fusion and some pre-made “virtual disks” with Windows XP already installed. A few hours of file transfers later (the VPN is slow) and I was up and running with Windows on my MacBook Pro.

That’s where the pain started.

How in the world can you people use Windows? 🙂

Apparently the XP installs were fresh installs, so upon first login I get bombarded with questions. Little balloons popping up everywhere trying to be helpful but only serving to overwhelm me with a desire to put my fist through the screen. You close one and another one immediately pops up, again, and again, and again, and a-fucking-gain. Oh hey it noticed some sort of hardware, tho I don’t know what, and it’s trying to help me cope with it but doesn’t tell me what it wants to cope with. And the stream of dialogs and “helpful” things just keep coming like and endless parade.

MY GOD WILL YOU SHUT UP AND STOP TRYING TO BE SO FUCKING HELPFUL!

The thing is, I’m a computer savvy person so I understand a lot of what’s going on and why. But I step back and try to look at this experience from the angle of someone like my mother, and I just cringe. No wonder she has to take her computer into the shop so often. No wonder she spends so much time on the phone with tech support. What a nightmare! And all I’ve done so far is log into the machine!

Yes, I’ve been away from Windows that long. I wish it could have been longer.

The I run some installers and wow, talk about more unfriendly and bizarre designs. I know some of this is merely because it’s the devil I don’t know… because it’s “not Mac”. Ultimately however it’s not that. It’s about poor user interface and user experience considerations and design. Granted it is not easy to do, to make something so complex, so technical, into something that just anyone can use. But I do like to think Apple’s done a pretty fair job with that.

I just cannot see how people can use Windows and enjoy it. Most people have to use it because work requires it of them, and then they use it at home because it’s the devil they know. As well, I know most people just aren’t into computers as much as I am so it’s not something they think much about. Or the old stigma’s remain about Macs that keep them away. It’s all about inertia.

I have to use Windows for a little while to deal with this bug. Thankfully it’s only for a little while. 🙂

If you want to learn more about interface and user experience design, check out stuff from Bruce Tognazzini and Jef Raskin. In fact, here’s a great little article by Jef on Tog’s website, about “intuitive”.

Death March (software)

If you work in the software industry and don’t know what a “death march” is, you need to learn.

Check out Edward Yourdon’s appropriately titled book, Death March for more on the topic.

You can’t work in this industry and not read that book.

Better than setuid – BetterAuthorizationSample

While scanning my RSS feeds I ran into this post from JongAm. It’s in Korean so I really don’t know what he’s talking about specifically, but generally I can tell he’s writing about Mac OS X’s authorization mechanisms. (BTW, I love that URL’s and such don’t choke on Hangul).

There’s something at work I’ve been fiddling with the past some days. The issue is that something has to be done and that something requires authorization. Unfortunately, due to how things can work under the hood by the drivers and the OS interacting, it’s possible things could change and thus require the user to authenticate again. This is not a desirable user experience because the need to re-authorize is an under-the-hood detail, not something the user would be aware of. Thus to the user the user experience is randomly, or not, being asked to reauthenticate. Not good.

So what’s the solution? The original thought was to create a little helper tool app that would have its setuid bit set and be installed as root, thus the user would have to authenticate at most one time and the little helper tool would then be authorized “forever” to do the necessary voodoo. A reasonable thing. And setuid tools are not a new concept and are well-established, but still they’re considered tricky and risky because you’re still running code as root which opens up all manner of security issues.

As I searched around for information, I came across Apple’s BetterAuthorizationSample. Obviously written by Quinn (thus you know it will rock), it’s provided as a way to achieve the same results as a setuid tool but in a safer way by use of launchd. What’s even better is BAS provides a library and mechanism to make it easy for you to utilize this functionality in your own code. There is step-by-step documentation that walks you through the whole process, and of course a sample app to demonstrate various levels of complexity. For more information, read the ReadMe, the Design and Implementation Rationale, and the lib HOWTO.

Getting it up and running in my app wasn’t too difficult a task. Just follow the HOWTO guide. It does take a little bit to wrap your head around it all just because there’s a lot going on, but the documentation is thorough and complete. Just do as it says and you’ll be up and running. Works great in my app. I’m pleased.

I will note that if you don’t have an uninstaller for your application, this is one situation that might find a simple uninstaller to be a welcome thing. There are not only files in odd places to remove, but also you will need to unload the Launch Agent from launchd. There’s also the question of removing the entry in /etc/authorization but there’s really no good way to do that. Still, something you need to consider.

All in all a fantastic bit of sample code, and while not necessary for all authorization needs, if you are considering writing a setuid tool for Mac OS X, you should look at this mechanism instead.

Good stuff. Share and enjoy.

More about the iPhone dying

I mentioned how on Sunday my iPhone died, apparently of sudden and complete battery drain.

I’m still mystified as to why, but a friend read my blog posting and said he knew of two similar experiences. He said he believed the culprit at the time was a game called “Stick Wars Lite“. Funny he should mention that!

I was going to pick up foo.c to take him to the IPSC match but I arrived very early and didn’t know if he’d be up and about yet, so I pulled into a nearby strip mall parking lot and started to fiddle on my iPhone. To my surprise, foo.c pulled in next to me because he wa coming to grab some breakfast. I got out of the car and went into the shop with him. All this time the iPhone was in my hands. I heard some faint music and looked at my hand and noticed that the iPhone was still on and the screen must have been touched because it had brough up a game.

Stick Wars Lite.

I distinctly remember that game being active.

I then just hit the power button to put the phone to sleep.

Three people experiencing this? Might be more than a coincidence. However, I’m not sure that is the problem because I’ve attempted to reproduce it and it won’t happen (of course).

Nevertheless, I believe the phone did die because the battery was drained by something not playing nice and thus consumed all the power. One recommendation given was to never put your iPhone to sleep with an application active, to always go back to the Home screen before sleeping it. IMHO that should not be a requirement, or if it is then the iPhone ought to handle it automatically when you hit the power button.

I’ll keep an eye on things and if I can reproduce it, certainly I’ll report it to the developer. Being a developer myself, I appreciate it when people file (useful) bug reports, so I make sure to do the same for my fellow developer. Need a reproducible case first. 🙂

Learning to Program

I learned to program on my Apple //e a long long ago.

But I did take a class in undergrad that was a sort of introduction to programming for non-programmers (now that I think about it, I don’t know why I took the class as it was well below my knowledge level). It used a neat book called Karel The Robot. You can Google on “Karel the Robot” and all sorts of stuff comes up, including a lot of love and praise for it. It’s really a good way to learn how to program because it’s simple and friendly. It doesn’t focus upon a particular language, which is part of the simplicity and appeal. It allows people to learn about general programming concepts and constructs, and how to use them as building blocks to solve problems. After you grok the concepts, then you can get yourself caught up in the semantics of a particular language… trying to do both at once is just too much to focus on.

So I’m writing this blog entry as a bookmark to myself. I’ve been wanting to teach my kids how to program and wanting to use Karel to do it. So I found Karel on SourceForge. I also found RUR-PLE (history of it here) which is a Karel-like approach that uses Python. There’s also Guido van Robot. I really like Python as a language (tho I don’t get to use it often enough), and feel it’d be a great first language for my kids.

Anyway there you go. Karel.

The plutil command obeys no one’s rules but its own.

I love stumbling across little bits of programmer humor.

I just looked at the man page for plutil (“man 1 plutil“):

STANDARDS

The plutil command obeys no one’s rules but its own.

Heh heh.

Working on a smaller machine

As a software developer I appreciate having good hardware. In fact, I appreciate having lots of good hardware as that best facilitates getting work done in a day.

Over my career I have evolved what I prefer to have for optimal work. I like to have a laptop on which I do “communication” work. So the laptop does email, web browsing, instant messaging, and whatever other administrata or time wasting I wish to do. Having it on a laptop is good because often such tasks require portability. I do set up the machine to also do development work, but it is not meant to be a primary dev machine.

I then like to have a very beefy machine for dev work. For instance, these days something like an 8-core Mac Pro with 10 GB of RAM and multiple internal hard drives works very nice. I also like to have multiple monitors attached to the machine because lots of screen real estate is good. Furthermore, it works better to have multiple monitors instead of one big monitor because there is often different logic that can be done based upon “screen 1” or “screen 2”, especially when doing things like debugging and needing to cope with the menubar and screen redraws.

Finally, I like having extra machines for whatever needs. These are often sandbox machines of various configurations that I can nuke and pave and do what I need to to help test, reproduce bugs, and so on.

So as you can see, I’ve found surrounding myself with a lot of machines is a daily necessity for getting my job done.

What happens when I’m forced to use a little machine for everything?

I’ve been temporarily reassigned to another group in the company that needs some help with their projects. Due to the nature of the products and the fact I like to keep very clean machines (sorry Unsanity; no Input Manager hacks here), plus given the nature of the work may require working in other locations, I requested they provide me with a laptop for dedicated use for this work.

I received one. A recent MacBook Pro.

15″.

Man, that’s small. Well, to me at least. 🙂

Compounding that is Apple changed some things in Snow Leopard to make stuff bigger. For instance, the default font in Xcode is Menlo Regular 11, instead of old Monaco 9 or 10.  I played with it some trying to pick other fonts or make things smaller, but I have to say, after I got over the initial shock my eyes do like the Menlo 11 better. But with bigger font means less content on the already smaller scren.

Then when I need to run Xcode for dev work, TextWrangler for notes and other things, Firefox to get into the bug database, and a few other apps… gah. Too many windows on that little screen. Sure I love Exposé and use it all the time, but it’s still a lot for that little screen.

So I started to use Spaces.

I toyed with Spaces before, but I just haven’t had a compelling need for it. I think it’s neat. I’m glad Mac OS X has it. But I haven’t been able to successfully put it into my workflow…. until today.

Turned it on, 4 spaces. Xcode on the main, TextWrangler “below”, Firefox “to the right” and since I prefer to use the keyboard I know the shortcuts to navigate around. Man… everything worked pretty slick. A few things were annoying, such as being on the non-Xcode space and then Xcode’s build window popping open on that space; it makes sense in a way, but it’s not what I want… I want to keep that app’s windows on that space. I wonder if there’s a way to force that.

I don’t know if I’d need to use Spaces on my big dev machine with the 2 monitors and lots of screen space. But on the little machine yeah, what a help it was.

Finally Upgraded to Snow Leopard

Finally was able to upgrade all of my Macs to Snow Leopard (Mac OS X 10.6).

You see, as a software developer I can’t always jump on the latest bandwagon. Sure I might have new OS versions running on partitions of other machines, maybe even pre-release versions. But I can’t primarily change things around because we might be in the middle of something. If I’m working on a release, to change things like the OS or the toolset could bring about big delays or other troubles. The rule is to settle on the toolset and environment, make the release, then you can upgrade stuff. And so finally I can upgrade, tho I have been using Snow Leopard in various capacities for some time.

I am enjoying the little refinements in the OS. I’m glad Apple took the approach they did with this OS and working on refining what they already have instead of having to cram a gazillion new features in. Make what you have really good.

One thing I got the most kick out of was seeing a lot of disk space reappear. My MacBook Pro was down to only a couple GB free and I was starting to look at buying a bigger replacement hard drive. After installing Snow Leopard, I regained almost 20 GB of disk space. Of course, I know exactly why this change came about, but that doesn’t make it any less wonderful. 🙂

OmniObjectMeter, I love you

Last night and this morning I’ve been dealing with some odd behaviors in the code I’ve been writing. I know it’s my mistake, the trouble is finding just where the mistake is. I spent time in the debugger observing behaviors that lead me to believe some object instances were not being released thus causing the side-effects I was seeing. As well, the lack of deallocation is a pure memory leak. So, find and fix the leak and many things should improve.

The current tool from Apple for finding memory leaks is the “leaks” tool of Instruments. I do think Instruments is cool and very powerful, but it’s also extremely obtuse and complex. It’s a tool that I don’t need to use all the time (in fact, rarely). Consequently what I learned the last time I needed it has left my head so I have to ramp up all over again. That’s just too much precious time spent, and too many brain cycles distancing myself from the problem at hand. Tools should not get in the way of solving your problem. Before Instruments, Apple had tools like MallocDebug and ObjectAlloc, which were useful and simpler tools but still weren’t the best in terms of interface and usability.

Some years ago I discovered OmniObjectMeter from The Omni Group. It was a godsend. It allowed me to pinpoint and track down memory leaks quickly and easily. It was so simple, so logical, so well thought out. It was easy to get going with it, it was easy to use it, and most importantly you could find your problems very quickly. Unfortunately OmniObjectMeter was left to languish and didn’t work for some time (there was a “secret beta” that helped it limp along). But I’m happy to report it’s back up and kicking with version 2.6 that was released February 2009. You see, I tried using Instruments this morning and while I could see the leak I couldn’t exactly pin down the location that caused the leak. I lamented for OmniObjectMeter, checked the Omni website on a whim, discovered v2.6, downloaded it, and within 5 minutes my leak was found, fixed, and verified.

That’s testimony. 🙂

I’m glad she’s still working. Plus, now OmniObjectMeter is free! I’m proud to say I paid for my copy (well, I got the company to pay for it) all those years ago. So FTC, yet again this is just a satisfied customer telling the tale of his happy experience.