31 May 2014

The Template-Method-Antipattern

I had been skeptical about the GOF pattern book ever since I read it many years ago. Many of the patterns in the book seemed so trivial that I was irritated by how much attention they are given. Others have examples that seem overly simplified and never quite fit what actually happens in practice. Looking back now, I find that the premise of the book seems to be the early OO memes of "avoiding repetition" and "finding the right design for the application domain". The latter relates also to graphical modeling and model-driven-design. While those ideas are doubtlessly important, I think that early OO philosophy over-optimizes in that one direction and forgets about another very important direction: keeping the code as simple as possible. Instead of over-designing and already including space for "later extensions", realize that there are usually unknown unknowns and the later extensions might go into quite a different direction. It is the new agile world where running code and automatic tests are more important than fancy diagrams and great designs.

There are many guidelines which help us to write simpler (and thus more flexible) code. Before criticizing the template method even more, I want to remind you the two most powerful ones:

  • Number One law of procedural programming: favor pure functions over mutators. (And if you have mutators, separate them from the pure functions.) Note that this law fully applies to object-oriented programming as well!
  • Number One law of OO programming: favor composition over inheritance. (And let most of your inheritance be implementations of pure interfaces.)

I have always had trouble explaining why certain patterns were bad (especially those which over-use inheritance), but it was nonetheless very clear to me. I always found it hard to describe succinctly and precisely what the template does without going into the details of inheritance and the subclasses. Other people just didn't need this kind of clarity it seems. But yet, every time I had a debate with someone over a particular and specific piece of code I could convince them that my simpler variant was better in that particular case. So I am right in all cases, but still couldn't give a generally convincing reasoning why this is so.

Now recently I realized that rigorous unit-testing is a great way to validate a design: if it is hard to test, then that's a big smell and motivation to simplify! In a template method arrangement it is definitely hard (even though it's still possible) to test the template and its instantiations separately.

But instead of going on explaining what I find hard to explain, let's hear what others have to say:

2 May 2014

The start of my virtuous life

I recently finished working through the best self-help book that I have yet come across. "Ab Heute erfolgreich" (Successful from Today on) by German trainer and coach Alfred Stielau-Pallas. What makes the book so effective is the approach to read just one chapter per week (I usually did on Sunday) and then practice its teachings for an entire week. I liked this so much that sometimes I took a week off from the book to practice another important thing that I just had learned or been reminded of.
One of the many great ideas the book contains is Benjamin Franklins concept of 13 virtues which he practices one week per virtue for his entire life. (Going through all of them four times a year.) When I read this chapter I instantly thought that this would be a great way to continue the "weekly focus" which I liked so much about the "success book" (as I like to call it). But I also realized that Franklin's virtues aren't just right for me and the success book's practices are better, but still not completely reflecting my own virtues. So I knew from the moment of reading that I would have to come up with my own list.
Now, this actually wasn't easy and I procrastinated over it quite a bit. Fortunately I had already decided to go on sailing holidays for the May day long week-end and since sailing only takes a few hours each day, that's the prefect opportunity to do such kind of important work in the morning. (I only like be outside in the afternoon and evening anyways!)

So I came up with my own list of virtues and some practical exercises to go along with them, because I know too well, that just focusing on an abstract topic alone doesn't make it appear. It always needs concrete actions to take and finding those is not easy either. In fact, I plan to still spend at least one half-hour of focused thinking time per week to customize and concretify that week's virtue.

To start off, I wrote down some virtues which I think I already embody perfectly and which I don't want to practice in a focused way. Of course, I hope to still get better at those virtues, but I practice them spontaneously often anyway, so it doesn't feel just to give them even more weight. Those intrinsic virtues are Positive Outlook on life, Appreciation of small things, Gratefulness for what I have (and what I additionally get on each new day).
  • Not criticizing myself or others and instead focus on what's great about myself and others.
  • Thriftiness, frugality, and humility.
  • Humor.
  • Curiosity.
  • Creativity.
If you, dear Reader, now feel that I am at little arrogant and presumptuous here, praising myself so much, I can only reply that this is a simple exercise of my second virtue! While I hope to be well-aware of my shortcomings, I simply decided to focus on my strengths first! As the success book says: accept and love yourself as you are and only try to improve yourself one weakness at a time. I also want to add that this list is not set in stone. After each iteration through all my virtues (and especially after the first time through) I might decide to move any item from the "already perfect" list into the focus rotation list.

