Posts Tagged ‘google tech talk’

The Clean Code Talks - Unit Testing

Thursday, November 12th, 2009

The Clean Code Talks - Unit Testing

This Google tech talk is about the benefit of unit tests. It starts out asking one fundamental question: how do you write hard to test code? Despite most developers(myself included) being good at writing hard-to-test code, most aren’t quite sure how to do it. The speaker describes some of these ways; mixing the ‘new’ operator in business logic, looking for things, doing work in the constructor, global state, and deep inheritance, among others. All of these make it hard to isolate specific parts of the code to test - in the deep inheritance case, you’re also testing superclasses; in the other cases, you can’t isolate one function from all the other functions. It’s also hard to test purely procedural code - there are no ’seams’ that can be exploited to isolate specific parts.

The speaker then describes the progression of levels of testing. The highest level are scenario tests. These test the whole application, doing something a user would do and ensuring the correct thing happens. These test the whole app, which makes them slow; if the test fails, it’s also hard to know precisely where the test failed; you generally have to trace through with a debugger in order to find the specific place.

The next level of testing are functional tests. These test specific subsystems, with simulators for external parts. These are much faster than scenario tests, and you have a better idea of what failed. However, you still don’t know precisely what went wrong - If you’re testing a radio and it doesn’t work, you don’t have much of an idea why. You still have to use a debugger to isolate the point of failure.

This leads to the bottom level of testing: unit tests. These test individual classes in isolation from one another. They are very fast - the speaker suggests running them whenever you save. If all your unit tests pass, you have a high confidence that the class in question is OK, even though you are unsure about the interaction between class. It’s also very easy to figure out what precisely went wrong.

The speaker stressed that this is a continuum of tests; you shouldn’t have only unit tests or only scenario tests - you should have both, although unit tests are more important. It’s also important to have ’seams’, where you can inject the class’s dependencies in order to mock out their behaviour. This is done using dependency injection, where a class asks for its dependencies in the constructor. Going back to before, a class should be either in the business of construction or finding objects, or doing actual work - it’s easy to test either of these methods, just hard to test a method that mixes the two.

Closures in Java

Wednesday, September 23rd, 2009

Advanced Topics in Programming Languages: Closures for Java

This Google Tech talk was quite interesting, although a bit depressing since it seems unlikely that Java will get closures anytime soon. Still, it was a great talk on how closures could be added to Java cleanly.

The talk starts off with describing use cases for closures. The two main issues were to enable control abstraction APIs and to make API calls easier. Enabling control abstraction APIs means allowing programmers to define abstractions like the foreach loop without having to wait for them to be added to Java itself. Making API calls easier by removing the reliance on anonymous inner classes (which closures get compiled to) also looks quite nice.

The talk first presents a way of defining foreach using anonymous inner classes, and shows the problems that occur with this approach. The first is an issue with toString and lexical scoping, and then goes on to the problems with Exceptions, return and break statements, being unable to access non-final variables, etc. Many of these problems have solutions, but solving these problems has nothing to do with what you are trying to express and solving them just obscures the business logic.

A few examples of where closures would be useful are then presented. The first was a withLock construct, though the concept also translates to streams. The accepted Project Coin proposal on automatic resource management implements a subset of what you would be able to do with closures, using them to automatically close streams. Another example is performing timing on functions - without closures, this adds at least 7 lines of non-business-logic code to each method you want timing information on, whereas you could program a withTiming closure and only take two lines.

The talk outlines the requirements than any proposal for adding closures to Java must implement. The first is that it must be able to create control abstractions that are on par with built-in statement forms. They must be concise, or there’s no advantage in using them. Lastly, they must inter-operate with existing APIs that currently take anonymous inner classes.

The details of the proposal, which were quite interesting, were then presented. This proposal adds function types to Java - for example int=>int represents a function that takes an int and returns an int. Function types are implemented as an interface generated on-demand with an ‘invoke’ method. Since they are regular interfaces, you can extend them, make them serializable, all of the things you can do with regular interfaces. The syntax for closures is:

     {int x => x + 1}

Which represents a closure that takes an int and returns it’s increment. Unfortunately, closures do not have any way to yield a result early - returns will return from the function the closure is in, not the closure itself. The way these closures are made to be inter-operable with existing APIs is through a process called ‘closure conversion’, which will make a closure auto-implement an interface such as runnable if the argument is a Runnable. The compiler is required to enforce this, so you can pass in closures to API calls requiring a Runnable and it will work as expected.

To simulate build-in control abstractions, such as withLock, you don’t want to have to pass in a closure as an argument. The proposal thus suggests that functions requiring a closure as the last argument can be expressed as

