Restructuring Code: From “Push” to “Pull”

Nicole Rauch and Andreas Leidig

Workshop

Summary

Developers often experience code that is both hard to read and to track. At the same time, they strive to write “clean code” that follows certain principles like single responsibility, encapsulation and coherence. While most developers know these principles by name, they often find it hard to establish them, especially in legacy code. They feel the need to restructure parts of the application, but they have no idea how they could perform such a large-scale restructuring, and sometimes they might not even have an idea of what the resulting code should look like. One can find lots of information on how to perform small refactorings in legacy code, but there is almost no guidance for performing large restructurings and redesigns.

Our strategy addresses a specific coding pattern, which we have found in many applications: Fields in objects are written from multiple locations in the code. This has various effects: The value of a field at a given time is not clear, and tracking back all the methods that write the field is difficult. Also, it is hard to tell how and when the results have been determined because there is no unique location in the code where the transformation from the input values to the individual output values takes place. We call this way of coding “Push” because the code actively pushes its results into some object’s fields. These results are determined in a procedural and sequential way. The resulting code is rather fragile and tends to be buggy. Changing and extending the code is difficult and often turns into a “Shotgun Surgery”.

Instead of using the “Push” style, the output values can be calculated on-demand when retrieving them. This way, the algorithms for each value are clearly visible and very descriptional. We call this style “Pull” because the values are pulled by other parts of the code. The resulting code has proven to be readable, understandable, testable, changeable and extensible.

This workshop intends to give a concrete example of how a large restructuring can be performed in a systematic, structured and reproducible way, taking small and safe steps. The participants can directly experience its advantages by implementing “Pull” style code themselves. If this strategy is carried out consequently, the resulting code follows the abovementioned principles and becomes object-oriented, with small methods in objects communicating with each other, in one word: the code has become “clean code”.

Participants should have one computer per pair, containing a Java IDE. There should additionally be one keyboard and mouse per pair.

Download SlidesDownload workshop guide

Presenters

NicoleRauch_110x128

Nicole Rauch is a software developer with a solid background in compiler construction and formal methods. Over the last two years, she worked on the restructuring of large legacy code applications with different technologies. Alongside her occupation as developer, she took part in organizing some self-organized conferences related to agile coaching.

AndreasLeidig_107x128

Andreas Leidig has broad experience in object oriented software development and agile practices. He started programming years ago with Smalltalk and worked as an agile coach for a few years. Nowadays he is working full-time in developing enterprise software products.

Nicole and Andreas co-operate at work and in user group activities. Their passion for producing great software led them to organize a conference on Software Craftsmanship, known as SoCraTes. Subsequently they initiated Softwerkskammer, a Germany-wide user community for software craftsmanship, and Nicole founded the regional Softwerkskammer branch in Karlsruhe.