Clean Code, A review

legit | 20 February 2010

A few months ago I was working on some code that someone else had written. I was trying to add a feature into a class and couldn’t seem to figure out how the class worked, and thus couldn’t figure out where or how to implement my addition. After reviewing the code for a while I started to understand what was going on in the code and had a good enough understanding of it to then implement my new feature. The problem was since I then understood what the code was doing and how it worked I also felt compelled to restructure it, perhaps in a more logical order, or just change it so that it would be easier to understand later on. My problem was that after thinking about a new way to structure that class I wasn’t really certain about why that would be a better alternative, it just seemed better. So after wondering why it seemed like a good idea I decided to do some research into how to write “better” code, and why “better” code is “better”. My research ended pretty quickly as I remembered a few of my workmates talking about the book “Clean Code” a while back and how it discussed what makes “better” code. So I went to my local bookstore, picked up a copy, and started reading it.

Clean Code: A Handbook of Agile Software Craftsmanship was written by Robert C. Martin who has previously published books (which I have not read) on software engineering. He opens up Clean Code with the classic “WTF” comic of software quality, you know, the one that describes good code as code that produces the least “WTF’s per minute”. This is a really good example, since, that is exactly the kind of experience that got me to pick up the book. Martins purpose for the book is to present ideas on how to clean up code, and reduce the “WTF’s/Minute”, and make it easier for others and you to understand and modify later on. The first thing that Martin discusses is the problem of bad code, what it is, how it happens, and what the long term results are (maintenance, lots of maintenance). He then quickly transitions into a discussion of what clean code is, he asked several senior and well recognized software engineers what clean code is, and then lists their responses to get things started. After a brief overview of what clean code is, its off to the races, in depth analysis of clean code techniques and plenty of code examples to go along with it.

By far this book is not a quick read, or at least it shouldn’t be, most of the code examples that Martin gives are given first in the difficult code to understand context and then cleaned up into a clean code example. This method of actually showing the before and after code samples, if actually read and understood, gives immense meaning to the ideas that Martin gives on how to produce clean code. That being said, it is because of these code samples that this book is a bit of a slower read. Martin also encourages his readers to take the time to understand the code changes, warning that by not doing so you will miss out on the real understanding of why these code changes are occurring.

Martin begins the clean code concepts with fairly cosmetic issues, naming conventions, class and method sizes, comments, and formatting. These issues are fairly light on the technical scale (with class and method sizes being a bit more difficult) however, Martin does a good job of showing the benefits of implementing these clean code concepts. Then Martin goes into a discussion of more technical issues, error handling, objects and data structures, crosscutting concerns, system wide organization, testing, and even some discussion of concurrent programming. All the while he discusses not only good principles but why they are good principles, and gives examples of how to apply them. On some issues Martin admits controversy surrounding issues and clarifies why he came to his conclusion, as well as what others think. Martin then finishes the book with three chapters which take entire classes from well know API’s and dissects them into better, cleaner code, using the principles previously discussed in the book. These sections, as Martin states, are necessarily slower to read, given the large amounts of code and the complex changes. However, by slowly reading through them and understanding the changes being made it helps to bring together all the concepts of the book and shows how large sections of code can be understood and cleaned.

In the end of the book are several appendices, one is an in depth chapter long analysis of some concurrent clean code concepts, which an excellent discussion of what makes concurrent code difficult to debug and how to clean it up. There is also an appendix that cross references the principles of clean coding with the code transformations made in the last three chapters of the book. This appendix makes the book a great reference to keep on the shelf after reading it.

Overall the book is incredibly helpful, especially to software engineers who are earlier in their career (such as I). Held within the book are many useful techniques that senior engineers have taken a long time to hone, and if you read and absorb the concepts I think everyone can learn something from the book. That being said, several of the concepts that Martin discusses are controversial. His opinion that comments are unnecessary if the code is well organized and named, and in general clean, is definitely something that could be hard to swallow (depending on your stance with commenting). Martin isn’t completely against comments though, just unnecessary comments (he still supports comments in API’s). One of Martins main arguments against comments is that they simply produce one more thing that has to be maintained later on, a valid point. But despite the few controversial ideas that Martin discusses, none of which does he force on you, the book is filled with many highly useful concepts and excellent descriptions of why they are good ideas.

