OK OK, another version of it...

Tuesday, 27 January 2009 by Kate Howlett
This article is a draft for the external web site. All examples are in Java or using Java jargon.

So how is baking like programming? What are the similarities? Are there any? I think so and this article is to show you what they are.

Similarities:
  • They both have an overall plan - what should happen
  • They both have an ultimate goal
  • A time frame
  • Variables
  • Methods
  • Correct timing and interaction
  • Testing

Differences:
  • Baking has an oven period
Let me elaborate. The overall plan and ultimate goal are self-explanatory so I will start with the time frame. The time frame is important, it lets you know how long you will be working on the product.

Time Frame


With baking this time frame is more concrete if you are using tried and tested methods but depending on the chef's experience and skill and the quality of his tools and ingredients it is still open to variation. Exactly the same considerations apply to programming though the time frame is based solely on these details rather than adjusted accordingly. Of course the time frame is also often a made up figure you tell your superior to ensure you get some leisure time!

Variables


We have variables. We have ones whose value never changes and we have ones that are open for adjustment. In baking a constant variable could be, for example, the amount of flour used, I've found that it's best not to deviate from the given amount!
Eggs, sugar and butter are somewhat unchangeable though a little variation won't affect the product too much. And the variables that change would be spices, the amount of fruit or chocolate chips added.

{code}
final int flour = 200;
final int eggs = 3; // Three large eggs
int chocolateChips = 200;
// All values will be in grams unless otherwise stated.
{code}

Methods


And then we have methods, these may take the results from previous methods or they may start with all new ingredients. They are incredibly important and their order is crucial. You can't miss out step two - it just won't work just as you can't add melted chocolate to flour. Likewise the wrong variable can mess up the whole method and perhaps even the whole program.

Though the ingredients are important it is the methods that actually get us to the final product, without them we'd have a gooey mess. The methods need to have exactly the right variables and be run at the right time otherwise things will go wrong. As you can see the melt() method needs to be passed butter and it needs to return meltedButter before the butter gets passed to the cream() method. What would be more sensible would be to add some checks - if() statements - to ensure that we didn't pass null variables. Otherwise we'd have NullPointerExceptions all over the place.

{code}
public ChocolateCake extends Cake {
public ChocolateCake makeCake() {
// All int units refer to grams unless otherwise stated
long minutes = 1000*60;
public Butter butter = 43;
MeltedButter meltedButter = melt(butter);
final int flour = 230;
final int sugar = 230;
int eggs = 2; // 2 large eggs
int cocoa = 45;
int milk = 115;
int vanilla = 5;
long duration = 3*minutes;
long bakingTime = 50*minutes;
float temperature = 175;
CakeBatter batter = cream(meltedButter, flour, sugar, eggs, cocoa, milk, vanilla, duration);

ChocolateCake chocolateCake = batter.bake(temperature, bakingTime)
return chocolateCake;
}
}
{code}

Well sort of. Obviously if we were doing this properly we'd have a CakeFactory that returned Cake Objects. We'd have better inheritance and perhaps an interface. Maybe the variables would be objects that had units of quantities that we could change.

Testing


The testing. Of course people will be more eager to test the result of a cake than a program! If we consider an important occasion to be similar to deployment we can see how testing is essential to both areas. In baking for an occasion you would not make a cake that you had never made before. Just as you wouldn't just write a program and deploy it immediately would you? We have two kinds of testing in software (that I'm going to compare anyway) We have unit testing and we have integration testing.

Unit testing would be ensuring that each part works, you're testing the individual sections of code or the different steps in a recipe. Provided each of these steps goes according to plan then the final product should be fine.

But we also do integration testing. This is the testing of the final product, to make sure that each of the operations did interact correctly and ensure that we didn't leave anything out. It also tests whether third party intervention has made a difference. Though you may set the the oven to the right temperature it may not actually work properly which affects your cake. Other outside influences can affect your final program. In these situations you review what you did, work out what went wrong and where and try to fix the problems. If it still just doesn't work after multiple attempts then you consider the possibility that due to your skills/variables/tools this is simply not the right way for you to proceed and you try something else.

Broader comparisons


There are similarities beyond the individual recipe or program. There are as many different chefs as programmers. People might start producing food or code from a young age or in the comfort of their own home. Even those who specialise in the same areas may add their own personal touches or have rules that they believe everyone should follow.

There is also the concept of different specialties: some people make cakes, some make pastry, some write C, some write ruby. Despite this it is often true that once you fully grasp the process in your own area it is easier to follow the recipes of others. People who are just beginning can improve incredibly, plateau at decent or remain awful (poor guys).

People can be interested in new things, experiment and dabble in other areas. Once this playing around begins they may stay within their own area having learnt from the experience or the might decide that this new area is superior or more interesting than their own. Some people have a knack for certain subjects and become experts while still others are jacks of all trades. And of course there are the people in between.

Conclusion


I think we can see that the similarities far outweigh the differences and I hope that the next time you stare at your computer you think, even for just a moment, about cakes - I know I do. Or when you are waiting for your crumble to turn golden brown you are inspired to write some code. Or maybe you are just amused. That works for me.

Related Articles


Some articles that compare programming to other art forms.

Magic As Programming
Hackers and Painters
Posted in Labels: , , | 1 Comment »