organized-connected-boxes

Is Code Organization Really Important In Programming?

Can computer code be organized? If so, is code organization important in writing a computer program? 

Let’s take a look at how we organize our routines.

When we do an activity, we usually have a name for it: laundry, dishes, work, school etc. Each of these terms conjures up a picture in your mind of all the steps involved.

Take laundry, for instance. It is a finite procedure that has a definitive start and end.

  1. Collect the dirty laundry.
  2. Sort it by colour.
  3. Put one colour type in the washer.
  4. Add soap.
  5. Add softener (optional!).
  6. Select the appropriate washer setting(s).
  7. Start washer.
  8. Wait until done.
  9. Remove clean clothes from the washer.

Repeat the above steps until done.

Remember: we haven’t finished the laundry. There’s another set of instructions for handling the wet clothes. Funny, how one word generated nine+ steps!

We can apply the same concept to organizing computer code.

One word or short phrase can execute multiple commands or functions. That's how coders organize software! Share on X

Code Organization in Different Languages

Most programming languages use specific structures to organize code. Each language has a different name for them: procedures, functions, or methods. 

Python and C call them functions. Java calls them methods, and Turing has both procedures and functions.

Every language also has different formatting parameters to indicate the end of the structure.

Python’s functions rely on indentation:

python-code-organization

Java’s methods and C/C++ functions use braces:

java-code-organization
cplusplus-code-organization

While Turing uses end as the keyword:

turing-code-organization

If a coder doesn’t follow the correct structure rules, the program either won’t compile, or it won’t work correctly (Python).

Although Python is the only language that relies on indentation, coding convention dictates that all of the others be indented as well. (A convention means that this is something that has become an unwritten rule that coders follow to make coding more uniform and easy to read.)

Turing has a really nice feature that the other languages lack. Its programming interface uses <F2> to indent the code correctly for the coder. This is unbelievably useful when trying to track down errors!

When the code doesn’t indent correctly, this can indicate that the coder has missed something – like an end statement:

turing-missing-end-statement

The code actually cascades to the right to make errors easier to see.

Turing Code Organization

Unlike the other programming languages, Turing uses the keywords “procedure” and “function” to differentiate between two organizational purposes.

A Turing procedure executes its code from top to bottom and stops execution when it reaches the end statement:

turing-top-down-code-organization

Whereas, a Turing function requires at least one result statement to exit the structure. The purpose of a function is to send a result back to the location where it’s called:

turing-function-procedure

Think of a calculator. If you enter a value and press the square root key, the calculator returns the square root of that value.

Turing works the same way. The function returns a value back to where it was called. It can return anything: integer, real, string, array, or Boolean.

Why Do We Use Code Organization?

Let’s revisit the laundry example. I’m sure the first time you did your laundry, you had to find out what all the steps were by either you observing someone, asking someone or doing an online search. After a couple of loads, you probably had the process memorized pretty well.

So, when someone says “do the laundry,” you already know all the steps. You don’t have to relearn them. You just do them. Hopefully, in the correct order!

That’s why coders organize code into functions/procedures/methods. 

When they want the program to repeat a specific series of commands, they code them once, and then reuse the code by calling it when they need to.

If a segment of code repeats more than once, it should probably be in its own structure. Coders call this KISS!

Check out these beginner resources:

coding-for-beginners-computer-corner