In the end I really only had one major complaint about the book, which is the subtitle “A Handbook of Agile Software Craftsmanship”. It is my opinion that clean code can and should be produced in non-agile environments as well, which I don’t think Martin would disagree with. Clean code is by far easier to modify than unclean code, and so its applicability may be higher within an agile environment, but it is certainly still applicable in non-agile environments (where the requirements are more static). My favorite concept of the book is also in the title, the idea of software engineering as a craft, too much of the time it seems that software engineering is seen as “coding”. The idea that software engineering is just hacking up something that does what your manager asked for is outdated and I think this book gives a nice introduction to software engineering as a true craft that should be treated with care and responsibility. Instead of just writing code, we as software engineers should write code that is of high quality, something that is truly well-crafted.

After reading the book my coding style has changed, I try to write clean code the first time, instead of just getting it to work. I also have a better understanding of why certain coding changes are better than others, which is why I picked up the book in the first place, so it fulfilled my needs and then some. I highly recommend it for anyone that is doing any programming.

Next on the bookshelf is “Data Visualization: Principles and Practice” by Alexandru C. Telea.

Roadmaps in Software Development

legit | 11 April 2009

Ars Technica recently posted an article on Mozilla’s future version of Firefox.  This version, deemed Namaroka, is the version of the web browser that will be released after Firefox version 3.5, which is currently in beta.  In the article Ars Thechnica talks about the various features of version 3.5 and the upcoming features of the Namaroka release.  As part of the documentation for Namaroka Mozilla has put out a roadmap documenting the various goals and specific features of the release.

Many of the open source software that I keep an eye on include roadmaps in their external development documents, however I have read very little about the idea of creating roadmaps as tools for development. Part of the reason for this may be that what I have read just refers to roadmaps differently, or I haven’t read enough, or possibly that roadmaps counteract the ideas of agile development. Since there seems to be a lack of input on the subject, I thought I would examine some roadmaps and look at their purpose in Software Development.

Songbird’s Roadmap:

If you haven’t been introduced to songbird I highly suggest you check it out, it is an excellent media player built on Mozilla’s software that allows you to both play and store you’re music as well as explore the web in a musical context. They have published roadmap’s up to and after their release of their non-beta software (currently version 1.1), you can find them here: http://wiki.songbirdnest.com/Roadmap. Songbird appears to use their roadmaps as a way of tracking the progress of various large objectives of upcoming releases of their software. They also allow the community to interact with it via comments and the fact that it is in a wiki.

Songbirds roadmap seems to be a resource for the community to see what the development time is being spent on and how far things are coming along. Internally I would assume that it is used in much the same way, a way to track the progress of large objectives as well as pinpointing what the large objectives are.

The Wordpress Roadmap:

You can find Wordpress roadmaps here: http://wordpress.org/about/roadmap/.  Wordpress is an open source blogging platform (I use it for Coding Notes). Wordpress seems to use their roadmaps in a much more specific way than what Mozilla or Songbird does, Wordpress utilizes a ticket system to track the progress of and allow developer involvement in the process of dealing with issues that need to be tackled for certain release “milestones” or versions. As a result there are far more and much more specific items being taken care of on the roadmap, and the entire roadmap as a whole looks much more like an internal document than an external overview of whats going on.

The pro’s of this approach are that the community can get a much more specific view of the changes that are being made, and can perhaps have a more beneficial involvement in the large scale development process. The con is that naturally it is harder to see the overall picture. However by looking at the number of open tickets and closed tickets one can get a very basic picture of the progress (as of this writing for the 2.8 release it stands at: 271 closed tickets out of a total 762 tickets).

Mozilla’s Namaroka Roadmap:

