Welcome to Part 3 of the multi-post series on Fundamentals of Programming. Last time, I just touched upon one very important point regarding style in programming: indentation. In this series, we’ll see a bit more about styles in programming. You can think of those as the “formatting” used in programming. We’ll end with the importance of conventions, comments and consistency in programming. I hope you will enjoy this part and learn something.
Editors and IDEs help!
Last time, we saw about indentation. Basically, indentation is making specific lines of code offset a bit so that you know at a glance where they belong. There are two ways of making indentation while programming: one is to use tabs (i.e. the tab key) and one is using multiple-spaces e.g. (press the space key 4 times).
Which one you use depends on you. As long as you stick to the style you choose, you should be alright. Now, if you’re writing code for other people e.g. open-source projects or a company, they may enforce a style on you i.e. you should only use space for e.g. because upstream, some other people have troubles with tabs. At this stage, you shouldn’t worry too much about those. In any case, you can have your Integrated Development Environment (IDE) do the substitution for you for e.g. replace a single press of the Tab key by 4 spaces.
You still remember what an IDE is, right? It’s that program that makes the task of writing other programs easier. Like a word processor for writing programs.
It’s a good idea to use an IDE when writing programs. Which IDE you will use largely depends on which language you are writing in, so we won’t go into the various IDEs that exist. If you write in many languages, or don’t want the hassle of having to install and learn an IDE, you could use so-called “Programmer Notepads”.
I did say you’re fine using Windows’ Notepad program. Actually you are. Only, it is not specially meant for writing programs. It’s just a generic text editor.
These are text editors that have IDE-like functions especially code highlighting, syntax checking, code completion and indentation guidelines for e.g. There are many of those out there, so I’ve listed the 5 most common ones so you have something to start. Just choose one and start typing! The number of features provided will vary from just syntax highlighting to fairly complex things like versioning through plugins. So just get one of those 5 and you should have an easier task at writing code:
On Linux and other Unix variants, you will have the all-powerful Vim and Emacs, as well as GUI variants such as Kate and GEdit. Am just listing those here for reference, but if you’re already on Linux, you should probably already have found those. They probably come ready installed with your OS anyway so just find where they are and start using them. 🙂
Which editor you choose doesn’t really matter as long as it does what you want, and it makes things simple. If your editor makes a simple task more complex, you should either consider using another editor or see whether you’re doing stuff right in the first place! Editors vary in terms of features: some people like editors that does everything from highlighting brackets to sending satellites in space on their own. Others just want a place to type and if it provides pretty colors, fine! See what you want in an editor and choose appropriately.
At first, it’s good to try a lot of them to see which one you like. But after a while, try to choose one and learn how it works. You don’t want to keep switching editors mid-project and having to re-learn where the various options e.g. search and replace are found. I.e. find one and love it.
When you’re using one of these editors, the task of checking indentation, finding where brackets and braces open and close etc are handled automatically. That’s one less thing to worry about. But you still need to ensure your indentation is correct. Let’s see how pseudo code would look in one of these editors: Notepad++.
And here’s how it looks in plain old Notepad:
You can see a few things are different from the Notepad++ image. First, the “reserved” or keywords are highlighted in a different color: blue. Second, brackets are highlighted so you can easily identify where pairs of brackets start and end (they are bolded here). Quoted text is in a different color, so if you accidentally missed a quote, you’d see everything in grey and you know something is off. See the faint dotted lines below “If”? They represent indentation guides. That is, they show where the “If” is going to end and what code falls under the control of the If. Similarly, if I had different levels of indentation, I’d see additional lines. Finally, you can see line numbers. If you’re writing code and a friend says there’s a problem on line 13028, you don’t have to start counting but can go directly to it! Notepad cannot do this for you.
All these little things really help when you are writing large programs with thousands of lines. You can immediately see where a block starts or ends without having to do a ton of scrolling.
I think you’ve got the point now. Use an editor or an IDE. It makes life easier!
What are comments? It’s as the name says, things you write in the code to make it clearer when reading it. They’re like small notes you write in the code itself to describe bits of code that might be difficult to understand at first glance.
Why would you want to type additional things and make things more tiring? Well, comments help. A lot! A LOT! They may not make sense now, but think about reading the same code, but 2 years later. Will you remember then, what that obscure piece of code does? Probably not. If you have put comments there, then yes, you’ll know immediately.
First, don’t write stupid comments. After you start programming, it’s no use commenting every single line of code. That is both senseless and wasting time. Things like (a+b) are obvious to any programmer. What you should be commenting is when you have codes that do special tasks. For e.g.
Does this make sense?
Does it now?
Now you know the importance of comments.
Comments are identified by different symbols in different languages so make sure to use the right symbol, but here is a quick rundown of the most common symbols used to identify comments in code.
Now you how what comments are, i.e. descriptions about code, written in the code itself and identified using special symbols, I advise you to use plenty of these where you think the code is not very clear or easy to understand. You’ll learn what to comment as you start writing more and more code. It then becomes second-nature and you’ll wish every programmer out there commented their code appropriately.
Consistency refers to writing code in the same style, every time. This is not always possible as sometimes different projects impose different styles. But whatever style you will choose, stick to it. I.e. be consistent.
For e.g. I like plenty of white space in my code. I find it makes stuff easier to read. The second one is how I’d write this code. The first one could be written by another person who likes compactness and minimalism. Both do the same thing. If it makes sense to them, fine.
But now, if I start arbitrarily switching between the various styles, for e.g. I use different numbers of white-spaces or different styles of opening and closing brackets, it makes things tough for others.
We’ll see more styles of coding in the other parts, especially after we start “Conditions”. It then becomes important that you write in one consistent way instead of as-you-please. Just keep this point in mind at the moment: choose your style and stick to it. Don’t change mid-way because you think brackets are prettier on their own line. You’ll make many other people angry!
That’s it for this rather short part. I hope you found it interesting. As usual, ask your questions using the comments box below and I welcome comments from other programmers if they think I could have written things better or provided more details. See you in the next part!
[seriesposts title=”In the same series:” titletag=”h3″ listtype=ul show_date=0]