withLock( lock ) { doSomething(); };

instead of

withLock( lock, {=>doSomething();});

which is much less cumbersome.

These closures work properly - they can access local variables, throw exceptions, and are interoperable with existing APIs. You also have completion transparency, which is the ability to create functions which return the same type as a closure they take as an argument. This allows for constructs such as withLock to not need to worry about the return type of the closure; you can define one withLock function and then be able to return calls to withLock as the proper type.

The talk wraps up with a number of examples of how closures could be used, and then a link to where you could get more information on the proposal. This was a great talk, and I wish the proposal was going to be implemented in Java 7.

JSR 305

Thursday, May 14th, 2009

JSR-305

This Google Tech Talk was about JSR-305, a JSR that was adopted and will be in Java 7 (I believe). This JSR involves added annotations for software defect detection - essentially, it aims to provide standard annotations that tools can understand and use to help detect errors in your code. Some of these have been implemented nonstandardly in tools such as IntelliJ, such as the @Nonnull annotation.

This talk went over what would be provided and examples of why this would be useful, instead of going into implementation which would be tool-dependent. He did mention one possibility for enforcing these annotations at runtime, but I think it would have been interesting to go more in-depth on this. Oh well, there’s only so much you can cover in an hour.

I’ve known about some of the annotations before as ones that were coming to Java7, eg @Nonnull, but not some of the others. While (hopefully) the ones built-in to Java7 will have compiler support, since the JSR includes a way of defining your own classes it would be nice if there was a way to be able to tell javac how to verify those, reducing the need for external tools. On second thought, this seems like informing the compiler how to check for violations of these annotations seem rather difficult; maybe it should be left to tools developers.

One thing mentioned in this presentation that I was unaware of was class- and package-level default annotations. When I heard about @Nonnull, I liked the idea, but it seemed like it would end up making Java even more verbose, since you’d usually want to add it to essentially every variable. Default annotations allow you to annotate a class or package and essentially have variables ‘inherit’ from this default; this allows you to make every variable in one class @Nonnull with only one annotation, much better than if each variable declaration needed an annotation.

Google Tech Talk: git

Tuesday, May 12th, 2009

Git

This Google Tech Talk, by Randall Schwartz, covers Git and how it works. I started using Git to host my emacs customizations on GitHub several months ago, but had no real idea of how to use it in depth. I could use it to commit changes and push to my repos, but did not know how to do any more advanced things, and almost deleted my work a few times while trying to revert commits.

This tech talk helped in my understanding of Git. Most of it was describing how Git worked instead of just what the commands do, which was very interesting and gave a better understanding of the system than just a rundown of the commands would have been. The downside of this is that by the time he got to how to do the commands, he was running out of time, so he ran through those pretty quickly. However, it was a great introduction to understanding Git and how to use it.

The staging area and index were explained quite well, which helps me understand a few times what I thought was committed actually wasn’t - I didn’t really understand why you had to add modified files before committing, and occasionally forgot to do this. Later, I was surprised when those files weren’t changed when I cloned my repos. Knowing how Git works will certainly help me use it correctly in the future.

Collaborative Editing

Thursday, May 7th, 2009

Eclipse Day at the Googleplex: Wiring Hacker Synapses

This Google Tech Talk goes over the algorithm Eclipse Ganymede uses for collaborative text editing. This feature allows two people to connect to the same file and make changes to it, with each person’s changes being reflected in the other person’s document. This is not difficult until changes start conflicting with each other: How do you preserve the meaning of edits when, due to latency issues, editing instructions may occur out-of-order? This presentation addresses that issue.

This presentation made me think of how to use Emacs for collaborative editing. A few thoughts and searches led me to several options:

The first option to use Emacs for collaborative editor is to use GNU Screen. Screen is a terminal multiplexor, allowing features such as multiple shells, multiple connections to a screen instance, and many other features. I’ve been meaning to get around to learning screen for a while; even though I mostly use run shells inside Emacs, solving many issues, Screen still seems like it would be useful to know. To perform collaborative editing in Emacs with Screen, just have one person start a screen instance, run ‘emacs -nw’, and have the other person connect to the same screen instance, probably via SSH. This will allow both users to view and edit the document at the same time.

Emacs can also spawn a new frame on any X display using the make-frame-on-display function. Just pass the function which display to use, and a new frame will pop up in that display. Since both frames will be running the same Emacs instance, each person connected will be able to see changes and edit in real time. Users can still affect each other, however; changes to the emacs session will be effected over all connected clients, and closing emacs will kill it for everyone.

There are certainly other ways to do collaborative editing within emacs: Just look at The EmacsWiki page on Collaborative Editing for more.