Programming tips

One Liner If…Else Statements

You can easily convert if...else statements to one-liners using a ternary operator (also known as a conditional operator). As an example, you can replace this expression:

int age = 9;    if(age < 18) {    printf("A Child");} else {    printf("An Adult");}

With a lot neater shorthand, like this:

age < 18 ? printf("A Child") : printf("An Adult");

The general syntax for a ternary operator is:

condition ? true_expression : false_expression

The ternary operator is common in other programming languages too, so it is definitely not only a “C++ thing.” Tip: Overusing ternary operators can make code less readable. It is beneficial to use it only when the if...else statement is clear enough as a one-liner.

Comprehensive Range-Based for Loops

A range-based for loop is an upgraded version of the traditional for loop, and it was introduced in C++ 11. The syntax of a range-based for loop is:

for(range_declaration : range_expression)

As an example, you can loop through an array of numbers using a range-based for-loop as follows:

int numbers[] = {1,2,3,4,5};for (auto number: numbers){    cout << number << endl;}

Or you can loop through the characters in a string in a similar fashion using range-based for loop:

string name = "Charlie";for (auto character: name){    cout << character << endl;}

Use Auto To Omit the Data Type of a Variable

You can omit the data type of a variable by using theauto keyword in C++ 11 and later. This is extremely useful when you need to declare a variable at runtime, e.g., when using iterators. As an example, the data types of the following variables are declared during runtime.

auto a = 'a';auto t = true;auto x = 1;auto y = 2.0;

Include All Standard Libraries in One Go

Use #include <bits/stdc++.h> to include all the standard libraries in your project without including all of them separately. This is especially useful in a programming competition where time is precious. For example, you can replace this (and many more):

#include <iostream>#include <algorithm>#include <vector>#include <string>#include <stack>#include <set>#include <queue>#include <map>

With this:

#include <bits/stdc++.h>

Keep in mind

  • <bits/stdc++.h> contains a lot of header files that you might not need to use in your project. This can end up increasing the compilation time.
  • <bits/stdc++.h> is not the standard header file of the GNU C++ library. Thus, non-GCC compilers might struggle to compile. However, most of the time this is not going to happen!

Take breaks when debugging

When debugging, it’s easy to go down the rabbit hole for hours, and there’s no guarantee that you will fix the problem. To avoid this, it’s best to step away from the for a few hours, and return with a fresh perspective. Not only is this a guaranteed way to help solve the problem, but you’ll also save yourself hours of headache. So if help isn’t available – to touch on our previous tip about seeking advice – consider taking a break to clear your mind and return later. In the meantime, the bug won’t be going anywhere, and you’ll at least restore some needed sanity to improve productivity.

Don’t just read the sample code. Tinker with it!

Reading sample code is not enough to understand how it works. To develop a true understanding, you need to actually run the code and tinker with it. With the additions of comments and instructions, sample code is packaged to be by the reader; but in reality, it’s pretty difficult to replicate from scratch. Reading is not the same as understanding, and actually trying to write the code yourself, or at least running it, will facilitate the learning process much more.

Seek out more online resources. There’s a wealth of content

If a particular concept doesn’t make sense, be it on in a textbook, or during class lecture, maintain your confidence and look for alternate online resources to learn the same content. Everyone learns differently, and just because one source doesn’t make sense, doesn’t mean there’s something wrong with you. It means that you’re not clicking with the delivery of the material. The online resources to learn computer programming are endless, and there’s always tutorial, or blog explanation that will make the material-at-hand crystal clear. Hint: Don’t underestimate the power of search.

Ask for help. You’ll need it

As awesome as it would be to become the next Steve Jobs on your own, the reality is that people learn faster with mentors and peer feedback. What may seem like an immovable bug or topic could be quickly alleviated by a fresh pair of eyes or a new interpretation of the subject. Whether it’s online or in-person, ignore the trolls and don’t be afraid to ask for help, because every programmer has been in your shoes before. Besides, most developers love to code, and if there’s one thing that passionate individuals enjoy, it’s to share their knowledge with others. Word of Warning: At Coding Dojo we suggest using the 20 minute rule. Take at least 20 minutes to figure something out on your own before asking for help. There’s a good chance that the answer is already in front of you, and besides, struggling makes you a better programmer overall. Hint: Stackoverlfow and learn programming are gold mines for online programming assistance.

Code by hand. It sharpens proficiency and you’ll need it to get a job

Computer monitors become thinner, hard drives lighter, and programming languages more powerful, but coding-by-hand still remains one of the most effective methods to learn how to program. Be it on a whiteboard or notebook, coding-by-hand requires further caution, precision, and intent behind every line of code. Because unlike on a computer, you can’t run hand-written code midway through the sheet to check if the work is correct. Although more time consuming, this restriction will mold you into a more fundamentally sound developer, both in the classroom and the job market. For college exams and technical interviews – a critical component of the job interview process – you will have to code-by-hand, because not only is this good for learning, but it’s universally known to be the ultimate test for a programmer’s proficiency. So start early and get used to this old-school practice.

Grasp the fundamentals for long-term benefits

As elementary as they may appear at first, programming fundamentals always need to come first: the better you understand them, the easier it is to learn more advanced concepts. From our experience at Coding Dojo, students who rush through the beginning of our courses – where we focus most on web development fundamentals – are often the first to get stuck as we transition into more advanced material, such as back-end programming. So before you ditch the first class of computer science 101, or skip chapter one of an online tutorial, keep in mind that you are overlooking the most important step in your learning. Hint: Read this great article about the 5 Basic Concepts of Any Programming Language