The Namaroka Roadmap can be found here: https://wiki.mozilla.org/Firefox/Namoroka. While Ars Technica calls it a roadmap, Mozilla does not use that name but it is very much the style of a roadmap. Like Songbird’s roadmaps the Namaroka roadmap is a broader overview of the development that needs to take place. However, it differs significantly from the other roadmaps by providing multiple perspectives on what needs to be accomplished for the Namaroka release. What I mean by this is that they have provided multiple sections, a broad overview of goals, a rough timeline of when different items should be completed, and various areas of interest and requirements plus others. This allows for multiple perspectives to be taken on the same things. They don’t provide a way to see what is completed or in progress however, but the document is in a wiki so as the items are accomplished perhaps the document will change. Also like songbird they invite community involvement via a discussion section.

Personal Experience, a basic roadmap from my Senior Project

While I was completing my senior project my team developed a basic roadmap; it was meant mainly as a rough diagram of what needed to be done and a time line of when we could expect things to be finished. However, since we where following a win-win spiral lifecycle model we where working in a risk-driven environment, which meant that whether the timeline said we needed something done or not was purely based on the current biggest risk and not on what the timeline said (however, risk assessment included time constraints). Thus what was really more of our roadmap was our list of current and past risks and their level of significance, this not only showed what we needed to eventually consider but what was currently being dealt with (the highest risk). Unlike the other roadmaps both of these documents where used internally only and where never shown to our “client”. Since our client was also our professor he did see both documents but not as our client, instead as our mentor. Unfortunately, I don’t have a copy of either documents as neither where stored in our CVS.

Final Analysis: where roadmaps fit in agile development

As I stated earlier I think that one of the reasons that roadmaps are not discussed as a part of the development process as much as other documents are is because they tend to orient themselves more toward a waterfall model or non-agile life-cycle. I don’t think that this means that they can’t be used in an agile process, I do think they must be used differently though. So if you are trying to be agile how to roadmaps fit in?

In agile development more of an emphasis is taken on dealing with what needs to be dealt with than dealing with a specific order that common development tasks must be accomplished in. Thus by creating a roadmap it would seem that you are forcing certain tasks to be accomplished in a certain order or direction. As can be seen with the Songbird and Wordpress roadmaps there is not a specific deadline for the tasks other than the over all goal of the version release date, this allows for the various tasks to be accomplished in any order. Also the Namaroka roadmap does not give individual tasks a deadline but instead outlines a very rough idea of when groups of tasks should become “finished” in each various phase of the development. By not giving firm deadlines for specific tasks it is possible to maintain the agility of the development, furthermore, it is always possible to change dates, and by doing this agility can be maintained.

Along with maintaining flexibility in a roadmap they can be great tools in the beginning of a project by outlining a project and preventing developers from diving too deep too fast, and later on they can help developers still see the big picture while developing small sections of code. The key to utilizing a roadmap in an agile development is allowing change within the document, if it stays static and is the absolute rule to the path of development then it is much more like waterfall development and a hindrance to the naturally changing environment of development. However by allowing it to change constantly (like the risks in my senior project) it allows for the whole project’s progress to be visible without hampering agile development.

Furthermore, as all three of the open source roadmap examples shown above are available to the community, it can be seen that roadmaps can also be an excellent way to provide progress reports to the community (or client) of software projects while still maintaining flexibility. Although, depending on the client it may not be a good idea to reveal as detailed an overview of the actual design elements of a software project as these roadmaps provide.

I am very interested in hearing what others think about this topic, or if you know of any articles or books that talk about roadmaps involved in the development process I would like to hear about them.

Senior Project Reflections 1: Following the Life Cycle

legit | 6 September 2008

Chirp… Chirp… Welcome back everyone, it took me a bit longer than anticipated to write this but I am somewhat glad that I took the extended break.

