Você está na página 1de 6

FUNDAMENTALS OF CODING

SONG ZHENG | MAY 14, 2013 | 12 COMMENTS


Truth about coding Part 1: Simple Codes Why is coding so difficult? Its not. In fact, everyone already knows how to code. Surprise! Lets say you are driving and you approach a STOP sign. You stop. Deep inside your brain embedded in the many neurons, these lines of code (in Ruby) were executed:
if self.saw(stopSign) self.stopCar() end

If the word self does not make sense, replace it with me, or I. Same meaning. Generally, most programming styles follow the syntax: noun.verb(item) Breaking it down: The noun is called object. Nouns can do stuff. The verb is called a method or function. Methods contains a few lines of code and whenever you call the method, the code executes. For example, somewhere in your brain the method stopCar is probably defined like this:
def stopCar() self.stepOnBrakes() self.lookLeft() self.lookRight() end

The item inside the parenthesis is called a parameter. Its simply data that we can pass into the method. Now lets say you are a fabulous teacher and you decide to give all your students chocolates. You approach each student and give him/her a golden Ferrero Rocher. If you were a computer, your brain probably executed code that looks like this:
for e in students self.giveChocolateTo( e ) end

Here, e refers to the student you are handing the chocolate to. What sort of code would your mind be executing while you exercise?
while ( self.amStillAlive() ) self.keepRunning() end

Heres a piece of good news. 90% of programming logic is comprised of if statements and for and while loops. This statistic is made up, but it makes sense. Before we move on, heres a few lines of code that you can try out right away and see results.
x = 0 while (x > -1) x = x+1 end

First we store 0 into a variable called x. While x is greater than -1, we increment x by 1. The desired result here is that the while loop will execute infinitely and x gets larger and larger. This causes CPU to overwork and generate a warm wave of heat from your computer, perfectly useful as a personal heater on a cold winter day. Just knowing if statements, for loops, and while loops, you can start coding right away. However, whats the point of coding if you dont do it right? You should be creating objects whenever you can (read on!)

Part 2: The Foundations Imagine that you were God and it was the 6th day of creation. After working so hard for the first 5 days, how do you create millions of people in one day? Do you simple sit down and make them individually, one by one? No. What you need is a blueprint of how a person should look like, and then create as many of them as you can before the day ends. This blueprint is a very fundamental concept in programming, it is called a class. Whene ver you get confused with the word class, just replace it with blueprint.

Lets say you are a game developer youd like to build a game with many characters in it. You will first create a class, and what properties of the object needs to be set when the object is created:
class Character def initialize(nameVariable, intelligenceVariable) @name = nameVariable @intelligence = intelligenceVariable end end

initialize gets executed every time we create a new character from this c lass. In the method, we are setting the characters name and intelligence level (the prefix @ refers to the characters property, like self). When the character is created, its properties is set to whatever we pass into the method. Lets create two characters, a and b.
a = Character.new( "aperson", 10 ) b = Character.new( "bperson", 10 )

For a, we are passing input values aperson and 10, which will get set as the characters property (because initialize method runs whenever an object is created). As a result, character a will have a name aperson and intelligence value of 10, and character b will have a name bperson and intelligence value of 10. See how easy it was to create a character once we define a class? I can create many more by calling Character.new() over and over again.
x = 0; while( x < 5,000,000,000 ){ Character.new( "Adam", x ) Character.new( "Eve", x ) x = x+1; }

And just like that, 5 billion Adams and Eves are created with progressively increasing intelligence.

Now lets say in the game every character has the ability to watch a very famous TV show, Jersey Shore. Since this applies to every person, we should write this in the blueprint:
class Character def initialize(nameVariable, intelligenceVariable) ... end

def watchJerseyShore() @intelligence = @intelligence - 2 end end

Now, every time a character executes the watchJerseyShore method, their intelligence level will decrease. For example,
a.watchJerseyShore() a.watchJerseyShore()

Character a just watched Jersey Shore two times, so his intelligence is now 6. Its not obvious, but note that the only way to change a characters intelligence is to call the method watchJerseyShore(). This rigidity protects the integrity of the characters properties and prevents you or other developers from changing the characters intelligence level in ways that it was not intended to. This important concept, called ENCAPSULATION, is the idea that access to certain object properties should be restricted. For instance, if one day you were drunk and tried to execute a.intelligence = 100000, you will get an error!

In addition to the Character class, lets say you now want to build a Teenager class that has name, intelligence, and also watch Jersey Shore, should you simply copy/paste everything from Character class into Teenager class? No. All you have to do is get Teenager class to extend the Character class. And all the Character properties and methods will be inherited by your Teenager class.
class Teenager < Character end

To create a teenager object, its as simple as


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

Teeny is a Teenager object with the name teeny and intelligence of 10. To reduce teenys intelligence level, simply call watchJerseyShore()
teeny.watchJerseyShore()

This ability to inherit properties is an important concept called INHERITANCE!

Since teenagers grow up differently from the rest of the human species, you decide to change their default watchJerseyShore() method. Can you do this? Yes.
class Teenager < Character def watchJerseyShore() @intelligence = @intelligence + 2 end end

Now, every time a teenager watches Jersey Shore, their intelligence increases! Teenager class not only inherits all of the Characters properties and methods, it can also override methods! This flexibility for overriding methods that objects inherit is a very important concept called POLYMORPHISM.

Together ENCAPSULATION, INHERITANCE, and POLYMORPHISM make up the foundation of Object Oriented languages. If you understand these 3 concepts, you will now be able to easily pick up Ruby, Java, Objective C, C#, Python, and many other languages. This marks the end of our first post. Subscribe to this blog if you want to be notified when new content comes out. Now, get started on your first Ruby lesson, and write some useful code! by Song Zheng, code master, and Joanne Daudier, hacker wannabe. Illustrations by the talented David Nguyen.

Você também pode gostar