background preloader

Code

Facebook Twitter

Singletons are Pathological Liars. By Miško Hevery So you join a new project, which has an extensive mature code base. Your new lead asks you to implement a new feature, and, as a good developer, you start by writing a test. But since you are new to the project, you do a lot of exploratory “What happens if I execute this method” tests. You start by writing this: testCreditCardCharge() { CreditCard c = new CreditCard( "1234 5678 9012 3456", 5, 2008); c.charge(100); } This code: Only works when you run as part of the suite.When run in isolation, throws NullPointerException.When you get your credit card bill, you are out $100 for every time the test runs. Now, I want to focus on the last point. But why do I get NullPointerException in isolation while the test works fine when run as part of the suite? TestCreditCardCharge() { CreditCardProcessor.init(); CreditCard c = new CreditCard( "1234 5678 9012 3456", 5, 2008); c.charge(100); } You run the test again; still no success, and you get a different exception.

But there is more! Where Have All the Singletons Gone? By Miško Hevery In Singletons are Pathological Liars we discussed the problems of having singletons in your code. Let’s build on that and answer the question “If I don’t have singletons how do I ensure there is only one instance of X and how do I get X to all of the places it is needed?” An OO application is a graph of objects. There are three different kinds of graphs I think of when I design an application Collaborator Graph: This is the graph of objects that would be emitted if you serialized your application.

If the new operators are mixed with application logic (see: How to Think About the new Operator) then the Constructor Graph and the Collaborator Graph tend to be one and the same. The above shows the application collaborator graph. Now, in order to have a testable codebase we have to make sure that we don’t mix the object construction with application logic. In our tests it is now easy to instantiate the graph of objects and substitute test-doubles for our collaborators.

Root Cause of Singletons. Since I have gotten lots of love/hate mail on the Singletons are Pathological Liars and Where Have All the Singletons Gone I feel obliged to to do some root cause analysis. Lets get the definition right. There is Singleton the design pattern (Notice the capital “S” as in name of something) and there is a singleton as in one of something (notice the lower case “s”).

There is nothing wrong with having a single instance of a class, lots of reasons why you may want to do that. However, when I complain about the Singletons, I complain about the design pattern. Specifically: (1) private constructor and (2) global instance variable which refers to the singleton. I would say that at this point most developers recognize that global state is harmful to your application design.

Someone pointed out that any design pattern can be abused. Now, there is one kind of Singleton which is OK. The other kind of Singletons, which are semi-acceptable are those which don’t effect the execution of your code. How to Write 3v1L, Untestable Code. July 24th, 2008 · 15 Comments · by Miško Hevery, Jonathan Wolter, Russ Ruffer, Brad Cross, and lots of other test infected Googlers This guide lists principles that will help you write impossible to tests code. Or, avoiding these techniques will help you write code that can be tested.

Make Your Own Dependencies – Instantiate objects using new in the middle of methods, don’t pass the object in. This is evil because whenever you new up an object inside a block of code and then use it, anyone that wants to test that code is also forced to use that concrete object you new’ed up. Java Specific Final Methods – Use final classes and methods all the time. C++ Specific Use non-virtual methods – Unless you need to override the method in a deep and scary inheritance hierarchy, just make the method non-virtual. Tags: Advice · Rant · Testability. Dependency Injection – Why Bother? « Chris Collins. Dependencies by jonathanvlarocca In this post we discuss the various mechanisms of Inversion of Control (IoC) including Dependency Lookup and Dependency Injection. We look at the differences between the various IoC approaches and present the advantages and disadvantages of each.

Take the following example Java code. The Target class needs to collaborate with a Dependency class to do its work. What is wrong with this code? Firstly, we are creating a hard link between the Target class and the Dependency class. This is known as tight coupling. Lets say you want to unit test the Target instance in isolation. Loose Coupling Is there a way we can satisfy this dependency, without the tight coupling? Inversion of Control, or IoC, attempts to solve this issue by providing services by which the Target class can satisfy its dependencies without the tight coupling between them. Dependency Pull Dependency Pull is a type of Dependency Lookup. Contextualised Dependency Lookup Setter Dependency Injection. Google Guice. Contributions Appearing in the Book. C#, Objective-C and JavaScript Move Up in TIOBE Index - ReadWriteCloud. TIOBE Software has released its programming community index for December 2011, and the numbers show that C# is gaining in popularity.

