23 September 2017

Verbs instead of Nouns

Verbs instead of Nouns is a basic Coderetreat activity. It was used right from the beginning of Coderetreat. I tried it the first time during the GDCR 2012. The goal was to focus on verbs instead of nouns (obviously ;-). By searching for verb names, we did not think about what a class represented or contained, rather what it did.

Constraints in General
A constraint, also known as an activity, is an artificial challenge during an exercise, e.g. code kata, coding dojo or Coderetreat. It is designed to help participants think about writing code differently than they would otherwise. Every activity has a specific learning goal in mind.

Constraints are the primary tool to focus a coding exercise. For example, to improve my Object Orientation, I will practise Jeff Bay's Object Calisthenics or even Brutal Coding Constraints. Some constraints are an exaggeration of a fundamental rule of clean code or object oriented design and might be applicable during day to day work. More extreme ones will still help you understand the underlying concepts.

Learning Goal
Verbs instead of Nouns is listed as stretch activity. Stretch activities are designed to push you out of your usual coding habits - your coding comfort zone - and broaden your horizon by showing you new ways how to do things. By design stretch activities might look awkward, ridiculous or even plain wrong.

The learning goal of Verbs instead of Nouns is to push you out of noun oriented thinking. Noun oriented thinking is a way of object orientation, where the nouns of the problem description become classes, and the verbs become methods. This is the classic definition of Object Oriented Analysis and Design. As with any technique, following it blindly is not healthy. According to Alan Kay Object Oriented Programming is about messaging and encapsulation. He wanted "to get rid of data". His objects are defined by the messages they accept. Object orientated programming becomes verb based, if we focus on behaviour.

In functional programming, verbs are natural. All activities are functions. For example Steve Yegge describes functional programming as verb based in his humorous critique of 2006's style Java. Verbs instead of Nouns is an object oriented constraint.

Interpretation of the Constraint
Besides its name there is no information about this constraint available on the Coderetreat site. There was a discussion how to meet the constraint (which has been deleted to make space for the new GDCR organisation): Separate value objects from operations and build service objects for the operations, which would be named with a verb. Or do not consider what a class contains or represents, but what it does. This keeps the concerns separated and the classes small and simple.

Being a Value
Obviously not everything can be named with a verb. Values, at least primitive values, are things: 2, true, "Hello". The Oxford Dictionary explains value - the way we use it in code - as the numerical amount denoted by an algebraic term; a magnitude, quantity, or number. Now "Hello" is neither a quantity nor number, it is a constant term. The entry about Value Object on Wikipedia defines a value object as a small object that represents a simple entity whose equality is not based on identity: i.e. two value objects are equal when they have the same value, not necessarily being the same object.. The definition uses "having the same value"... I am not getting anywhere.

On the other hand, in the Lambda Calculus, even numbers are represented as functions. For example the number two can be represented by the higher order function n2(f,x) = f(f(x)), see Tom Stuart's Programming with Nothing. Being a function makes it verb based but which which verb would name n2(f,x)?

Suitable Exercises
For a stretch exercise, a suitable exercise is challenging. There is no point if everything goes smooth. We need an assignment that does not support the constraint. Everything that is functional in nature is not suitable, because functions are verb based. This rules out algorithmic exercises as algorithms are usually functional. We need a kata with some state - some "values" - and the need to mutate that. Let's try different problems.

Discussion of Game of Life
As I said, I did Verbs instead of Nouns first on the Game of Life. While Game of Life is a larger exercise, most of it can be implemented in a functional way, lending itself to the constraint. Here are some of its classes:

Classify has two implementations, ClassifyPopulation and ClassifyReproduction. Both classes check if a population is optimal for survival or not. There is one public method and its arguments are passed into the constructor. These classes are functors, function objects, the representation of functions in object oriented languages. The class name suits these class and the verb oriented thinking helped in extracting and evolving them.

LocateCell represents the position of the cells in the grid. It contains two integers x, y and an equals method to identify same positions. What is the verb of being a coordinate? A coordinate locates, as it discovers the exact place or position of something (Oxford Dictionary). Here Coordinate might be a more natural name.