But now, let's get to my focus virtues. For each one, let me briefly call it out, define it, say why I want it, and sketch some actual concrete practice exercises.
Mindfulness: The full and untainted awareness of what I am doing, what I am thinking, and what I am feeling. Also the knowledge that my thoughts can be mistaken or can focus on entirely unimportant things. And the knowledge that all feelings are temporary and fleeting and most of them even go by without any action by myself. There's actually a lot more to say about mindfulness and there might be better definitions, but let me just explain why I intentionally put this in the number one spot. You might know the saying "what you can't measure, you can't manage." Obviously what you're not aware of is even harder to improve!
As with every other virtue I want to practice mindfulness every day (and in every moment) and specifically I want to keep up my daily sitting meditation practice and weekly visits to a meditation group. During the regular mindfulness focus week I might then do extra activities like trying out new kinds of meditation, doing longer stretches of meditation or on different times of the, or visit new practice groups, read or reread some books or articles on meditation, and finally try to plan the week so that every activity can be done in a mindful fashion

Empathy and Listening to others: Everybody loves talking and by listening we give the gift of our attention to the other person. It's a great way to become liked and also to learn a lot. For practice, revise some deep questions to ask others, don't be afraid of silence and long breaks to give them room to think and answer, and finally be aware of how much you are talking yourself and keep it to the minimum of things that the other person actually wants or needs to hear. 

Compassion and Listening to myself: This is based on the idea of a silent retreat for one week, but still going to work. I will restrict myself to only professionally needed conversation, and spend my private life in complete isolation for this week. While that sounds really extreme, it is just one week after all and I don't have that many private interaction with people anyway. I think that by offsetting such a Silent Week with its opposite –a Connection Week– I might even get more socializing and certainly higher-quality socializing than by just floating along through life.

Generosity: You might have heard about that psychological study which showed that people actually become happier by spending money on others instead of spending it on themselves. So there's not much here to say: just do at least one good deed per day and don't let it just be tipping generously. Spend some quality-time to think creatively about possible gifts. When you buy something, think: who of my friends or acquaintances might like that, too? Sometimes I just buy two of a thing and later decide who to gift the spare one to! When you see some advertising, think: who of my friends might like this? or something like this? 
Creativity is important here! Don't just dismiss an initial thought because you don't think any of your friends will like it. Just continue a theme with a chain of associations until you get to something that will actually be appreciated. For instance, when I see another beautiful Porsche car at the traffic light, I might think of a friend who's going on a road trip (be it by car or bike or by hitchhiking doesn't matter here!) Now I can think of anything that my friend might need for that trip, be it a map, a guidebook, a dictionary, a scarf, or some nice tires for the bike. 

Decisiveness: Now we get to the core of self-improvement and to the harder stuff (at least for me). I grew up as a rather indecisive person and liked to go with the flow and with the opportunities that present themselves. One of the big things I have learned from the success book is that decisions just have to be made to get anywhere. Going with the flow only takes you where others want you to go or think or should go, but finding your own way requires to make decisions! I was so very fortunate to have read a great (despite the cheesy title) book on decision making just before starting the success book. I found it very helpful to have some decision-helpers (like simply sketching up a pros-and-cons list). It's also important for me to avoid procrastinating on decisions by asking myself "until when do I need to decide that? and what additional information do I really need to decide well?" I can also unblock my own undecidedness in a question by setting myself a timebox of five to 30 minutes (or more for harder decisions) and use that as quality thinking time just for this one decision. 
But even with all those techniques (and many more in the decisions-book), I can still be in a circle of thoughts without realizing that a decision is needed. Therefore it is important to regularly practice explicit decision making in the hope that it will become more and more automated with time.

Vision and Planning: After a strategic decision is made, nothing will happen unless I have some concrete action-steps that I can follow. That's why setting aside quality time for planning things is so important. I also include "vision" in this point because planning becomes much more powerful if it's done for medium or long-term goals. If I plan a project for three months or even a year then I can include many more cool things in it, while still being in sight of a clear finish line. I am not sure, whether a focus week is the best way to practice this, but since the topic is so important, I just mention it here.

Order and Finishing small projects: This one is also very important and fits perfectly to be the end spot in the list. I enjoy creative chaos as much as I like some order and having one week per quarter year where I really clean up, sort through things, tie loose ends, and absolutely don't start anything new (unless it takes less than, say, thirty minutes) seems like a perfect compromise. This is also a great ime to decide what to do with projects that have been stagnating for a while. Do I want to finish something (and, if yes, to what degree?) or just archive it? Also it's great to have a "clean house" before starting off with new plans for a new era of my life...

Finally, there are some other virtues or focus which do not fit well with the weekly rotation, but I want to mention them anyways:
  • Most importantly for me, there is reading text books and writing my blog (often inspired by what I have read). I try to read some pages of a book every week and write a blog post for at least every book that I've read.
  • Next, there is learning new skills.
  • And finally, there is finding role models, mentors, and advisers and spending quality time with them.
While reading and writing works well since I resolved to do it more regularly about a year ago, the other two items need some more thinking about how I'll go about them. But this post is long enough already, isn't it? Let me know in the comments if you are inspired by this or not. ;-)