According to TIOBE, the most popular languages right now are Java, C, C++, C# and Objective-C. (In that order.) There's no movement at all in the top 3, though TIOBE says that C++ has lost a bit of popularity since December 2010. C# moved up from 5th place to 4th, and is just a hair behind C++. JavaScript moved into the top 10, up two slots from December 2010. Considering how much more prevalent Web applications are becoming, it's surprising that JavaScript hasn't moved up farther. Objective-C, jumped three spots to 5th place. The TIOBE Index from the TIOBE Software Site The TIOBE ratings are based on the number of page hits for languages by searching for "languagename programming" in Google, Wikipedia, Blogger, Bing, Baidu, YouTube and Yahoo. Their long term trends are interesting to look at as well. Alternatives to TIOBE Java C Objective-C C++ PHP.

[Infographic] PHP vs. Python vs. Ruby. The Number One Trait of a Great Developer. Note: Our very own [Tammer Saleh]( recently wrote a blog post about [hiring for judgement]( With his permission, we're reposting it here. Maybe the best programmers aren't those who spectacularly solve crazy problems, but those who don't create them, which is much more silent. – Lena Herrmann When I look around at other companies hiring Ruby on Rails developers, I see them focusing on three major traits: Super-smart; Large community following; Deep Ruby knowledge. They're all wrong. While these are great aspects in moderation, they all miss the number one quality of a fantastic developer: Judgement. A story about Jack and Dianne Jack is a Rockstar. Dianne is a good developer. “How many devices do we expect to have?” Dianne wrote a RESTful API endpoint in Sinatra with a MySQL DB.

Will Dianne's solution scale to 10k users? It's all about trust Hire for Judgement Tagged: Wildcards (The Java™ Tutorials > Bonus > Generics) Consider the problem of writing a routine that prints out all the elements in a collection. Here's how you might write it in an older version of the language (i.e., a pre-5.0 release): void printCollection(Collection c) { Iterator i = c.iterator(); for (k = 0; k < c.size(); k++) { System.out.println(i.next()); } } And here is a naive attempt at writing it using generics (and the new for loop syntax): The problem is that this new version is much less useful than the old one. Whereas the old code could be called with any kind of collection as a parameter, the new code only takes Collection<Object>, which, as we've just demonstrated, is not a supertype of all kinds of collections!

So what is the supertype of all kinds of collections? And now, we can call it with any type of collection. Collection<? Since we don't know what the element type of c stands for, we cannot add objects to it. On the other hand, given a List<? These classes can be drawn on a canvas: public void drawAll(List<? List<? Google’s Dart Programming Language Disappoints Hackers. As expected, Google announced its new programming language Dart at GOTO Conf. We previously reported on the language here. Dart is a new programming language that will run in its own virtual machine in Chrome and compile to JavaScript in other browsers.

The stated goals are: Create a structured yet flexible language for web programming.Make Dart feel familiar and natural to programmers and thus easy to learn.Ensure that Dart delivers high performance on all modern web browsers and environments ranging from small handheld devices to server-side execution. The essential purpose seems to be to create a language better suited for creating large, complicated Web applications such as Gmail. Since the two presenters at GOTO, Gilad Bracha and Lars Bak, both worked on Smalltalk related projects – Newspeak and Strongtalk- so many of us expected Dart to be Smalltalk like. The overall sentiment in places like Hacker News is that of disappointment, but some developers did find something to like. Evolution of Computer Languages [Infographics] Things You Should Never Do, Part I. By Joel Spolsky Thursday, April 06, 2000 Netscape 6.0 is finally going into its first public beta. There never was a version 5.0. The last major release, version 4.0, was released almost three years ago.

Three years is an awfully long time in the Internet world. It's a bit smarmy of me to criticize them for waiting so long between releases. Well, yes. They decided to rewrite the code from scratch. Netscape wasn't the first company to make this mistake. We're programmers. There's a subtle reason that programmers always want to throw away the code and start over. It’s harder to read code than to write it. This is why code reuse is so hard. As a corollary of this axiom, you can ask almost any programmer today about the code they are working on. Why is it a mess?

"Well," they say, "look at this function. The idea that new code is better than old is patently absurd. Back to that two page function. Each of these bugs took weeks of real-world usage before they were found. Is there an alternative?