

Guide: Writing Testable Code [pdf] - pushedx
http://misko.hevery.com/attachments/Guide-Writing%20Testable%20Code.pdf

======
stevekl
I want to use this opportunity to open a discussion and get hacker news
community's thoughts on preparing code examples.

I always find many contrieved examples in coding guides such as:

    
    
      class Bicycle
        def initialize(size_of_wheels)
          # blah
        end
    
        def run
          # blah
        end
      end
    

These are fine. But as such, the examples always fit well with the principles
and you try to implement the principles, reality is a always far more messier.

Are there a better way to write coding examples? does it ever make sense, to
grab snippets from a real code base and so that it is in touch with reality

~~~
nstart
There are better ways as given in Robert C Martin's excellent book, Clean
Code. He constantly refers to code taken from the Fitnesse repo and shows how
the code looked before and after refactoring and testing.

I do agree with the sentiment that real world code examples are more useful.
When I was learning design patterns during my degree, we were actively
discouraged from using Head First as THE reference even though it did simplify
things. Our lecturer was later challenged on this view. He then took the
students on a counter challenge to refactor and redesign code from real world
examples (taken from students' projects). Reality hit quite hard for a lot of
us as we struggled to see the patterns in less obvious situations.

An interesting point to note however, was that after gaining an understanding
of the design patterns at a practical level, I could then reference simple
examples as given in Head First and immediately formulate how I would use it
to solve a practical scenario that I might have been stuck with. Funny how it
worked one way but not the other.

------
SoulMan
Could have just posted the HTML link :) [http://misko.hevery.com/code-
reviewers-guide/](http://misko.hevery.com/code-reviewers-guide/)

~~~
lumpypua
PDFs are nice. I print 'em out, read them on the crapper and pretend I'm at
the googleplex. <3 Testing on the Toilet

------
CoffeeDregs
Seems like a nice guide and I plan to read it through fully. Does anyone have
suggestions for other "how to test software" guides?

~~~
jrvarela56
Follow
[http://googletesting.blogspot.com/](http://googletesting.blogspot.com/) for
weekly posts about specific testing techniques.

------
co_dh
I stop right at the "class". How possible can you test an OO class? Try some
functional program, they are designed to be testable.

