Trust me, this is ground zero for making anything you want, from scratch!
Over three decades ago, kids like me were learning this easy programming language called BASIC. As a child, I can clearly remember screwing with the computers on display at a large department store. As you can imagine, I was very pleased with my skills.
10 PRINT “I AM COOL!”
20 GOTO 10
What does this do? It creates a non-stop loop that lasts forever, which takes over the computer, rendering it useless. And the screen would look something like this:
I AM COOL!
I AM COOL!
I AM COOL!
What is going on!? Way back then, if you worked at that store and saw this, you might think the computer was broken because there was no way to stop this thing. You had to be a computer expert to stop this coolness with a secret keyboard shortcut: CONTROL-BREAK!
Why am I telling you this dumb story? Because that childish “infinite loop” is the foundation of any powerful application you want to create. For example, if you want to compete with Google.com, all you need is a loop like this:
10 DOWNLOAD THE INTERNET
20 MIKEY, WANT ME TO FIND SOMETHING?
30 GOTO 10
That’s how you do it. Really. I was actually stuck on a boat with the guy who coded Bing. Yes, I got to quiz him for a whole hour. The important thing to understand here, the “loop” is extremely powerful!
Trust the loop. This is the circle of LIFE! As sure as the sun will rise, infinite loops will help you make the app of your dreams, for your brilliant startup!
An important concept with loops is loops within loops. In software development, a loop within a loop is called a “nested loop.” The best analogy I can think of is the Moon looping around the Earth, which is looping around the Sun. A loop within a loop. Sounds loopy but I’m serious. Essentially, this is how all of your software works.
In my example here, if the Sun is an operating system like Linux, your web server (probably NginX or Apache) is a planet and moons are your individual applications, like WordPress.
The brain of your computer, your smartphone, laptop or whatever device, is the CPU. For the sake of learning here, the CPU can only do one thing at a time. Your CPU is following our instructions, in a loop.
Here’s another analogy. Imagine a record spinning on a DJ’s turntable. Your CPU is like a needle reading the song (your application) turning in a circle from start to finish, in a loop. Likewise, all software has a beginning, middle and ending, just like a song. As a programmer, you’re writing the code, the instructions, the “song” that your CPU will read from start to finish. You can call these instructions a “program” or an “application” or an “app” for short.
On your screen, you read your code (your program) from start to finish, top to bottom, like a book. All these instructions go into a plain text file. Thankfully, you’re not literally reading your loops of instructions in a circle. Or you might get dizzy.
If your program has no loops, and it’s just a list of instructions, that’s often called a “script.” Like a script that actors would read. Also note, a script is considered a program, but a program isn’t necessarily a script.
Also worth noting, a “program” is an “application” and the words are more or less interchangeable, but an application is typically larger and more sophisticated than a program. Or you can sound like a hipster and call everything an “app.”
Back to the DJ and “song” analogy, the beginning of a web page typically holds the navigation menus and advertising, the middle of a web page typically holds the content, and the end of a web page typically has some kind of Copyright notice. Your server’s CPU is reading that code in order, from start to finish.
LOOP LINUX // Your operating system | LOOP NGINX // Your web server | | LOOP PHP // Your programming language | | | LOOP WORDPRESS // Your blog software | | | | DISPLAY HEADER.PHP // Navigation, ads? | | | | DISPLAY SINGLE.PHP // Say, "I'm Cool!" | | | | DISPLAY FOOTER.PHP // (C)opyright 2016 | | | WORDPRESS IS DONE | | PHP IS DONE | NGINX IS DONE LINUX IS DONE // Let's do it again!What language is this? It’s “pseudocode” which is English shorthand that vaguely resembles programming code. Pseudocode is great in this context, because we don’t want to get hung up on technical details. We just want the basic big picture. This is how a typical WordPress website works. As you can see, it’s lots of loops within loops, looping and looping. As long as you pay your hosting bill, this thing continues looping.
In this example, the inner loop only happens once. Often with web pages, you want quick, short loops because people are in a hurry. Too many loops and people will be wondering what’s taking so long!
Also notice the indentation. Every time you “nest” a loop within a loop, you should indent. That helps make the code more readable.
When would we have more than one loop? Imagine you want to create a chat app, which allows you to chat for hours and hours. Think of a loop that’s waiting for you and your friend to type something, looping over and over, just waiting for someone to break the ice. When someone finally says something, your app executes the instructions you gave it. Your app noticed something happened, somebody said something, the silence was broken, something changed. The “condition” of your app changed.
Your app might then execute a “conditional” loop. We’ll get into conditionals later. Basically, when conditions change, your app behaves the way you instruct it to. For example, your chat app might save what someone said, setting a conditional loop into motion. In any case, your chat app might loop over and over again for the length of your conversation, unlike our previous example. The chat server is always waiting for you to type something, because a loop is there waiting in anticipation.
Building software applications from scratch, you just want something that works, no matter how simple. Your application might just say, “I need to do X.” Without actually doing X. Personally, my code has notes in it like this:
// TODO: Add this cool feature
// TODO: This loop could be faster
To find what you need “to do” press CONTROL-F to FIND your various TODOs. These notes are called “comments” and don’t display anything on the screen. They’re pretty much invisible to the outside world. You can write…
// I hate lima beans!
It’s just you and the computer. Nobody is listening. Have fun! Remember, you’re the author of this thing. You can do whatever you want.
The important thing is you need to make the compiler or interpreter happy. The compiler tells you when you have an error in your code that the computer can’t understand, this is called a “syntax error.”
If someone looks over your shoulder and says, “You’re the worst programmer I’ve ever seen.” Then you should give up! Haha. Only kidding!
Your code will be terrible at first. Just remember, your code is a means to an end. If you get too hung up on the craftsmanship, you won’t get anything done. Of course you want to write clean, beautiful code, but remember what you’re trying to accomplish.
Your personal style will change over time. I suppose different companies follow different conventions. In my opinion, the important thing is that your code is “readable.” You need to be able to revist your code next week, next month or next year and have it make sense. You want to spell out everything explicitly, writing descriptive comments all over your code, because you will forget how the complicated parts work. When you’re starting out, don’t worry about this too much. This will come naturally in time.
You are going to make stupid mistakes. But holy cow, if you get stuck: All the answers are on StackExchange! I’m not kidding. Sometimes they will even write the code for you. For payment, all they want is reputation points. You can learn a lot from the generous people on StackExchange. It’s a powerful resource. Learn to use it, grasshopper.
Getting to the point of printing “I AM COOL!” is a big deal, because that means your server architecture is more or less working properly. Then your application grows in complexity, one little change at a time, one line of code at a time. Once you build some momentum, it’s amazing what you can create in a very short time.
The wonderful thing about building your own software from scratch, everything comes together very quickly. Because you’re making all the decisions yourself and communicating with yourself, at the speed of thought. You’re not asking or waiting for permission to do something.
And when it’s your code, it’s easier to fix, because you know how you think! You’re never guessing what somebody else was thinking. And you can figure out your code wherever you are, in your imagination–while brushing your teeth, walking down the street, anywhere. Like one time I coded an ad server on a napkin at a diner. With mobile phones this is even easier. Right now, I’m writing this on my phone at IKEA. If you have Android, I recommend DroidEdit for writing code on the go.
Ideally, once your app is making money and becomes cumbersome to maintain yourself, you “exit” with millions of dollars in stock options and find yourself a beach, a pina colada and live the dream ;-) Then some poor computer science graduates take over and refactor your code the “proper” way, cursing at you every day, even though they have a nice salary because you’re such a great programmer!
Ready for Part 3?
Questions or comments? I keep my latest contact information here.