Friday, May 15, 2009

Learn to Program, 2nd Ed: A Book Review

Author: Chris Pine Format: Paperback, 200 pages Publisher: Pragmatic Bookshelf; 2nd edition (March 28, 2009) ISBN-10: 1934356360 ISBN-13: 978-1934356364 Amazingly, this is a "how to program" book that doesn't use JavaScript as the first programming language the newbie experiences. I'm intrigued. Of course, it's a second edition, so using Ruby isn't exactly new for the new person. I never read the first edition, so I'll have to rely completely on the text I have in my hand (uh...sitting beside me, since I'm keyboarding with my hands). Just looking at the TOC, I already like the book, since the "Getting Started" chapter includes how to get started using Windows, Mac OS X, and Linux. Truly an equal opportunity author. The first section of the Introduction showed me that author Chris Pine (as far as I know, no relation to the actor playing James Kirk in the current Star Trek film) is a person after my own heart. He compares the writing of instructions on making a PB&J sandwich to writing a computer program. One of my "issues" is writing good tutorials for the correct audience. The instructions on how to make a PB&J sandwich differ, depending on if your audience is a 5 year old child vs. a 35 year old adult, for instance. I heard something of my own "voice" in those particular paragraphs, as Pine outlined the set of instructions pretty much the same way I would. Pine almost immediately answered one of my questions (Why Ruby?) in the Introduction. The answer is completely pragmatic: "Ruby programs tend to be short". He compared the same task written in Java and Ruby. Ruby was just a little "blip" compared to the several lines of code Java required to do the same thing (print "Hello Word", as if that were inventive). In the "Getting Started" section, I ignored the instructions for installing a text editor and Ruby for Windows and Mac, and honed in on Linux. Easy stuff. Everything was already on my Ubuntu box. Time to move on to the first lesson. You can just about blink and be 4 or 5 chapters into the book. The tutorials for beginners are written in more less the same way, and with the same sort of problems as a lot of other beginning programming books. Integers, floats, strings, and variables are all delivered in short order. What's nice is that Pine's writing style is friendly and easy to read, and each of the beginning chapters are relatively short. This makes the learning, particularly for the programming newbie, go by fast and gives the sense of quick and easy learning early on. Of course, while you may cruise through the first 4 chapters at warp speed, Chapter 5 starts only on page 25, meaning you have a way to go before getting through all the material. I noticed a few, very minor omissions (actually, one "omission" was explained, and was only an omission in the instructions if the reader didn't actually read Chapters 1 and 2). One exercise in Chapter 5 required the reader to increment a value by one, which hadn't been illustrated in the chapter material. On the one hand, if you have any programming experience, you can figure this out on your own. If not and it stumps you, the answers are easy enough to find in the Appendix. Pine made a point of saying the book was spawned from an online tutorial in part, because of the amount of emails he received citing various omissions. From that point of view, I can see that this book has been very finely honed from its earlier incarnations, including the first edition in all likelihood. That said, this particular bolt could have used a bit of tightening, but it was one, small bolt. One of the things that's different about Pine's book is that he explains technical terms and concepts as he goes along. Most books define terms such as "methods", "strings", "objects", and so on separately and then illustrate their use in examples. Pine often has the reader perform various tasks using methods, objects, and variables either without explaining what they are, or explaining them during the actual tutorial. He "took a breath", so to speak, at the beginning of Chapter 6 and more specifically defined what methods are. This is probably good for the reader who wants to get his or her hands dirty right away, but it's important to not only be able to write code and understand why it works, but be able to understand what different parts of a program are called. This is the language used by programmers to speak with each other about how code works. I'm relieved that Pine doesn't spend a ton of time defining his terms before getting to work, but still, the reader will really need to pay attention to pick out these little tidbits nestled in the text. The first 6 chapters of the book are pretty much a basics primer with no surprises. What you put in to your small, newbie programs is just what you get out. Pine introduces how to write an interactive program with Chapter 7, which starts the second half of the book. I noticed that the exercises at the end of Chapter 8 asked the reader to use what they've learned, not just in the chapter, but in prior chapters (the question was implicit though, and I would have preferred if Pine had spelled it out). Not enough tutorial books do this or at least, they don't do this well. Stuff you learn in prior chapters doesn't get practiced in subsequent chapters usually, so the reader forgets about them. You can't learn to program (or to do most anything else) unless you are constantly using what you've newly learned and then adding all that to what you're learning now. Kudos to Pine for this. Pine's tendency to define terms as he goes along, which I mentioned earlier, can get a little muddy at times. For instance, in Chapter 9, he starts out by describing and illustrating local variables without letting the reader know there's such a thing as global variables. You can still follow along and pick up the gist of the chapter, but it would have helped if Pine would have just defined the terms straight out here. There are several other places in the book where, if you aren't paying attention (a highlighter sometimes helps), you could miss out encapsulating the conceptual bits of the book into your memory. Having said all that, you also encounter another real conceptual explanation (they're rare) of local variables in Chapter 9 (particularly in the section "Experiment: Duby"; Pine has a definite sense of humor and it's evident throughout the book). I have to admit that I was a little surprised I could take my hands off the keyboard for 3 pages and just read something. No complaints either way, but the book was just so "hands on" (excepting that small bit in Chapter 6 I previously mentioned) up to this point. This is proof that explanations are necessary from time to time, and Pine does include more of these narratives as the book progresses. The author "declares" the readers as a "programmers" if they've made it to the beginning of Chapter 10. I suppose it depends on how you define "programmers", but I wouldn't get terribly excited if you find yourself staring at Chapter 10. You're not ready for your first programming job yet and, you can't do anything "practical" with Ruby yet (think of a real world problem that can be solved by writing a program...can you write that program yet?). Pine seems to be doing two things here. First, he's bolstering the reader's confidence and re-enforcing that the reader really has learned "stuff". Second, he's declaring that the reader has in fact, gone through the bare bones basics of learning Ruby. Actually, most of what the reader has learned at this point applies to many programming languages (methods, variables, and such). This is good and a step in the right direction, but don't abandon the rest of the chapters. It's more than just fluff and in fact, Chapter 10 is pretty much "review". As I said before, I approve of review. Even by the end of the book, the reader can't be expected to write programs in Ruby equal to a professional programmer. This book pretty much gets the reader's feet wet and that's about it. This is no insult to the author or the book. In 200 pages (actually, the practical instruction ends at page 149), I wouldn't expect any book to provide a complete programming novice with that level of expertise. Chapter 15 covers where the reader can go to look for help in expanding their knowledge of Ruby or just getting out of a jam. The first suggestion is to consult irb, which should have been installed when the reader installed Ruby. Since my Ubuntu 8.04 computer had Ruby already installed, I followed the instructions to invoke irb, but discovered that I needed to install it separately. No worries there. It was just a sudo apt-get install away. Pine also provides a couple of online resources and, I presume as a last resort, he put his email address on the final page of the chapter. As my last resort, I Googled "ruby forums" and discovered there's quite a bit more online than listed in Chapter 15. Once you get to this point in the book, explore the "interwebs". Of course, there are always other books after this one, depending on where you want to go next (Pragmatic's list of Ruby and other programming books is found after the index in this book). I noticed that there was no accompanying CD/DVD, which didn't break my heart, but there was also no website listed in the book to support what the author has written. In other words, there's no place (at least no place stated) to go to get the code samples used by Pine, and being able to download a book's code samples from the web is fairly standard for programming books. I thought about this for a second and realized there was nothing really lost here. First off, the code used in the book isn't incredibly long or complex, so the reader doesn't have to keyboard his or her fingers to the nubs performing the exercises. Second, the only way to learn to code is to code, so it's good practice for the reader to write each program listed to hone their newborn skills. Since this book started out as an online tutorial, I did wonder though if Pine had maintained anything online to augment his writing? The answer (though not in the book) is found at The Pragmatic Books for this book. Probably the most valuable resource is the Discussion Forums for this book. There, readers have already asked questions and the author is quite interactive with them. What I came away with is a basic understanding of the principles of programming, which I recognized from other languages. I also came away with an interest in learning more Ruby. It's actually a pretty good language for learning programming, which I hadn't previously considered. It's also in wide use in the industry and who hasn't heard of Ruby on Rails (though the "rails" part is yet to come)? Regardless of your motivation for learning basic programming, I think Chris Pine's book does a fine job as an introduction. It's basic, fast, and funny, and while not revolutionary, Learn to Program will definitely get you started in the right direction.