I am unhappy with LookupLivingCells. It has two methods reproduce and isAlive. The verb Lookup only points to the second method. A proper class name should contain all functionality the class offers, so LookupAndTrackLivingCells is more appropriate. I do not like class names with And in them because they violate the Single Responsibility Principle. On the other hand - in an object oriented way - the class is fine as it encapsulates the collection of LocateCells and represents a Generation of cells.

Discussion of Trivia
Next I tried refactoring towards the constraint. Refactoring towards a constraint allows a more fine grained transition. Together with fellow craftsman Johan Martinsson we worked on the Trivia exercise and spent several hours extracting "verbs" from the legacy code base. (Many of the observations I describe later were made by Johan or found through discussion with him.) Let's look at some of the classes we created:

We extracted Ask. An noun oriented name might be Questions or QuestionsDeck. It is a closure over the list of questions and it does ask them.

MovePlayerOnBoard contains the board of the Trivia game. We felt being unable to escape our mental model of objects as state. On the other hand, the code for the class was chosen only by looking at the behaviour. It must be good. MovePlayerOnBoard has one public method but is not a functor because it contains mutable state, the positions of the players on the board.

Score is a similar reasonable class by object oriented standard. A player scores by answering correctly, or does not score by answering wrongly. Like MovePlayerOnBoard and AllowToPlay, it is a real object with internal, encapsulated state and various methods manipulating its state. These classes are far away from functors and functional programming.

Conclusion
Verbs are abstractions, too. There are "small verbs" like increasePurse, and higher level ones like moveAndAsk. Smaller verbs are easier to identify and to create or extract. Most of our verbs encapsulate primitives. If the code is primarily state, finding a suitable verb is hard. These verb names feel even more "wrong" than other verb oriented names. Maybe, when we only behaviour of a class is mutating the subject, we should show the subject in its name.

Responsibilities
A method that does much is difficult to name with a single verb. In the refactoring exercise, we moved out logic to make the describing verb(s) simpler, clearer and "pure". During refactoring we had trouble finding concise verbs for convoluted legacy methods. I guess when creating verb based code from scratch, such methods would never exist. Naming classes as verbs helps to split logic into more classes containing different aspects of data.

Design
Many verb oriented classes are functors, objects with a single method. Some are closing over state. There are classes with different aspects of the same verb, e.g. answerCorrectly and answerWrongly in a class Answer. Despite some weird names, the resulting design was always good. The constraint drives to nice, small, focused objects.

Usefulness as Exercise
The constraint is difficult. Especially when dealing with state, it is hard to find verb oriented names. It forces small, focused objects and discourages state oriented designs like Java Beans. Intermediate Object Oriented programmers will gain most of the constraint. They understand the basics of objects and usually create noun based classes. With more knowledge of object oriented design principles like SOLID, the constraint might have has less impact on the design.

Example Code

20 August 2017

Same procedure as every year

One advantage of freelance work is that I can take as many days off as I like. Since 2013 I am not visiting clients during summer, instead I am working on a different project. This year, my wife wants us to reach production, I am pushing hard, working 14 hours some days.

My Trustworthy Hammer Drill

27 April 2017

Interview Dirk Rombauts

Next in line is Dirk Rombauts, a fellow software developer from Vienna, whom I met at the Vienna BDD Meetup.

Dirk, please tell us a bit about yourself.

I decided to become a software developer around the age of 16. I was into natural sciences, math, astronomy and computers at the time, and a lot of my older friends were studying to become software developers. I used to play with Lego all the time as a kid, and software development is a lot like building Lego things: you have building blocks - frameworks and libraries - and if you put them together in meaningful ways, you can create quite cool things.

My university studies did a good job of giving me a basic understanding of software development and of the limitations of what a computer can do. They also pushed me in the direction of curly braces, since our main projects were done in C++. I did a bit of work in Java and C++ before jumping on the C#/.NET bandwagon in 2003, and I've enjoyed the ride ever since.

The thing I am most proud of is the work I've done on Pickles, the open source living documentation generator. A living documentation is a documentation that is always up-to-date with the newest insights. Wikipedia is a living encyclopaedia, but unlike Wikipedia, with living documentation you do not need human intervention: assuming your specification is written in the Given/When/Then style of Cucumber or SpecFlow, Pickles will contain those (frankly quite boring-looking) specification files and turn them into a much better-looking web app. That means that non-technical people can read the specification files without needing a programming environment - or in other words: it becomes easier for non-technical people to participate in the discussions. Pickles has emerged as the leading living documentation generator, and receives regular code contributions.

