DEV Community

pieterjoubert
pieterjoubert

Posted on

Learn to Program 04: Anatomy of a Program

Rust Hello World Example

Replace the text inside the double quotes where my name Pieter was with your name. Assuming your name is Name the code will look as follows:

fn main() {
  println!("Hello Name!");
 }

So, what is actually happening in the small program we wrote above?

We've written some instructions (commonly known as code) that tells the computer to perform certain actions. There's an interesting idea hidden in the use of the word code to describe the instructions that we provide to the computer.

A code, more commonly, is some kind of encrypted message that needs to be decrypted or deciphered. This rough definition gives us the following useful insights into the properties any programming code we write will have:

  • The code is not in plain language (known as a Natural Language). Instead it's written in a different language, often referred to as a Formal Language. Formal also refers to the fact that there are specific rules that this language needs to follow.
  • The code needs to be decrypted by something or someone. Depending on the language you are using this will either be an interpreter or a compiler, a piece of software that translates your code into something the computer can understand.
  • There is an intended message or purpose to our code. This is what we, as the programmer, are trying to achieve with our code. It is important to understand what this purpose is, so that we can determine whether the computer is executing our code correctly.

Why are these properties of code important? Well they guide us in terms of what we need to do to write working and successful code. Each of these properties loosely fits into a basic coding principle or practise:

  • We need to understand the rules of the language we are using. (We don't need to memorise them fully but we need to understand them, and understand them well enough to research any new rules we come across).
  • We need to setup our interpreter or compiler (and the associated tools) in such a way that maximises our efficiency in writing code.
  • We need to follow some kind of structured approach to determining, documenting and sharing the purpose of our code.

So, for Task 2: Let's look at what happens when we do not follow the rules of the language we are using.

  1. Create a new C# repl.it
  2. Copy over the code below
using System;

class MainClass {
  public static void Main (string[] args) {
    Console.WriteLine ("Hello World")
  }
}
  1. Carefully look at the results of running this code.

Link to Main Content Post. If you are confused about this post start here.

Top comments (0)