You sit there, staring at the screen in a moment of quiet reflection, your mind wanders and thinks about what you’re really looking at. You’ve seen it lots of times but until now never thought ‘how does this work?’ and ‘how was this created?’. What is this thing of apparent mystery and magic?
If you’re unfamiliar with ‘code’ and the scientific art form (*1) of ‘programming’ then it can appear to be quite a bit scary and baffling to read the words that orchestrate your computing world. So, let us shed a light and reveal some of the darkness.
*1 Combining the words ‘scientific’ and ‘art’ to describe ‘programming’ can be seen as controversial. However it should be taken as my feeling about programming itself. Programming or more generically ‘Computer’ qualifications are scientific. I have a Master of Science in Computing, something I am really proud of. However the actual style that a programmer has is akin to writing and creativity. That style can be considered beautiful (you can admire somebodies work) and unique to the person to whom it belongs, thus an ‘art form’. The science element comes from the principles that apply to engineering within the subset of computing.
Moodle™ is a registered trademark of ‘Martin Dougiamas’ – moodle.com/trademarks.
Other names / logos can be trademarks of their respective owners. Please review their website for details.
I am independent from the organisations listed above and in the text (except MoodleBites where I am a facilitator). I am in no way writing for or endorsed by them.
What is ‘Code’?
Code is quite simply like a recipe you’d use to cook food. It is a set of instructions that are performed in order to produce a final result. In the case of food, a meal. You make meals in order to eat and survive. You create code in order to make software and have a purposeful tool that solves a real world problem. That problem just like a meal can be related to survival. Moodle is software that helps us to learn new things, which could be said to contribute to your knowledge that helps you to survive.
Code like a recipe is just the process. The actual ingredients are knowledge and the hardware upon which the process interacts. For example, you can write a line of code that adds two numbers together. That line is written in a language that can appear to be a form of English, but it is not. The code instruction will ‘tell’ the hardware what to do. The hardware through a process of electrical interactions of circuits will undertake the process of changing the physical state of something. Thus adding 4 to 8 being 0100 and 1000 in binary (shown as a ‘nibble’ which is ‘half a byte’ – somebody has sense of humour!), to become 1100 or 12.
Because computing hardware operates using binary, and those binary values operate as switches (*2) to configure the state of the machine, then it can be extremely difficult for a human to easily understand what a long sequence of binary numbers will do. That’s where code comes in, it is a human understandable abstraction of the binary.
*2 Consider your interactions with turning on a light bulb. You operate a switch that has two states, on or off. When ‘on’ (given there is electrical power) then the light bulb lights. When ‘off’ then the bulb is unlit. A switch sets up a ‘state’ for that machine to be in.
There are ‘code generations’ of different levels of abstraction, so a ‘1st generation language’ is binary itself, the second is ‘Assembler’, the third there are many such as ‘C, C++, Java…’ and PHP which Moodle uses is a forth (because it is implemented in C / C++) as far as I can tell, but I could not find any specific references:
There are higher generations. But the general rule is that the higher you go the easier it is for humans to understand the syntax. And to get to the binary there are programs that convert one level down to the next.
For example a ‘compiler’ will can convert a third generation into a second and an ‘assembler’ a second into a first.
Computers do not understand the higher generation code languages, instead software like ‘compilers’, ‘interpreters’, ‘linkers’ and ‘assemblers’ must first convert them to the binary that the computer does understand and can actually ‘process’. If that ‘chicken and egg’ light bulb has just lit in your head with the quandary of ‘What came first? The higher generation code or the compiler?’ then I understand it to be that someone had to first create an ‘assembler’ by hand in binary first. Then in turn to create a ‘compiler’ then the initial elements had to be created in assembly language for its use on third generation languages.
What the code in Moodle does
In a simple form, the code in Moodle uses the information supplied by the user along with stored data to produce the output that can be understood and displayed by a web browser.
When you interact with Moodle using a web browser, you send information to a web server (another computer) that combines that with data it already knows to produce a result. That result for the most part is written in another language (code) called Hyper Text Markup Language (HTML – en.wikipedia.org/wiki/HTML). That language is human readable and is interpreted by your web browser to produce what you see (or hear via assistive technologies).
If we wanted to have a heading printed on a web page using PHP then we could write:
where PHP will ‘echo’ what it is given to the output of the HTML as the web server serves it to the client, your web browser. Of course this is really simple. In Moodle 3.6 the course title is processed by the code (in the method ‘render_context_header’ in ‘/lib/outputrenderers.php’):
which calls the method ‘heading’:
Note: All Moodle PHP code is licensed under GPLv3 – www.gnu.org/licenses/gpl-3.0.en.html.
which if our course is called ‘Topics’ results in the HTML:
This may seem all very complex and over the top just to undertake what appears to be a simple task. However, there are reasons for this.
The first is the ability to choose different paths through the code depending on the data, so a program will make decisions and can produce different results if the input data is different.
The second is reuse, code can have ‘functions’ (in procedural programming) and ‘methods’ (in object orientated programming) which can be used over and over again by different parts of the program to undertake the same task. So in our example the method ‘heading’ (ignore that it says ‘function’, long story) can be ‘called’ by other parts of the program with different data and will return a ‘string’ containing heading HTML based on that data.
English but not English
As you look at the example it appears to be in English, but it is not. It has its own syntax. For example:
- The ‘$this’ is a ‘reference’ to the instantiated object of the class that the method belongs and allows you to call other methods (or access attributes) of the same class or if permitted a parent or child class. Just like the line ‘$this->heading($this->page->heading, $contextheader→headinglevel);’ calls the ‘heading’ method. Imagine that a ‘reference’ (also there is another form called a ‘pointer’) is a small label with a piece of string attached to it that is tied at the other end to the object that the label names.
- ‘if ($level < 1 or $level > 6)’ is saying if the value contained within the ‘$level’ is less than 1 or greater than 6 then output an error. This is because HTML has only six heading tags between ‘h1’ and ‘h6’.
- ‘$html .=’ is saying append the resulting string returned by ‘html_writer::tag’ to the string contained in the ‘$html’ variable.
- ‘html_writer::tag’ is the way to call the static ‘tag’ method within the ‘html_writer’ class statically. Having things as static is just a way for object orientated programs to ‘encapsulate’ (a way of organising a program to be more compartmentalised and in a form of ‘objects’ that we humans classify ‘objects’) methods without an object so they behave like functions used in procedural programming.
Please don’t worry if some of this does not make sense. Understanding the concepts takes time. Just try an imagine how long it really took in your childhood to learn your native language. Pragmatically, we are talking years here. And how long was it before you became really good at it and understood the more complex words and concepts?
The same is true of code. You need to learn the concepts and syntax in order to gain knowledge to have the skill to apply that to solve problems that will be orchestrated by a machine.
Learning to code
Learning to code is just the start on your journey towards becoming a software engineer. As software engineering encompasses the disciplines and processes that surround the practice of writing code itself.
I started off in the early 1980’s learning BASIC (Beginners All purpose Symbolic Instruction Code) on a 16k ZX Spectrum which used cassette tapes for offline storage. When floppy disc’s were considered a ‘luxury’. Since then, things have moved on.
To start off I recommend using ‘Scratch’ – scratch.mit.edu/about – as a way of getting to grips with sequences, control structures, decisions and variables. It’s graphical and auditory with virtually instant results.
For PHP you can use – www.w3schools.com/php/default.asp but you’ll need to install it first – www.php.net/manual/en/install.php. Or take the free MoodleBites ‘MoodleBites technical course preparation’ (www.moodlebites.com/enrol/index.php?id=228) course which will install a local Moodle as well as PHP with a web server.
The greatest barrier to learning code is fear. This is because it can get scary very fast. Its like attempting to climb a big steep mountain for the first time without any previous experience. You take one step and find yourself surrounded by difficult terrain and what appears to be no way back. This can happen with the multitude of technical terms and concepts that surround the true meaning of the code syntax.
To overcome the fear you need to mitigate the risk that you think you’ll break your machine. If you take the philosophy that you can just uninstall or remove something that is broken and start again, then that is a start. Having an old machine, one dedicated to your learning or a virtual machine (you can run an operating system within an operating system) to practice on where it does not matter if you break it will help you overcome the fear.
Don’t be worried if it takes time, you don’t understand something first time. Be inquisitive, ask questions and persevere. No question is stupid if you don’t know the answer. Giving up can be easy but taking a small break and then trying again takes determination and you need to be determined. Set yourself goals and targets. But make those goals small and don’t expect the result to be all singing and dancing. If your initial expectations are too high then you’ll give up.
Over time you will discover that you will have your own style and way of writing code that has a pattern in the solutions you create. Just like a writer will have their own style of writing.
A restriction that can be applied to your style is a coding standard / style that forces you to write the code in a certain way. Moodle has such a ‘standard’ as you can find here: docs.moodle.org/dev/Coding_style.
Code is the instructions that makes the machine that is the computer, work. It is not something to be feared or considered magical. It is simply the recipe for software. The true skill comes in its understanding and application.
Latest posts by Gareth Barnard (see all)
- Customising H5P in Moodle using the Foundation theme – 16th September 2019
- Software – 16th August 2019
- Essential to Foundation, theme evolution – 16th July 2019