📜 ⬆️ ⬇️

Wonderful introduction to programming

Good day Habr.

I would like to present to the public the translation of one wonderful article, which describes the basic principles of programming. A few words about - why is all this actually and who needs it? The answer is - the last few months I, a novice programmer myself, have been actively trying to train children from other fields. In this difficult work, I have to wool the Internet in search of interesting materials in the first place, in order to break their stereotypes about the fact that the code is boring and tedious. To my deep regret, there are not so many such materials. I am sure there are a lot of newbies who regularly read Habr and this article will be extremely interesting and useful for them.

So the original is here , by Song Zheng.

Basics of programming


Part 1. Simple code examples.

Perhaps you think programming is very difficult. But it is not. In reality, each of us has programming skills. They did not expect?
Let's imagine that we are driving and a STOP sign appears in front of us. And what will you do? You stop!
')
Deep in the depths of our brain, in the neuronal plexuses are programmed (On Ruby) some such commands:
if self.saw(stopSign) self.stopCar() end 

To make it easier for you to understand the meaning of the word "self", you can safely replace it with "I", the meaning will remain the same. In general, most programming languages ​​follow the following style for their syntax:
 .() 


Parse this example.

The noun in this example is called Object. The object can do various useful actions.
The verb from the example is called Method or Function. The method contains a couple of lines of code and this code is executed every time you call it.

For example, somewhere inside your head, the method responsible for stopping the machine might look like this:
 def stopCar() self.stepOnBrakes() self.lookLeft() self.lookRight() end 


image

The “subject” in brackets is called a parameter. This is just some data that we would like to transfer to our method.

Now imagine that you are a teacher and you decide to show a share of unprecedented generosity and give all your students a piece of candy (in the original "fabulous teacher", not applicable to our latitudes). You distribute each student to Ferrero.
If you were a computer, then something like this would be executed in your head:
 for e in students self.giveChocolateTo( e ) end 

In this code, instead of "e", each of your students is substituted in turn, to whom you give a candy.

How do you think, how could you describe in the code how you play sports? For example, like this:
 while ( self.amStillAlive() ) self.keepRunning() end 

It is worth noting that 90% of logic in programming falls on the expression 'if' - to control the progress of the task and the cycles 'for' and 'while'.

Before going further, I would like to show you a couple of lines of code that you can run and see what comes of it:
 x = 0 while (x > -1) x = x+1 end 

Let's look at an example.
First, we save a value equal to 0 in the variable x . Then, for as long as x is greater than -1, we increase the value in the variable x by 1.

The expected result in this example is an infinite loop that will increase the value of the variable by one over and over again. This will cause the processor of your computer to work hard, warming you with its warm air currents. Ideal as a hot water bottle on a cold winter day =)

Knowing cycles and conditional statements allows you to start programming right now. It is worth now to learn how to work with them. You should be able to create objects whenever you need it.

Part 2. Basics.

Imagine that you are Gd in the 6th day of the creation of the world. You all worked hard for 5 days, and now you have to create millions of people in 1 day. How to implement it? Maybe you should just sit down and make people one by one? No, all you need is a layout that describes how a person should look, and then from this layout you can create as many people as you can before the end of the day.
Actually, this “layout” is one of the basic programming concepts and it is called “Class”. Every time you feel embarrassed at the mention of the word "Class" - just replace it with "Layout", it will ease your perception.

image

Let's imagine that you are a game developer and you would like to create many different characters in this game. First, you should create a class with the properties you would like to see in the object created from the class:
 class Character def initialize(nameVariable, intelligenceVariable) @name = nameVariable @intelligence = intelligenceVariable end end 

The 'initialize' method is executed each time a new object is created and endows it with the properties that you described in the base class. In the method, we set the character's name "nameVariable" and his intelligence level "intelligenceVariable" (the prefix "@" is a prefix "self" for the properties of the character). When a character is created, its properties are created. Let's create 2 characters - character “A” and character “B”:

 a = Character.new( "aperson", 10 ) b = Character.new( "bperson", 10 ) 

For character A, we set 2 values ​​for “aperson” and “10”, which become the properties of this character (because every time the object is created, the “initialize” method is executed).

As a result, we have 2 characters, one of them is called “aperson” and “bperson”, whose intelligence level is 10.

Have you noticed how easily we created the character, just describing the class? You can create objects again and again just by calling Character.new (...). For example, like this:
 x = 0; while( x < 5,000,000,000 ){ Character.new( "Adam", x ) Character.new( "Eve", x ) x = x+1; } 

In this way, 5 billion Adam and Eve were created, which gradually increased the value of intelligence. Like this:

image

Now let's imagine that each of our characters has the ability to watch the famous TV show "Jersey Shore." Since this ability applies to all characters, then it is worth indicating this in our layout class:
 class Character def initialize(nameVariable, intelligenceVariable) ... end def watchJerseyShore() @intelligence = @intelligence - 2 end end 


From this point on, every time a character uses the watchJerseyShore method, his intelligence level will decrease. For example, like this:

 a.watchJerseyShore() a.watchJerseyShore() 


Character A watched the show 2 times, respectively, the level of his intellect decreased to 6.

Perhaps this is not obvious, but the only way to somehow change the value of intelligence in a character is to call the watchJerseyShore () method. This rigidity prevents the character from changing values, in this case the character's intelligence level. The level of intelligence can not be changed accidentally, it can be changed only by intentionally calling the watchJerseyShore () method. This important concept is called Encapsulation , the idea is that access to the properties of the object is limited. For example, if once, having drunk too much, you try to execute the a.intelligence = 100000 method - you will get an error.

image

Everything else, if you go to create a class "Teenager", which will have such properties as "name", "intelligence" and who also watches the show "Jersey Show", should you blindly copy all the code that you wrote describing the character class? Not! All you need to do is inherit from the base class of its properties. If we expand the class of the Teenager with the Character class, then all the properties of the Character will be inherited by the Teenager class.

 class Teenager < Character end 


Then you can simply create a Teen object:

 teeny = Teenager.new( "teeny", 10) 


Teeny is a Teeny class object with the name “Teeny” and an intelligence level of 10. To reduce the intelligence level of our Teeny, just watch a Jersey show:
 teeny.watchJerseyShore() 

This ability is an important concept and is called Inheritance .

image

Teen grew not like everyone else and you decided to change the value of his method, which is responsible for watching the show Jersey Shour. You might change this method as follows:
 class Teenager < Character def watchJerseyShore() @intelligence = @intelligence + 2 end end 

From now on, every time a teenager watches a Jersey show, his intelligence level increases! The class of teenagers can not only inherit all the methods and properties from the base class, it can also redefine them. This flexibility in overriding methods that inherit from the base class is an important concept and is called Polymorphism .
image

All together - Encapsulation, Inheritance and Polymorphism form the basis of Object-Oriented Programming Languages. If you understand these 3 principles, then it will not be difficult for you to comprehend such languages ​​as Ruby, Java, Objective C, C #, Python and many many others.

END

I tried quite loosely to translate some parts of this wonderful article. I am sure that there is much aspiration in terms of improving the flow of thought to the masses, so judge strictly, everything will be taken into account and corrected.

Thank you for your attention, sincerely I hope that this wonderful article will help beginners in understanding the 3 cornerstones of the PLO.

Source: https://habr.com/ru/post/180513/


All Articles