If you like excuses then heres mine: I took an introduction to philosophy class because I somehow skipped a general credit elective in the process of earning my degree in Computer Science. The reasons behind taking a philosophy class instead of something else include the fact that philosophy is interesting to me (due in part to me being a Christian and growing up enjoying discussing theology) and also because I find the philosophy behind Artificial Intelligence(AI) research very intriguing but was somewhat ill-equipped as to how to approach it. I also enjoy hearing other peoples perspectives because it allows me to understand what I believe better and to better round out the sharp edges in my beliefs. At any rate It was a very interesting class and I am very glad I took it, I really enjoyed discussing metaphysics and the aspects of AI with my classmates (it was an online class so by “discussing” I mean online forum posting). So the excuse is that this class along with working full time took pretty much all of my time and I didn’t have much left for writing and “reflecting”.

However, now that I have had all of three months to think about software engineering I am finding that the break gave me a good opportunity to distance myself from the biases of the project and do a fairer analysis of my senior project. So hopefully you will enjoy it as well.

I’ve decided to post several reflections of different parts of my Senior Project. So here is the first part with an undetermined number to follow:

When beginning to reflect on my senior project a specific incident came to mind, I was sitting at a Starbucks listening to some marketers talk about their boss and clients and how they disliked them. The end result (from my perspective at least, since they never actually stated this) was that they didn’t enjoy working as a team since everyone had unreasonable and unachievable requests. I can and will discuss some other aspects that I found interesting about the marketers discussion, for now however, I am going to focus on the aspects of team building and one way that I found life-cycles can encourage quality team development. I realize I have discussed my teams life-cycle model before in my post “The Importance of Risk” but I hope I am bringing to light a different aspect of the model here.

One of the things that I found absolutely amazing about my senior project team was in the weekly meetings that we had together. We would usually get together on Saturday’s outside of class and work on our current and next iteration of our cycle (we were using the win-win spiral model), but in these meetings (and throughout the whole class) we never had a project manager, a project lead, or a born leader. Every meeting we would get together and discuss things and at some point someone would go up to the white board and begin facilitating and organizing the discussion. This facilitator would differ from week to week and they never lead the discussion but simply organize what we where all saying so that we could find the finishing point of that meeting.

The nice thing about doing things this way is that it avoided the standard “group project syndrome”, where one person would become a self assigned leader, and a couple of people would do all the work and couple of people would do virtually nothing. This type of group seemed pretty consistent in all of my previous projects and seems to be a natural way for groups to form because of the natural desire for a leader. One major problem with this “group project syndrome” type of group is that it results in very little team gelling and causes people to end up on very large critical paths by themselves. This results in a dangerous type of project that may never have an “end”.

However one of the beautiful things of software engineering is the invention of various types of life-cycle models, this makes working on projects so much easier because in “modern” life-cycle models the model itself can be the project lead, and the members of the team can simply be peers trying to work out problems and collaborate while following the model. If used effectively and actually followed life-cycle models can lead to wonderful end results with very gelled teams1. To some extent this happened with our team on the CBAA project, we would get together let the life-cycle model be the leader and then collaborate on the discussion of what needed to happen next according to the life-cycle model. Not having a person as a leader was a very important aspect that allowed our team to gel really well, no one was distinct or “special” in relation to anyone else which allowed there to be very little conflict over ideas and design issues.

By using the life-cycle model as the leader and collaborating with a team to come to a conclusion I was suprised by how many meetings ended with everyone agreeing and verbally saying “well its going to be tough but I feel good about it and I think we can actually accomplish this” and every meeting I left feeling very in sync with everyone else. This helped us to all know the direction that not only others were going but also the direction that the team was going as a whole. We certainly had things we could have improved on, many things actually, but I think that for only having a single semester we were able to accomplish quite a bit of positive team gelling through this collaborative effort and treating the life-cycle model as the leader.



1 – Tom DeMarco in his book The Deadline points out that although it goes against common business practice it is actually quite beneficial to keep well gelled teams togethor rather than separate them. The benefit being that once they are gelled they are much more able to begin projects in a good way rather than a rocky way, since they are already gelled and comfortable with each other.

My Thoughts on Modularity

