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

So, let's imagine a normal human being.

And let's call him Bill.

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!

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, unwraped it from a Candy Erapper 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`v 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.

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.

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.

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…

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

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.

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.



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.

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

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, unmunched 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.

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.



Bill is still very hungry, he is starving. You have to feed him. Here. In comments.