• Chatter
  • Development
  • Insights
  • Honk.com

Author Archive

Javascript Design Idiom – Build Wide Not Deep

Posted March 8th, 2010 in Development by Brian

Providing a rich user experience calls for good client side software. Javascript is a powerful language to provide the needed functionality. Like any other tool it has it’s strengths and weaknesses. Successful design in Javascript favors composition, loose coupling, and separation of concerns more so than in most other languages. To see why, let’s look at some of the properties of Javascript.

Javascript is a prototype language with loose and dynamic typing. It is a flexible language. You have anonymous functions. You can alter objects at any time. You can aggregate functions from many sources. It provides closures and dynamic invocation. Also, each web page can be treated as a separate “application”, so you often do not need to have too many interacting pieces.

Javascript can also be a difficult language to use. Javascript does not give you a dependable stack trace. The debuggers often leave much to be desired. The weak type system makes it difficult to discern objects from each other. Inheritance is difficult.

These characteristics make it beneficial to build via composition. It makes sense to have loosely coupled objects. It also makes sense to unit test your Javascript software. It makes sense to craft the code to be scannable.

That being said, I really enjoy developing in Javascript because it encourages (or forces) me to better design my code. Working with a good testing framework leads to a quick, red/green/refactor cycle.

The Maintenance Costs of Complex Objects

Unfortunately, it is difficult to build an example because complexity in Javascript requires complex code. So I’m not really going to show code examples, but talk at a higher level.

Since Javascript does not provide good stack traces it becomes very time consuming to debug complex functions. To combat this problem, you should take advantage of Javascript’s flexibility and make loosely coupled functions that are composable. Good abstractions are important.

A good example of this is jQuery.

$(“.person”).click(function() {$(this).toggleClass(“active”);});

When an element with the ‘person’ class is clicked, the ‘active’ is toggled. Now imagine doing this with the old imperative style of Javascript. I could write the example, but it hurts to write and read, so I will spare you of it.

The more you can express in one line or one thought, the better.

Now these concepts are do not need to be constrained to DOM manipulation and hooking up events. You can add client/server concepts to the mix. Need to handle the case where the user can click on filters, each of which creates an Ajax request to update the search results? Don’t inline the code, create a function to do this. Give it a memorable name, like lastAjaxWins (not that that is the best name ever, but it is a memorable phrase).

The idea is to have consistent levels of abstraction. It may be expedient to have a function that has inconsistent levels of abstraction, but Javascript will not be very forgiving when you need to maintain and debug your code.

Fortunately, if you unit test and have loosely coupled objects and functions, your code will be easier to think about and your will not need to debug as much.

Heuristics to Measuring Complexity

Javascript makes complexity especially bad. When developing Javascript software, it helps to have some heuristics to keep the complexity down.

Talk About It

Whenever I complete a piece of functionality, I try to recreate the mental model of where this piece fits in on the web page. It should be clear, distinct, and easy to talk about. Talk about it with somebody. Is it easy to summarize?

If not, it is probably too complex. Break it apart. Create functions and objects which you can name. Compose these names to describe dependent objects.

Use Your Test Framework

A good javascript test framework (a couple of examples are Screw Unit and Jasmine) allows for nested ‘describe’ blocks and ‘it’ blocks (nested SUT’s, contexts, and test cases). When writing the name of the ‘describe’ block and ‘it’ block, try to capture the system under test, the situation of the test, and what happens. Be descriptive and precise. Don’t be vague. Being vague is cheating.

This is important because the length and complexity of your statements are a good indication of the complexity of the functionality you are testing.

Are the tests very painful to write? Are there repitive paterns? If yes, then that may be an indication that you need to extract something.

Refer to your Mental Model

Think about the client/server interaction. Whenever you add functionality, think about how it fits into the overall picture. Unless you are doing really heavy client side development, it should be easy to think about how that object and/or function fits into the picture. If you are doing heavy client side development, use good abstractions. Talk in the language of your abstractions. Let the concepts permeate through your consciousness. Having an intuitive grasp on your past, present, and future problems and solutions.

To Pair or Not to Pair

Posted January 31st, 2010 in Development, Featured by Brian

Here at Honk, we have been transitioning away from pair programming. I do not think that this necessarily reflects badly on pair programming. We still do some pair programming. However, our current circumstances steered us away from pair programming being our primary development practice.

Pair programming is a very powerful way to transfer and increase the skill of your developers. During my time at Pivotal Labs (and on the first few months at Honk), I have made leaps in my design skill, development speed, and bug reduction. I cannot think of a better way to transfer skill. It is easy to learn from someone when you are in the act of solving a problem.

Accelerated learning is something that cannot be overstated, and thus makes pair programming a very sustainable process in the right circumstances. You can also concentrate two minds on a single problem. Compared with soloing, you have half the tracks of development with more concentrated thought on each track of development.

However, we currently have a situation where we have few developers and many problems to solve. We simply would not have enough tracks of development, given the size of our current team. There are two solutions, hire talent or make due with your existing talent. Unfortunately, finding the right fit is very difficult in an industry where there is not enough supply. So for now, we have to make due and try to get more done with the same number of developers.

Going back to primarily solo programming has been an interesting experience. I certainly feel less time pressure, and more empowered to focus on making better quality software. I think this has to do with my propensities, and the fact that there is nobody is there to prod me to take the quicker solution. I don’t mean this to be a value judgment, just a trade-off that has occurred.

A key practice is to keep communication open and share knowledge. Even though there is not the constant banter, you can still quickly and efficiently communicate with the rest of your team. For the most part, I feel that we have been successful at that.

Another practice is discipline is even more important. We no longer have our pair to make sure we are following good practices. That means, we need to keep ourselves in check. That means slower development on each track, but more tracks should mean we get more done. We must also be disciplined enough to recognize situations where some code can be controversial and ask for the help of a peer.

Integrating our work is also of the upmost importance. We still perform TDD and have a continuous integration server. We have verification via a smoke suite and manual QA to ensure that we release correct software. We also practice shared code ownership, and try to communicate issues with code.

So in the sense, we have modified our system of software development to account for present realities. That is not to say that everything we are doing is correct, but if problems emerge, we can combat them. In a future, pair programming may be the best option, given different circumstances.