RespectYou are vegetarian and mentioned ethics of work several times during our conversations. Why does that matter to you?

I eat vegetarian for two reasons: I consider the way animals are treated in the food industry to be unethical (there is that word again). I do not object to people raising animals, killing them in a quick and painless way and eating them - while it is done in a respectful way. Stuffing stalls to overflowing with animals, treating them like inanimate objects, hauling them hundreds of kilometres to a place reeking of fear and death to slaughter them - that is a far cry from treating animals with the respect that every living creature deserves. The other reason is that I believe - based on the results of scientific studies I've seen - that by eating vegetarian, I significantly reduce my risk of cancer or cardiovascular diseases. I do not try to convince people of my views: everybody should decide for themselves, I detest proselytising, and the sad truth is that for every scientific study that supports my view there is another study that says that another diet is much healthier. So it comes down to what you choose to believe.

What other topics are you concerned about?

As with the animals, I believe we should treat other people and the environment with respect. Cheating people is not respectful. Doing work that harms people is not respectful. Doing work that damages the environment is not respectful. Therefore, work ethics are important to me. I believe women and men are of equal value and should be granted the same rights, the same opportunities and the same compensations for the same job. That makes me a feminist, and I am proud of that.

We live in a world where everybody with an Internet connection can watch hours of pornography, and yet it's rare to see a positive attitude towards sexuality. This is very obvious in the way society treats sex workers: they perform a very important service and ought to receive gratitude and respect for that - instead they are treated like dirt, the name of their profession is an insult, and in some regions they are indeed even prosecuted (but not their customers). I hope it goes without saying that I do not condone trafficking.

There is a chasm between the rich and the poor, and the chasm grows wider all the time. I begrudge no-one the spoils of their work. But I do not get that a manager should earn over 10 times more than regular workers. And I do not think it is right for "the rich" (be they people or companies) to exploit others for the sake of profit maximisation. I live a life that is affluent compared to many other places in the world, and I too have the desire to earn a bit more, but at some point enough is enough. At least in my opinion - many people with big salaries seem to think otherwise.

And then there is this fixation on working a lot. In the 1950s, when automated assembly and production became a thing, people dreamed of working only a few hours a day and devoting the rest of their time to family, friends, hobbies, art, ... In one of his short stories, renowned science fiction writer Isaac Asimov has one of the characters reminisce about the bad old days of a demanding four hour work week. Over half of a century later we are still working eight hours a day, and there is more and more talk about increasing that to 10 or even 12 hours a day. Something went wrong along the way.

What do you consider the biggest challenges for humanity?

I think the biggest challenge for humanity is the ingrained "us versus them" attitude of people. People tend to divide the world in two categories: us and the others. They will look after the interests of the "us" group, and ignore (or even sabotage) the interests of the others. Back in the days of hunter-gatherer societies this made sense: in order to survive, a human needs a tribe. So survival of the human becomes linked to survival of the tribe. A hunter-gatherer society usually operated in a scarcity environment, so it was important to make sure that the scarce resources were obtained by the "us" tribe and not by the other tribes. The "us versus them" mentality was a significant survival trait.

2nd FightEver since the development of agriculture and civilization, there have been plenty of resources and it is no longer necessary to divide the world in those two categories. The "us versus them" way of thinking is so deeply ingrained in our genes that it is very hard to overcome. We see it in rivalries between supporters of football clubs, in nationalism, in racism, in ethnic cleansing, ... Much of organised religion falls into that category as well, compounded by the fact that you now have a higher authority that tells you that it is right and proper that you look after people of your of tribe and creed, while being content to let the others rot in hell or even helping them along on the way.

If humanity is to evolve beyond its current state, we will need to overcome to "us versus them" way of thinking. If we do not, we will be fated to go through endless cycles of war and peace. As population sizes grow, those conflicts grow in scale and destructiveness too.

What do you do to engage these topics? For example, did you take part in public protests, donate money to NGOs or sign petitions?

