The Cake is a Lie
Have you ever thought — How to explain programming to the one never faced it before? It could be a problem, as long a new one will not understand you.
So, let’s imagine — you have a friend, who is not soiled by computer science, never tried to automate something, never played factorio, never written a single line of code.
So, let’s imagine a normal human being.
And let’s call him Bill. He is not very good in Maths, just «not good», but he loves candies!
Your task is to teach Bill some basic (or magic) IT things, you are doing every day. The simplest ones.
So what shall you do first? Basically — 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, unwrapped it from Candy wrapper and ate the payload.
Simple? And we have used 2 magic words!
Just a 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!
This is 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.
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 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 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. Thank god police were nearby. Be careful next time, and do not feed wild birds (and Bill).
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.
— Dan: Here you are.
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 candy. Do you want a candy?
— Bill: Yes, sure.
— Dan: Here you are.
It was a count-controlled loop.
Sooner or later, but Bjarne Stroustrup comes to feed Bill.
— Bjarne: Bill, do you want a candy.
— Bill: Yep, sure.
— Bjarne: Here it is.
— Bjarne: Bill, do you want a candy.
— Bill: — — —
Bill was munching a candy. He was busy and nothing can disturb him. He have raised a mutex, the thread got locked and time stops…
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 yourself.
Next Bill will get candies, one by one, from a bag, and eat them, one by one.
This is called LIFO — Last it, First out. As long the candy from the bottom of the pack will be eaten last.
At 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 the best candies in the nearest shop and present them to Bill? Here are Bill`s address and 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 only in case the company will only transfer candies from point A to point B, and a shop will always be a 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 reached that address he found only a secret military object. Nobody saw him since then.
A few months later, after escaping from jail, he tried to repeat, but yet again can’t reach «the target». This time something was 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 DAYTIME, AND THEN THE STARS SHINE! ALWAYS!
So, Bill has got his candies yet again. But this is a bit sad story, as long he forgets to check the 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 the case of TCP/IP it might be
— 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 (aka MTU).
I have a lot of 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 eats 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.
So, can you describe something from IT in a candy style? To feed teach Bill, and get him in IT.
Bill is still very hungry, he is starving. You have to feed him. Here. In comments.
PS: This is a translation of 10 years old article — https://habr.com/en/post/109076/. 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 dimension.