Clean Code Talks – Global State and Singletons

November 21st, 2008 · 10 Comments ·

by Miško Hevery

Google Tech Talks
November 13, 2008


Clean Code Talk Series
Topic: Global State and Singletons

Speaker: Miško Hevery



Tags: Uncategorized

10 responses so far ↓

  • Herme Garcia // Nov 21, 2008 at 3:00 pm

    Great and clear talks, Misko

    Thanks a lot.

  • Sandro // Nov 22, 2008 at 12:57 pm

    Great presentation! Singletons are indeed one of the more evil entities to a test environment. With the help of Guice, injecting “application” singleton objects is very easy thanks to the Scopes.SINGLETON binding. So there should be no reason to stick to the old global state ways which tools like these available.

    At the end of your presentation you mentioned some form of cue card that can be used in code reviews which have points to look for when evaluating code testability. I’m very interesting in acquiring a copy. Do you have it in an electronic format?

  • Eddy Young // Nov 22, 2008 at 4:26 pm

    You are a breath of fresh air. I really enjoy all your presentation, especially this last one.

    BTW do you read H S Lahman on comp.object? Much of what you advocate echoes what Lahman has been preaching for the past decades on comp.object.

    You talk of separating object instantiation from object calls; Lahman says that relationship instantiation and collaboration are two diffeernt concerns.

    Lahman also has an interesting view of client/service relationship. In his world (which I agree with), the client dictates what the service should do. However, most of the time, applications are designed so that clients respect their interface (maybe why SPI is so hard to grasp by some people). Therefore, dependencies between classes and packages, according to Lahman, derive from this “flow of requirements” from client to services. The biggest impact of viewing this way is that there is no such thing as application layers, but rather collaborating subsystems and levels of abstraction. For example, the merchant subsystem depends on the credit-card processing subsystem at the same level of abstraction. However, the credit-card processing subsystem depends on the credit-card network subsystem from a lower level of abstraction.

    Again, thank you for a very nice presentation.


  • Johannes // Nov 22, 2008 at 7:15 pm

    Hello Misko,
    are these code review cards, that you mentioned at the end of your talk, somewhere online available?


  • misko // Nov 23, 2008 at 1:59 pm

    I am working on open-sourcing the review cards and they should be available soon.

  • Nitin Verma // Dec 26, 2008 at 6:23 am

    Nice presentation Misko, I do all the parameter passing to make my code more testable as you said and I do agree with you on most but we make global to make life easy for most. Like Object never knows about its garbage collector, new Object(gc1); or new Object(gc2); … 2ndly I am not sure why you said Singletons in java are at JVM level and we can not have multiple apps using different instances in a single JVM. Classes are loaded by class loaders and we can have different instances of the Class Object per class loader.

  • Žilvinas // Dec 30, 2008 at 8:22 am

    I watched all of your online presentations and this particular presentation is a big “aha” moment for me.  I also enjoyed your talk about polymorphism, your notes about precondition checking, minimizing class responsibilities. Simply put. Thank you – you have relieved questions of my mind that were bothering me for years in search of code elegance. I’m very eager to watch more of your talks.

  • » Archive » Miško Hevery Programming Talks // Jun 29, 2009 at 11:35 am

    [...] Global State and Singletons [...]

  • Toby’s Epril » Blog Archive » 싱글톤과 스프링 // Jul 21, 2009 at 5:20 pm

    [...] 전역상태의 문제점에 관해서는 그의 Clean Code Talk 시리즈의 하나인 Global State and Singleton을 보면 이해하는데 도움이 될 [...]

  • R'phael Spindel // Aug 7, 2011 at 4:21 am

    Great talk,

    Only the guy in the blue shirt Gr0ked you completely, especially with his comment at the end about passing in any stateful jvm library objects (Date, Math.Random, etc.)…

Leave a Comment