To be honest, I am not convinced public protests do much good. They gather a bit of attention, but next week the focus of the media will turn somewhere else and the protest and its causes will be forgotten. I think that continuously doing small things has a bigger impact. If over time enough people do small things continuously, that will achieve a more lasting effect than a public protest.

I eat vegetarian so the meat industry doesn't receive financial incentives from me. I separate my trash in recyclable categories. I do not own a car - while I do use car-sharing from time to time, the first option I evaluate for getting from A to B is always public transport. I sign the occasional petition, but like public protests, I do not expect much to come from that. I donate money WWF (World Wide Fund for Nature), an NGO that protect animal diversity.

While I could quit my jobs and do charity work, I rather keep doing what I can do best and earn my salary as usual, just working on "the right things". Do you think that is possible?

It is not easy. Our society is geared toward earning money, spending that money in exchange for enjoyable things, and not asking too many questions about where those things came from. Working as software developer or IT administrator for an organisation like Caritas might be a worthwhile thing, but the pay would be such that it would be hard to make ends meet. My current strategy is to work part-time, earning enough money to pay the bills and also having time left to enjoy life.

During your professional career, did you ever have to take difficult decisions because of your values and social responsibility?

I haven't encountered any moral dilemma's in my work so far, except perhaps the target industry of my former employer. I hope that should I be asked to do something that I consider morally wrong - like tampering with emission control measures - I will have the courage to say no and to go looking for another job.

How do you think about selecting industry, customer and project based on your values and social responsibility?

I think it is important to follow your conscience when selecting a job. If you do not, and you end up working in a field that runs counter to your values, you will suffer. You may not notice on a conscious level, but it does have repercussions. Recent studies suggest that if your own picture of reality is at odds with reality itself, your body will release large quantities of cortisol (the stress hormone). Increased levels of cortisol are linked to increased illnesses, increased anxiety and unhappiness. If the reality is that you are working in some job, but your values (your picture of how reality should be) tell you that you should not work there, then you are setting yourself up for a cortisol trip with all the problems thereof.

Let us be more specific: Would you work for an animal factory? Would you work for a company producing equipment for an animal factory? Would you work for a sweat shop exploiting kids in Asia? Would you work for a company producing equipment for such a shop? Do you have problems with any industries?

The Slaughter of NatureI do not want to work for most of the companies you describe: exploiting animals or people is a no-go for me. Being accessory to the exploitation by working for a company producing equipment for an animal factory wouldn't sit right with me either. Even designing a website for such a company would make me uncomfortable. That said, I will do that kind of work if that's the only alternative. It may sound callous, but I would rather work for the company with the sweatshop than for the animal factory: there are far more human beings than animals, so I feel we need to devote more care to the well-being of animals.

I would probably be alright with working in the porn industry: there is nothing wrong with porn in and of itself, although there sadly is much more stereotypical and demeaning porn than there is sex-positive porn. As for the military: I could imagine myself working on a defensive system, but not so much on an offensive system. Still, if the choice is between the military and an animal factory, I will work for the military (unless the project involves animals like the Navy Marine Mammal Program in the United States of America).

I do not have a problem working in the gambling industry. I have done it before, and have recently accepted an offer to do so again. I consider it the responsibility of every single person to decide if they want to risk their money in gambling. The odds of winning are low, but you will at most lose your own money if you bet wrongly. When a bank bets on the wrong horse, they can lose the money of all their customers.

Did you ever reject a customer based on your values?

My previous employer is active in the financial industry. At first I did not mind but over time I began to realise that the work I was doing benefits only the rich: it helped the rich getting richer. The other, far more numerous part of humanity did not benefit from my work. Additionally, the speculations of investments banks can have a massive negative impact on society, like the financial crisis in 2008.

When I went looking for a new job after five years in the financial industry, I looked in different industries. I was offered several positions in the financial industry, but I declined those immediately.

On the other hand, what would be industries or companies that you would love to work on?

I would love to work for an animal-related NGO like WWF. I believe that when we learn to treat animals and the environment better, we will also learn to treat other humans better. I would also love to work for a space agency or company. I believe that more knowledge is the key to a better world. And we need space travel: if humanity somehow manages to survive the next couple of hundred million years, we will need to go in search of a new home among the stars when the Sun increases in luminosity and renders Earth uninhabitable.

Thank you Dirk!

You are welcome!