Recently we here in Bangalore were discussing different ways of doing things to make our software and work environment better. We were all convinced that one way to make our code better was for us all to help each other out to make our own work environment more fun to work. This was just one thing we all were convinced about anyway! Something to celebrate, Hurray!! :-) Of course, there were many different opinions about what it means to make it “better”.
At first sight we thought that it was our work processes that can make a difference in the way we write and perceive our code. Though, this influence can never be ignored, we believe that for a rapid growth company like ours, we programmers and developers also need to improve the way we think about code. We of course should be proud of what we have accomplished so far, but we got to remember as Olle would say, “it’s a marathon we are running for God’s sake!”. We need constant energy and effort to get it across the finish line.
We thought that for us and our code to look better, we got to think better. We got to be code designers and not monkey like application programmer. We need to get constant inspiration and fuel from other code writers and designers out there. We believe that the wheel has been already invented, i.e., someone else in the world would have already solved our problem at hand earlier and probably has a better solution! We just need to google for the right idea and steal it.
Steve jobs once remarked “Good artists copy and great ones steal!”
We steal better designs to be better designers!
So with the initiative from Jayesh we bought this book called “Design patterns: Elements of Resuable Object-Oriented Software” written by a geni called Erich Gamma (and 4 others). Ganesh started reading it first and gave us a nice 25 minute presentation this Monday morning when our internet line got down. We thought that it was a good start and wanted to continue evangelising our thoughts around code design.
We are thinking of doing a 3 blog series of “From Monkey to Man” depending on the popularity of this article. So please do give us feedbacks and counter arguments to encourage us write more on our reflections on Erich Gamma’s book.
Okay, thats enough of background info. Lets now jump start into what Erich sees in patterns…
Designing object-oriented software is hard, and even harder is designing reusable object oriented software.
How many times have you had the dejavu – that felling that you have solved a problem before but not knowing where or how? If you could remember the details of the previous problem and how you solved it, then you could reuse the experience instead of re-inventing the wheel again? The purpose of Erich Gammas book is to record that experience as “design patterns”
Gamma goes, design patterns capture solutions that have developed and evolved over time. Hence they are not the designs people tend to generate initially. They reflect untold re-design and re-coding as developers have struggled for greater re-use and flexibility in their software. Design patterns capture these solutions in a succinct and easily applied form.
Good object oriented designers will tell you that a re-usable and flexible design is difficult if not impossible to get it “right” the first time. Before a design is finished, they usually try it themselves, reuse it several times, modify it each time and then finally, you have a design that is “good enough” One thing great designers know NOT to do is to solve every problem from the scratch or from the first principle. Rather they want to re-use solutions that have worked for them and others in the past. When they find a solution to be “good enough” they use it again and again. Such experience & thinking is what makes them part of the greats.
Design patterns are NOT about designs such as a dictionary or a tuple or linked lists or queues that can be encoded in classes and reused as it is. Nor, are they complex, domain-specific designs for an entire application. They are descriptions of communicating objects and classes that are customized to solve a general or generic design problem in a particular context.
For instance, take the example of database connectivity, people have solved this in many ways and we do have our own standard. But do we really follow through the standards in all the places? Nope…the basics are that we need to connect to the DB once and only once per request and that it should be void of injection vulnerabilities. Why don’t we use singleton design pattern for this problem and the pattern will itself make sure to create only one connection even though your code attempts again and again?
The design patterns require neither unusual language features nor amazing programming skills or tricks. It can be implemented in Python although it might take a little more work than an ad hoc solution. But the extra effort invariably pays dividends in increased flexibility and re-usability. So, the question if is should we write the operation and function files in a procedural way? Nä, this is monkey work!
If we start designing our code with the back-drop of patterns in mind then we create more robust, re-usable and testable code.
Below are the arguments from Erich to use design patterns:
- Patterns make it easier to reuse successful designs and architectures.
- Expressing proven techniques as design patterns makes the codebase more accessible to new developers.
- Reduces the need for documentation
- Easier to maintain the system
- Put simply, design patters help a designer get a design “right ”faster and thus helps him/her reach the customer faster
I will stop here for now. In my next blog, I will talk about the details involved in design patterns…Erich has a solution for the three tier architecture (Model/View/Controller)!
Stealing is NOT bad afterall, it is clever! ;-)
#1 by Olle on June 24, 2009 - 15:22
There you go Ram.. Great!
#2 by Barry on June 24, 2009 - 15:36
Ram I like the idea of reusing designs that are successful this brings consistancy through the system.
Copying is not bad……..”The reason why we see so far is because we stand on the backs of giants”!
#3 by Ram Sundararajan on June 24, 2009 - 15:41
Yeah Barry, crush or get crushed ;-)
#4 by PEZ on June 24, 2009 - 15:45
That book is written by the Eric Gamma plus some more poeople, if I remember correctly. The Gang of Four. Just noting. =)
I don’t think I understand this part: “It can be implemented in Python although it might take a little more work than an ad hoc solution.”
#5 by Ram Sundararajan on July 29, 2009 - 11:04
I just updated the post with 4 others :-)
#6 by Ram Sundararajan on June 24, 2009 - 15:53
Yepp, there are four people who wrote the book :-)
What I mean with taking time is that we do not yet have a design pattern approach for many things. So I believe that it will take more time for us to build the platform and enable patterns in the code.
It was not a technical issue with Python. Python has great support for any good design pattern. It was more of an implementation set back.
Its easier and faster to hack around the code and create technical debt rather than creating good designs.
#7 by PEZ on June 24, 2009 - 16:04
I see. Then I’ll relax again. =) I think it’s important to remember that each language has it’s Tao. Design Patterns vary a lot between languages. Therefore that book can mainly serve as a manifest for the design patterns movement (as long as we’re not implementing in C++). We probably should be looking for good resources for Python Design Patterns. Google to the rescue, I found these immediately:
Part 1: http://video.google.com/videoplay?docid=-3035093035748181693
Part 2: http://video.google.com/videoplay?docid=-3246451475733984168 (Starts off with a discussion about paging)
Actually I’ve found those before. Have them on my iPod. They’re excellent.
I don’t think we need to build a platform to enable patterns. We just need to start using them. We are using them already here and there. But to leverage for real we should use them more explicitly and and of course also put a tag in the doc strings which pattern is in use.
#8 by RamESH on June 25, 2009 - 06:36
I feel that for implementing design patterns we need to start thinking “design” when we code…we got to get the general flow of the code. For this some knowledge of the available patterns in the market will be very helpful. That was the intent of the article rather than evagelising C++ instead of Python :-)
I feel that we can get a lot of inspiration from this book, eventhough it was written very many years back.
Yeah I have seen the Google video. You know sometimes I feel that we should buy Google ;-) They are pro Python and use it quite a bi themselves..Its so nice to look at their code and tech. discussions.
#9 by Walter on June 24, 2009 - 16:33
Gang of Four? So first they made music, and then wrote a book about software design? Cool.
#10 by Priya on June 25, 2009 - 11:56
Good initiative, Ram!
Copying in a nicer and better way! But I guess as time goes, creativity will vanish if reusable techniques are utilized again and again.
#11 by PEZ on June 27, 2009 - 11:15
I doubt it. I think we’ll get creativity at a higher level.
#12 by Mel on June 26, 2009 - 18:39
I can recommend the book Head First Design Patterns:
http://oreilly.com/catalog/9780596007126/
The whole Head First series takes a slightly different approach in their books with a lot of pictures, interesting and easy to grasp examples.
For me this worked really well and I found “Head First Design Patterns” a good introduction into the some of the more known design patterns.I think were the original GoF book can get a little bit dry, this book is really fun to read.
Agree with Ram. It’s good to know which techniques are out there and what other programmers are using in their daily work.