Assuming programming structure, choose your own adventure!

Programming structures are inherently the same and often very easy to understand if you are taught what to look for. When attempting to understand programming when you've never seen it before it can often look like gibberish. However there is a certain pattern that you can look for.

As an example a lot of programming languages surround their first line of code with brackets. Then they house the main functioning portion of their code inside of it.

public void myFunction() {  
    Console.WriteLine("I'm writing words to a console.");
}

C#  

This returns the line I'm writing words to a console. in a console window.

int main() {  
    int a = 5;
    int b = 6;
    cout << 5 + 6;
    return 0;
}

C++  

This returns the line 11 in a console window.

The reality of the structure of code is almost as if each time you see a bracket it runs the indented code. However this is only true if a statement inside of the code is either true or false. So we can assume if a variable called iAmSecond has a value of 5 and we compare the variable to a value of 5 then that statement is true. If that statement is true then it should run the code.

public void iAmFirst() { // <--- First  
    int iAmSecond = 5;
    if (iAmSecond == 5) { // <--- Second
         string iAmThird = "I'm the third line";
         Console.WriteLine(iAmThird); // <---- Third
    }
}

C#  

Sometimes though code can jump around. That doesn't mean that the order in which a program runs code changes.

Remember those books you would read as a child and you would choose your own adventure and you would have to go to that specific page to keep reading? Well that's exactly how programming structure works.

// The program starts with this function here.
public void myFunction() {  
    Console.WriteLine("Hello");
    myNextFunction();
}

public void myNextFunction() {  
    Console.WriteLine("I am writing");
    myOtherFunction();
}

public void myOtherFunction() {  
   Console.WriteLine("in multiple functions.");
}

Console Output:

Hello  
I am writing  
in multiple functions.  

What can we assume about the code above us? Well the code above us is going from one function to another. However it originally began in myFunction. From my myFunction it goes to myNextFunction() and then it goes to another function called myOtherFunction(). It's almost as if the program is following a very specific set of instructions, which it is.

Does this statement still hold true when I switch around the functions? Yes.

// The program starts with this function.
public void myFunction() {  
    Console.WriteLine("Hello");
    myOtherFunction();
}

public void myNextFunction() {  
    Console.WriteLine("I am writing");
}

public void myOtherFunction() {  
   Console.WriteLine("in multiple functions.");
   myNextFunction();
}

Console Output:

Hello  
in multiple functions.  
I am writing  

So the order in which we are sending console messages changes but we only switched a few things around in the code. Does that mean the programming is hard to follow? No. You just have to read it like a specific set of instructions.

If we start at myFunction() and it writes a line of code that says Hello then it goes to another function called myOtherFunction(). Then inside of that function is prints out another line of code and then moves to another function.

So what can you learn from this if you're new to programming?

  • Read the code and follow the paper trail.
  • Functions may cause code to jump around but it all begins from the same place.
  • Programming languages like C#, C++, Java, and Javascript use brackets to house their functionality.
  • Look for indents in code to see where the code is headed next.