What does this do?

In my previous post I talked about the challenges anyone trying to teach themselves how to code comes across, mostly due to the way normal tutorials are structured. This time, I’m going to fill in some of the blanks those types of tutorials seem to gloss over. I will assume for the purposes of this post you have done a few tutorials, but maybe didn’t quite understand exactly what it was that you were doing.

I’m going to be giving examples in C#, however the basic principles apply to most languages; some of the words might be different, some languages might do things slightly differently, but overall most of what I’m going to talk about here will be relevant to the majority of popular programming languages.

Lets start with my least favourite example: the “Hello World” app.

using System;
 namespace HelloWorldApp
 {
     class Program
     {
         static void Main(string[] args)
         {
             Console.WriteLine("Hello World!");
         }
     }
 }

Let us take this one step at a time. Even though this application is about as simple as it gets, if you don’t understand what every single line of this code is doing, you’re not going to know how to build your own applications.

The first thing you’ll need to know is this is what Visual Studio will write for you when you create a new console application from the projects menu. This was something that I found annoying when I was learning the basics. A console application just doesn’t look like an application to someone learning to code. Developers often forget that when someone says I want to learn how to make an application, they’re thinking of something like Photoshop, or a web browser, or the Steam desktop app. So a console app is completely underwhelming.

So, why does everyone suggest starting off writing a console app first? To keep it simple to write. Console applications are hard to use, because you have to do a whole lot of typing and remembering commands in order to get it to do something. This is not because they are less useful, it’s because creating a graphical user interface is not that easy. Generally speaking, the easier it is to use, the harder it is to code. And you probably need to learn a whole other language such as XAML to do it. Luckily we can write apps like the “Hello World” app, where the user doesn’t have to interact at all. You run it, and it does what it’s programmed to do.

So … what is this app doing? Lets take a look at the first two lines.

using System;
 namespace HelloWorldApp

The using statement here is telling the computer right from the start that we are going to need something that is not written in this program and it is called “System”, and the namespace for this application is called “HelloWorldApp”. These two are actually related. You see later in the code we say Console.WriteLine("Hello World!");. Nowhere in our code do we need to instruct the computer how to take the phrase “Hello World!” and write it out character by character in the console window. That’s because “System” is a namespace that is already present on your computer. “System” has a class called “Console” which has a method called “WriteLine”.

Okay, that is a lot of information to take in if all this is new to you. But take a step back and look at the original code. We have a namespace too, it’s called “HelloWorldApp”, that has a class called “Program”, which has a method called “Main”. The first line of code in this application is just there to tell your computer that we are going to need some code that is written somewhere else. The namespace sets the name of the code you’re looking for; so if you wrote another application, and you wanted it to use code in this Hello World application, you would need a line near the top that says using HelloWorldApp;. Okay, it’s not going to be that simple in practice. You’ll need to know about project references and dependencies too, but that’s not something we need to worry about for now. Just know they exist and at some point you’ll probably have to learn about those too.

So, the rest of the code.

class Program
     {
         static void Main(string[] args)
         {
             Console.WriteLine("Hello World!");
         }
     }

I admit, I skipped a set of braces there. So lets talk about what that’s about. In many programming languages you’ll see code inside a set of curly braces, “{” at the start and “}” at the end. But they are always directly after a line of code, the code above is entirely within the braces of namespace HelloWorldApp. That’s because the above code is the contents of that namespace. Anything before or after them is not part of that namespace. However, do note that we can have different files that use the same namespace. As far as the code is concerned, if you have something within the curly braces of a namespace of the same name then it is all part of the same thing.

Anyway, class Program. What does this do? Well, a class is something that we use to differentiate the different parts of our program. You might have heard the term Object Oriented Programming, or OOP for short. If not, you will soon enough. You’ll see classes and structs refered to as objects, right now it’s not important you know the difference between classes and structs. This line of code says, create a class, and call it Program. As a side note, using C# in the .NET environment will mean your computer will look for a class called “Program” as the point from which it starts reading your code. In C#, whenever you see the word class followed by a name, this is a class declaration. A class contains the stuff that actually does the things you programmed your application to do.

While the “Hello World” application has just one class, you will have to use multiple classes as you progress. Declaring classes and putting stuff in them is basically the bread and butter of OOP. It will be important you understand how to use them, but the details and uses are very much out of the scope of this article.

So what does the class contain?

static void Main(string[] args)
         {
             Console.WriteLine("Hello World!");
         }

In this case we have one method, some languages use the term function more often. In fact a few use “function” as a keyword. Similar to how class Program was a class declaration, static void Main(string[] args) is a method declaration. Methods are the “doing” bit of your code. We have them to separate out bits of code that we either want to always run, as in this case, or we can “call” them to do something. A method call is when you want one part of your program to use the capabilities of a method. In this example we are calling the method called “WriteLine” to display the words “Hello World!”.

