Showing posts with label cake. Show all posts
Showing posts with label cake. Show all posts

Thursday, 14 May 2020

How to Debug a Cake

So, I've been baking cakes for a long time. I got the recipe from my mum, who has also been baking cakes for a long time.

This recipe is basically flower, eggs, butter, bakingpowder, and lots of sugar. You know, the old fashioned way.

Well, a couple of years ago, suddenly my cakes started failing mysteriously.

So I started trying to fix this, by changing stuff randomly. I started downloading recipes from the Internet, to see if they worked better. I bought a cake-flour package from the supermarket, perhaps that worked. I even changed ovens.

None of these things worked.

So, I sat down, and thought about it. And then I realised, I didn't try and find the problem as I would have if I looked at the problem as a bloody Software Designer (which is what I am).

Well, debugging a cake turns out to be hard. System.out.println or logging or breakpoints are quite impossible.

As they say, you have to find the problem during actual production.

Some conclusions

If you did not make any changes to your recipe, and suddenly your cakes start to fail, either one of your ingredients has changed or your process/tools for making the cake. This is an obvious conclusion.

I was going to assume that my tools and process were more or less the same.


The only thing that really works, is to change one ingredient at a time.


In other words:

“How often have I said to you that when you have eliminated the impossible, whatever remains, however improbable, must be the truth? ”- Sherlock Holmes, The Sign of the Four (1890)

It also helps to understand the process. What kind of ingredients in a cake are essential and what kind of role do they play?

Addendum

Well, after a lot of experimenting, and, quite frankly, a lot of dismal ruined cakes, I found the cause to be the butter.

The butter I used, Blue Band, must have changed at the time. So I did a little digging.

I noticed that there are no longer any dairy products in this butter.

And when I use butter made from dairy products, the cakes turn out fine.

Also, when I started checking the supermarket, I did notice that Blue Band also (at around the same time) came out with a new product called "Butter for cookies and cake". I checked, and it did indeed contain dairy products.

Am I just being paranoid in thinking this is not a coincidence?

Thursday, 14 December 2017

The Dangers of Optional.orElse

Our architect at work explained how to properly use the Optional class, and sometimes it is not easy. I shall explain one of the intricaties in this blog with the aid of Cake, because who doesn't love cake?

Now some people tell me that the cake is a lie1 2. Now, this may or may not be the case. So there may or there may not be cake.

This is basically the definition of the Optional2 class in Java 8.

Optional<Cake> cake;

One of my colleagues is a fan of Eddie Izzard4 5.

Our architect at work presented us the code he encountered of the Optional.orElse. I've changed it a bit by adding more cake.

If you run this program, you'll notice that after you have received a nice cake, you immediately die!

This is due to the fact that the expression in the .orElse is immediately evaluated after the new Cake(). This is very basic Java and what is to be expected.

Unfortunately, we software designers seem to have a blind spot, when it comes to the orElse() method. We automatically compare it to the if-else construction we know and love, and then we assume the behaviour is the same.

It is as if your brain automatically shunts over to the wrong abstraction.

The .orElse() is actually only suitable for constants.

Conclusion

In order to fix the problem, you need to use a lambda. To use a lambda, you need to use a different method of the Optional class, namely .orElseGet().

The code would look as follows:

    cake.orElseGet(this::death);

I had really hoped, that they would have changed the method name to something better. Some notable good examples would have been:

  • "orElseConstant"
  • "orDefault"

References

[1] Know Your Memes - The Cake is a Lie!
http://knowyourmeme.com/memes/the-cake-is-a-lie
[2] Wikipedia - Portal (video game)
https://en.wikipedia.org/wiki/Portal_(video_game)
[3] Oracle Javadoc - Optional
https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html
[4] Wikipedia - Eddie Izzard
https://en.wikipedia.org/wiki/Eddie_Izzard
[5] Youtube - Eddie izzard-cake or death
https://www.youtube.com/watch?v=BNjcuZ-LiSY

Thursday, 16 November 2017

On Broken Window Theory and Cake

Occasionally, among my varied interests, I enjoy cooking. I find that it is one of mankind's most interesting practical applications of Alchemy, but that is a Blogpost for another day. I recently baked two cakes for my colleagues at work. I had no special reason for doing it besides the joy of cooking.

In this case it provided a perfect example of the Broken Window Theory1.

I put the two cakes on the counter of the cafeteria, as soon as I came in that morning. Two hours later, I noticed that both cakes were still in one piece.

My colleague and I surmised, that this was because the cake was still undamaged. To test this, we both took a piece of cake. With two hours all but a small bit of one cake remained and at the end of the day, the second cake suffered a similar fate.

Note: interestingly enough even a perfect cake will suffer, if it sits next to a cake that is already in the process of being eaten.

On code

Of course, a similar conclusion can be drawn from your code. If your code is a work of beauty, naturally people who need to make changes, will do so with respect for the beauty of the code.

With code that is already showing wear and tear, or code that has originally not been well designed, this process of entropy is increased dramatically.

It is in many ways similar to the design principles of the Boyscout Rule2 of Uncle Bob.

References

[1] Wikipedia - Broken Window Theory
https://en.wikipedia.org/wiki/Broken_windows_theory
[2] O'Reilly Commons - The Boy Scout Rule
http://programmer.97things.oreilly.com/wiki/index.php/The_Boy_Scout_Rule