legit | 8 March 2008

A while back when Mozilla released Firefox 1.0 and I started to explore the extensions I was amazed at the basic utility that I gained from some of the extensions. This made me think about the methodology of Mozilla in developing Firefox, I was sure that they would add some of the extensions into the core code base of Firefox. When 1.5 and then 2.0 where released I was somewhat amazed that they hadn’t added any functionality that where in my favorite extensions into the Firefox core program.

My senior project is essentially to develop a new quiz type for the MOODLE platform. The M in MOODLE stands for Modular, unfortunately the MOODLE platform wasn’t developed with the same methodology that Firefox was. MOODLE comes prepackaged with several modules, like a wiki, forums, and even a feature rich quiz engine some of which are in the core code base. Since my senior project is to develop a new quiz type the fact that MOODLE includes its own quiz engine would seem like a good thing, possibly less work for the team I’m working with on the senior project.

I now understand why Mozilla didn’t add some of the Firefox extensions to its code base. In order to spawn creativity and encourage extension development the extension can’t already exist. The problem with including extensions, no matter how basic, in the core code is that if anyone wants to add on to the functionality of the software and a module/extension already exists then most likely it will share some of the same functionality and developers, rather than develop the new extension, will simply learn to live with what exists. Unless specifically asked for (like my senior project) when something already exists in the code base it simply stifles development of anything new that follows the same functionality line.

I will say that MOODLE is very modular and has several great places where key components can be added. The problem with MOODLE’s design is that it includes too many modules as standard code base items, meaning they have documented the fact that certain modules are part of the core MOODLE code base. This creates difficulties when developing a new quiz engine because it would be easier to add on to the quiz module rather than creating an entirely new quiz module. However, since MOODLE is open source we can use the existing quiz engine and from it create a new quiz module that is our own, the problem with this is that it creates parallel development paths. Anytime MOODLE updates the quiz engine we would have to extract, modify, and re-release our own module in order to allow for the full functionality of the quiz engine that we are developing within our new functionality.

So what’s the moral of my little story? Essentially its this: when developing software that is designed to be modular/extendable make sure that you only develop the core code base, and don’t start adding new modules to the core code. Keep modular software modular. If MOODLE had left the quiz engine out of the core code base and marketed it as simply another module it would help but wouldn’t solve our problems. Doing this would however allow us to possibly work with developers on a smaller issue for development than have to look at an entire software package to be changed/modified. This is a hard thing to do though, especially for MOODLE, since you want all users to be able to do standard tasks, like take a quiz. But the key is to keep modular software modular.  At least as far I’m concerned.

The Importance of Risk