So what are all those words there doing? Static is a word we use when we know this method will be needed by our program, but we will only ever need one of it. That doesn’t make a whole lot of sense … of course we will only need one, right? Not quite, and the reasons why are again outside of the scope of this article. What you need to know right now is that in this case, this method should be static.

You’re probably staring into the void by now. This part of the method declaration basically says, when you call this method, don’t expect anything back. This is used because quite often we would have a variable type here, like int, string, or list, which are used when we use a method to fetch some data, not just perform an action. This is what’s called the return type. You call the method, it returns this kind of information. For example you might write a method that reads data from a file, so you would write the method that returns the type of data you are reading. In this case the method is just making text appear in the console window, there is nothing coming back to the caller, so the return type is unecessary, or “void”.

On to “Main”. This is just the name given to the method. As I mentioned before in C# .NET applications the class called “Program” will be run first, likewise within this class the method called “Main” will be the point at which your application actually does something; in this case, writing “Hello World!”. Method names normally describe what the method does, and within this method you see Console.WriteLine("Hello World!");, “WriteLine” is a method, and as you probably guessed, it writes a line. And the line it writes brings us onto the last bit of the method declaration.

You’ll notice at the end there’s some brackets containing the words “string[] args”. Before we get to this, the first thing you should do is get used to brackets being called parentheses. Google suggests the parenthesis is what’s inside the brackets while coders call the brackets parentheses. I don’t know which is right and it really doesnt matter, because we’re coding, so you will hear them being called parentheses all the time … just know they mean ( ). I’ll try to keep calling them brackets because its also easier to type as well as being a better known term.

Anyway, so what’s with the brackets and the stuff in them? Firstly, “string[]” is the code for a string array. Arrays are a whole other topic I could go off on, but again it’s not really relevant to this post, so just know that’s what it means. “args” on the other hand is the name given to that string array. Now, the reasons for the Main method having “string[] args” in it are also another topic, in fact if you remove that entirely from this app it will work exactly the same. All a method needs in order to work is the brackets, so static void Main() is absolutely fine. You still need the brackets in place because this is one of the ways the computer determines that this is a method.

A method you can see on screen thats a little clearer on what the brackets are for is shown in the Console.WriteLine("Hello World!"); line. As mentioned, “WriteLine” is a method. In C# we use double quotes to tell the computer this is text, so it knows it’s not looking for a number it can perform calculations on, or a true or false boolean value; this is a series of characters making up (normally) human readable text. Text is stored in a variable type called a string. So from that we can say the WriteLine method probably has a declaration that looks something like static void WriteLine(string value). What this declaration says is, when you call this method, I require a string (text) in order to run. If it didn’t say “string”, but instead it said “int”, it would need a whole number. And you can see in our app, when we call the WriteLine method, we provide a string, the string “Hello World!”. The variable types and their names are called the Arguments of the method.

That about wraps up what the Hello World application does, and why it’s so commonly taught. But I would say as learner, I can understand why it’s so demotivating. The good news is that once you have these basics down, you can start to do more exciting things. For example, if you spent a lot of time on online tutorials that give you examples like

int myNum; 
myNum = 15; 
Console.WriteLine(myNum); 

you can put this code right into the Main method between the curly braces and it will run. The above was taken from w3schools site, which is a great resource for tutorials and references for multiple languages.

I will mention here that I’ve tried my best to avoid jargon in this article, and I think the most technical term I didn’t explain was what a variable type is, but as I said this was more to fill the gaps I had trouble with when I first encountered “Hello World” tutorials. The main thing to remember is that in programming, every word you see being used has a reason to be there, or at least it should. If it doesn’t it’s messy code and you should delete that immediately (and it would probably cause an error and won’t run).

Now for the next step. I had read a bunch of basic tutorials on coding and I still had no idea how to actually write a program that I can click and it will run. So, if C# is the language you want to write, I suggest getting the free version of Visual Studio, and the .NET SDK, open up visual studio, and create a new console app project, and hit the play button. If you’re learning another language, find a recommended IDE (Intergrated Development Environment) or code editor for it, and visit the website for the developers of that language to find out what you need to get started.

I also used the term computer a lot during this tutorial, which probably made developers flinch every time they read it. Yes I meant the compiler. You see the code we write is code we can understand. The actual computer it’s running on has no idea what it means. A compiler is a translator from one to the other. Why don’t we write in machine code? Think back to the green numbers scrolling in The Matrix … you dont wan’t to have to make sense of that.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s