The Cake is a Lie!

Written by antonkorzunov | Published 2017/07/26
Tech Story Tags: programming | learning-to-code

TLDRvia the TL;DR App

Do you ever think — “How to explain programming to the one, who never faced it before”.

Original articles was created long time ago, in another galaxy. This is a just a translation trial, and it did not contain as much jokes, as it can.

So, lets imagine — you have a friend, who is not soiled by computer science, never try to automate something, never play factorio, never write a single line of code.

So, lets imagine a normal human being.

And lets call him Bill.

He is not very good in Math, even just “not good”, but he loves candies!

Your task is to teach Bill some magic IT things, you do every day. The simplest ones. Encipher the secret knowledge.

What shall you do?

Feed him!

Long story short, but one day Andrey Alexandrescu asking Bill:

— Andrey: Hi Bill, do you want a candy?

— Bill: Yep, sure!

Then Bill will get the candy, unwrap it from Candy wrapper and eat the payload.

Simple? And we have used 2 magic words!

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Just few day later Wirth asking Bill:

— Wirth: Hi Bill, do you want a candy?

— Bill: Yep! SURE!

— Wirth: Hey, I`v got another one, do you want it do?

— Bill: Yep! SURE!

It was a condition-controlled loop, or a cycle.

— Wirth: Hey, I`v got another one, do you want it do?

— Bill: Yep! SURE!

But there was no candy, and Bill got a common null pointer exception.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Beware — if Alexandrescu will suddenly returns and continues to feed Bill with candies — a buffer overflow will occur. At long Bill will always accept a new candy.

Buffer overflow

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Next day Wirth was prepared. He has got a big bag of candies.

— Wirth: Ok, let me check 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 hot a cycle with precondition.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

But one day Bill come to Wirth`s house, and house:

— 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 postcondition. Sad story. Thanks god police were nearby. Be careful next time, and do not feed wild birds.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Decades later Dan Abramov come to Bill and put all his candies on the table:

— Dan: speechless gazing at Bill.

— Bill: speechless gazing at a first candy

— Dan: Bill, do you want one on these candies?

— Bill: Yes, sure.

It was a collection-controlled loop.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Just after it, Dan got 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. Do you want a candy?

— Bill: Yes, sure.

It was a count-controlled loop.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Sooner or later, but Bjarne_Stroustrup come to feed Bill.

— Bjarne: Bill, do you have 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 raise a mutex, thread got locked and time stops…

Thread lock.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Bjarne was a very clever one, and he found a solution:

— Bjarne: Bill, do you want a candy.

— Bill: — — —

— Bjarne: Ok, I`ll wait until for 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.

Next Bill will get candies, one by one, from a bag, and eat them, one by one.

This is called LIFO — last it, last out. As long the candy from the bottom of the pack will be eaten last.

In the same time Bill`s digestion is a FIFO — first in, first out.

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 he asks a catering company to do it.

— Donald: Hey, my friend, can you buy best candies in the nearest shop and present them to Bill? Here is Bill`s address and money…

Since that time Bill always has new candies from Donald.

In this case 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 pure function.

But only in case the company will only transfer candies from point A to point B, and shop will always be non-pure function, as long it loses candies.

But Dennis_Ritchie was even clever!

— Dennis: Hi Bill, do you want a candy? — Bill: You know it! — Dennis: So here is an address. You will found your so desired candy there.

It was a pointer.

But, then Bill reaches that address he found there a military object. Any way — he got an address, and tries to reach his target at all costs, but security is security.

Access violation.

A few months later, after escaping from a jail, he tried to repeat, but yet again cant 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 comes to Bill`s house and shout:

— Cyberdemon:DO YOU WANT A CANDY?

— Bill: YES! AT THE DAY TIME, AND THEN THE STARS SHINE! ALWAYS!

So, Bill has got his candies yet again. But this is a bit sad story, as long he forget to check type of payload or perform any other data validation. ☠️

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

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 how ping works.

In the disturbed 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 a lot of untold stories, like a Bill and Gang-of-Four, or non-crossbrowser candy.

But let me told the last one, about The Indivisible Candy:

Bill eats it hundreds of times, but it zap back to his hand, unmunched and even still sweet!

So Indivisible candy may be a singleton or a memory leak, as long garbage collector cant digest it.

Have a good time, Bill.

Original article “The Val`s Candies” was released 7 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.

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

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


Published by HackerNoon on 2017/07/26