legit | 27 January 2008

    One of the hardest parts of a project for me is at the very beginning, getting started.  The start of a project represents an awkward and yet immediate transition from what do I know about the project and, what do I need to know.  What you do know is generally what your client, customer, or manager just told you about the project: “I need a humdinger that will zoom, zip, and fly.”  This, however, is where the awkwardness comes in, you now know what they think they want, the difficulty comes in translating that into a final product down the line.  And so naturally as a software engineer you start to ask questions about the product.  This is the start of finding out what you need to know, what the client means by “humdinger” and how exactly “zooming” and “zipping” relate to it. 

    This transition is a hard one because you begin with, most likely, a very vague understanding of what the client wants and its your responsibility to take that understanding and materialize it while they watch.  But all you have is a vague statement, so what does the final product actually look like? what does it really do? who are all of the stakeholders?  This all translates into a simple question of the scope of the project, and more importantly of version 1.0. Tied neatly into this question of scope is the whole bundle of what you and your teammates are as software engineers going to be doing until that 1.0 arrives and the client signs off on it.

    Scope, to me, is a scary thing, it defines not only the final product but more than likely it heavily impacts the core of your technology.  If you decide that these certain features are not going to arrive until 2.0 then you have to make sure that 1.0 is going to fulfill the architectural qualifications of allowing those features to be built in later.  Scope represents what you need to consider along the way of getting to 2.0 while you’re really just working towards 1.0.  This is where risk comes in, in my opinion to save the day.

    So, as I sit there with my teammates questioning what it is exactly the client wants and what I need to know in order to deliver it, risk starts popping up.  What if I don’t understand the client, the product, my responsibility, and so on.  Then reason and “project” kicks in and you start to look at your life cycle options.  Risk based life cycle models like the spiral or more refined win win spiral are excellent at starting projects appropriately.  More than likely all projects will start with some sort of problem of scope, meaning “what exactly was the client talking about?”.  With other life cycle models you may start with feasibility (waterfall), or some other standard starting point, or some other general direction.  But with risk based life cycle models you are immediately addressing the concerns that you face with the product.  If you understand the product but have another risk, then you can gladly and rightly start with this.

     This is where solace enters into the start of a project, because with other models you may have a place to start, or not, with risk based models you can immediately start to deal with the things that make starting a project so difficult.  If the team says “what exactly is a humdinger?” and that’s the largest risk, that’s where you start.  If the team says “I understand the product, but the timeline is too short” then you can start with reasoning out version 1.0.  Of course working with other models will certainly work, I would say that time has proven that using any number of models, including risk based, will work.  But one of the nicer aspects of risk based models is that instead of concerns on the backburner until the model allows time for it, with risk based models it can be immediately addressed.  This allows for a nice amount of stress to be dropped at the beginning of a project.

    – Legit

Managing Humans, A Review

legit | 22 January 2008

Michael Lopp is a middle manager of software engineering teams, having worked at various companies in the Silicon Valley including startups from the age of the internet boom his experience is truly varied. He has worked from the standpoint of the software developer, QA engineer and naturally the software engineering manager. This broad experience in work is very apparent and adds greatly to his book, which in the first chapter he jokingly titles “Don’t Be A Prick”. The book is described by him as “insights, ideas, and opinions about how to manage people” and his emphasis on people is spread throughout the book. In fact the key to what this management book is truly about is Lopp’s emphasis on people.

Managing Humans is a collection of Lopp’s essays from his blog, edited and categorized into book form, which gives it a very loose and varied feel. Chapter topics range widely from dealing with freak outs in the office to the importance of CVS comments. Due to this widely varied nature of the book the actual content and impact of the book is far greater than just a management book. In fact, management in this book is taken less from the standpoint of corporate policies and politics and more from the perspective of how to dissect the various types of people and how to utilize those people effectively, and more importantly how to understand and communicate with those people.

In one chapter Lopp describes the various types of attendees at meetings, how they interact, and their meeting personality. In another chapter Lopp talks about the different people that are important in the interviewing process, and while he advocates that an entire team should be involved he discusses the different bellwethers that add the most valuable feedback on the potential employee. In a broader since, Lopp talks throughout the book (with a pecific chapter for each) about the two rare coders termed “the fez”, and “the free electron”, but as mentioned earlier he talks not only about how to utilize them, but also how to make them better (more in the case of “the fez” than the “free electron”).

However, while Lopp talks about the different people that may be managed and how to utilize them he also talks about a varying degree of other topics, as well as somewhat how to be managed. He talks about how to resign gracefully, how to pass the phone screen, how he does the interview process, and how to pass the 90-day interview. He also talks about how to keep 1.0 software development projects alive from a management perspective. The most valuable aspect of the book, I believe, is Lopps explanation of how to deal with different people in a productive way, instead of a contradicting way. Lopp even talks about how to deal with the different styles of managers and how to communicate effectively to them, while still maintaining your sanity.

Overall, this book is more a dissection of the common Software Development process within a company and how to work in this environment than it is a management book. However, it would do a lot of managers some good to read this book in order to understand what, and more importantly who they are managing. On top of this issue Lopp’s discussions of being managed adds an important aspect that the common software engineer can benefit from. I would suggest this book as default reading material for anyone that is in the technology industry, as well as anyone that wants a semi-comical look at the politics of the standard office place.

- Legit