The Cake is a Lie!
Tags: explainlikeimfive, jokes
Published: 2018-12-01 23:03:29

---
title: The Cake is a Lie!
published: true
description: Do you ever think — “How to explain programming to the one, who never faced it before”.
tags: meta, explainlikeimfive, jokes
cover_image: https://thepracticaldev.s3.amazonaws.com/i/rd58rp1gmqssqxtrikg2.jpeg
---

So, let's imagine — you have a friend, who is not _soiled_ by a computer science, never tried to automate something, never played Factorio, never wrote a single line of code.

So, let's imagine a normal human being.

And let's call him __Bill__.

![bill](https://cdn-images-1.medium.com/max/1600/1*esRUHR89un2peZ7drOdcLA.png)

He is not very good at Math, just “not good”, but __he loves candies__!

Your task is to teach Bill some _magic IT things_, you do every day. The simplest ones. Decipher the secret knowledge.

What shall you do?

# Feed him!

![Feed with a cake](https://cdn-images-1.medium.com/max/1600/1*UMLY_xPgspHUj8l1X4sfnw.jpeg)

Long story short, but one day Andrey Alexandrescu asked Bill:
>— Andrey: Hi Bill, do you want a candy?
— Bill: Yep, sure!

Then Bill got the candy, unwrapped it from a Candy __Wrapper__ and ate the __payload__.
Simple? And we have used 2 _magic words_!

===

Just few days later Wirth asked Bill:
>— Wirth: Hi Bill, do you want a candy?
— Bill: Yep! SURE!
— Wirth: Hey, I've got another one, do you want it too?
— Bill: Yep! SURE!

It was a __condition-controlled loop__, or a __cycle__.

> — Wirth: Hey, I've got another one, do you want it too?
— Bill: Yep! SURE!

... But there was no candy, and Bill got a common __null pointer exception__ instead.

---

Beware — if Alexandrescu will suddenly return and continue to feed Bill with candies — a __buffer overflow__ will occur. As long as Bill will always accept a new candy.

![buffer overflow](https://cdn-images-1.medium.com/max/1600/1*a8DoAqe6ThdY3EVUq-2SUQ.jpeg)

---

Next day Wirth was prepared. He has got a big bag of candies.
> — Wirth: Ok, let me check the bag of mine….
— Wirth: I still have a candy. Bill, do you want a candy?
— Bill: Yep! SURE!

In this case, Wirth will provide a candy only if he has one.
Here we got a __cycle with precondition__.

---

But one day Bill came to Wirth`s house, and shouted:
>— Bill: GIVE ME A CANDY!
— Wirth: Nothing lasts forever! You have eaten everything!
— Bill: But I will take it IN ANY CASE!!!
— Bill: DO YOU HAVE A CANDY???

It was a __cycle with a postcondition__. Sad story. Thanks God police were nearby.

![no food](https://cdn-images-1.medium.com/max/1600/1*KfTBl4hAMEMlJMQvJi89JA.jpeg)

---
Decades later Dan Abramov came to Bill and put all his candies on the table:
>— Dan: speechless gazing at Bill.
— Bill: speechless gazing at the first candy
— Dan: Bill, do you want one on these candies?
— Bill: Yes, sure.

It was a __collection-controlled loop__.
---
Just after it, Dan took another bag, put all candies on the table, count them, and put back to the bag.
>— Dan: Bill, look like I still have a candy. So, do you want a candy?
— Bill: Yes, sure.

This time it was a __count-controlled loop__.

![count!](https://cdn-images-1.medium.com/max/1600/1*UYezfVzAXoQ4Br7COwtl3Q.jpeg)
---
Sooner or later, but Bjarne Stroustrup came to feed Bill.
>— Bjarne: Bill, do you want a candy.
— Bill: Yep, sure.
— Bjarne: Bill, do you want a candy.
— Bill: — — —

Bill was munching a candy. He was busy and nothing can disturb him. He raised a __mutex__, the thread got locked, and the time stops…

![Thread lock](https://cdn-images-1.medium.com/max/1600/1*HdPEOj9eetz9r_GDwoeaHA.jpeg)
---
Bjarne was a very clever one, and a very polite - he found a solution:
>— Bjarne: Bill, do you want a candy.
— Bill: — — —
— Bjarne: Ok, I'll wait until yuo finish munching…

Here we got a __thread synchronization__.
---
Ok, this time we will call for Peter Norton.
> — Peter: Bill, do you want a candy.
— Bill: Yep! Sure!
— Peter: I'll provide a whole pack. Munch it by youself.

So Bill got candies, one by one, from a bag, and ate them, one by one.
This is called __LIFO__ — last in, last out. As long as the candy from the bottom of the pack will be eaten last.
At the same time, Bill's digestion is __FIFO__ — first in, first out. (Sorry, but you had to know it before).

![You had to know it before](https://cdn-images-1.medium.com/max/1600/1*YX9ijeMhcD2d1E2vTD-qqw.png)

---
Donald Knuth also joined the feeding team. 
But one day he got tired of buying new candies for Bill, and asked a catering company to do it instead.
> — Donald: Hey, my friend, can you buy the best candies in the nearest shop and present them to Bill? Here are Bill's address and the money…

Since that time Bill always has new candies from Donald.

In this case, a catering company is a __function__, and address and money are __arguments__. And a candy is a __return value__.

>If, for the same address and the same amount of money, Bill will always get the same candies — it will be called a __pure function__.

![minion!](https://cdn-images-1.medium.com/max/1600/1*FUDllp1SIMbDUl98pn7VOQ.jpeg)

But Dennis Ritchie was even clever!
>— Dennis: Hi Bill, do you want a candy?
— Bill: You know it!
— Dennis: So here is the address. You will found your, so desired, candy there.

It was a __pointer__.

But, when Bill reached that address, he found a secret military object. Anyway — he got an address and tried to reach his target at all costs, but, you know, security is security.
![violation](https://cdn-images-1.medium.com/max/1600/1*FNg5NUuHvpsAlCv0dxWTBw.png)

A few months later, after escaping from a jail, he tried to repeat the attempt, but yet again was not able to reach “the target”. Something stopping him at the distance — invisible, and indestructible walls of __bounds checking__.

---

But one day…. Even, to say the truth — night — Cyberdemon came to Bill's house and shout:
>— Cyberdemon: DO YOU WANT A CANDY?
— Bill: YES! AT THE DAYTIME, AND THEN THE STARS SHINE! ALWAYS!

So, Bill has got his candies yet again. But this is a bit sad story, as long as he forgot to check the type of payload or perform any other __data validation__. ☠️ Don't trust Daemons.

![fire!](https://cdn-images-1.medium.com/max/1600/1*1Y2a8P7rK4sOSF00kL8COQ.jpeg)

---
After that Bill woke up in a void, and hear:
>— Cosmos: Bill! Do you want a candy?
— Bill: Yes!
— Cosmos: Bill! Do you want a candy?
— Bill: Yes!
— Cosmos: Bill! Do you want a candy?
— Bill: Yes!

This is how __ping__ works.
---

In the distributed systems with non-zero __latency__ this might look so:
>— Cosmos: Bill! Do you want a candy?
— Cosmos: Bill! Do you want a candy?
— Cosmos: Bill! Do you want a candy?
— Bill: Yes!
— Bill: Yes!
— Bill: Yes!

But this case valid only for TCP/__UDP__. In case of TCP/__IP__ it might change
>— Cosmos: Bill! Do you want a candy? Bill! Do you want a candy?
— Cosmos: Bill! Do you want a candy?
— Bill: Yes! Yes! Yes!

As long sibling packets will be joined by time or size factor(__MTU__).

![ping](https://cdn-images-1.medium.com/max/1600/1*P0hnvFZhcMew_dvQuLwICQ.jpeg)

# The End
I have many yet untold stories, like a Bill and __Gang-of-Four__, or __non-cross-browser candy__.
But let me told the last one, about The Indivisible Candy:
Bill eaten it hundreds of times, but it zaps back to his hand, _un-munched_ and even still sweet!
So `Indivisible candy` may be a __singleton__ or a __memory leak__, as long garbage collector can't digest it. As well as Bill.

![Have a good time, Bill](https://cdn-images-1.medium.com/max/1600/1*mIjltkQfakAxh4SZHP5d3w.jpeg)

So, __can you describe something from IT__? From abstract computer world using a candy, a cake or something else…. 

> Original article “The Val's Candies” was released 8 years ago, and since that time it got a lot of new examples as user comments — recursion, delegation, interfaces, stack overflow, procedures vs functions, classes, B-trees, and so on. But... in another language and another dimension.


![foood](https://cdn-images-1.medium.com/max/1600/1*RCd0hPQUkAbtHutD3jM2VA.png)
Bill is still very hungry, he is starving. __You have to feed him. Here. In comments.__