Baking and Programming?

Thursday, 30 October 2008 by Kate Howlett
Is baking like programming?
Update: Um updated version.

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.

h2. 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!

h2. 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}

h2. 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 and you can't add melted chocolate to flour for the same reason. 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 {color:red}NullPointerException{color}s 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)
}
}

{code}

Well sort of. Obviously we'd have a CakeFactory that passed back Cake Objects if we were going to do this properly. We'd have better inheritance and perhaps some interfaces. Maybe the variables would be objects that had units of quantities that we could change.

h2. 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 correct recipe for you and you try something else.

h2. 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.

h2. 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.

h2. Related Articles

Some articles that compare programming to other art forms.

http://www.terminally-incoherent.com/blog/2008/10/29/magic-as-programming/
http://paulgraham.com/hp.html
Posted in Labels: , , | 0 Comments »

No comments: