Five Typical Programming Mistakes For Beginners

Single Equal Sign on Conditional

A big part of programming is storing values and then comparing them. If this, then do that. Here’s a very basic example in JavaScript…

1
2
3
4
5
6
7
var x = 3;
var y = 4;
if (x = y) {
console.log("They're equal.");
} else {
console.log("They're not equal.");
}

What message should be displayed? Obviously, 3 is not equal to 4. If “x” equals “3”, and “y” equals “4”, then x ‚Ȇ y. In this short program, is the “They’re not equal” message displayed to the console? No, if you try running it for yourself, the message “They’re equal” is displayed. Here’s the real mind-blowing part. It’s actually a true statement!

In the conditional part of the program, the characters “x = y” are used. If you read that, the voice in your mind makes it sound correct. “If ‘x’ equals ‘y’ then…” However, that’s not what the computer is doing. It’s setting the value of “x” to the value of “y”.

To fix this problem, simply use two equal signs. The correct code is “x == y”. It looks a little weird, but those are the rules.¬†This is not just a problem for beginners.¬†If you’re in a rush, or under the pressure, you could easily miss it. The way to avoid this problem is to have a solid understanding of Comparison Operators.

https://www.w3schools.com/jsref/jsref_operators.asp

Case Matters

Even just writing this article, I fell into a typical trap. Capitalization matters! A variable of “x” is not the same as “X”. To your mind, “x” and “X” are practically the same thing. To a computer, it doesn’t think. It just runs programs.

Arrays Start at Zero

Variables are one way to store values. But if you have a lot of values, an array makes it easier to organize. It’s like an Excel spreadsheet in your program. The problem is that Excel starts at row #1. An array, in programming land, typically starts at row #0.¬†This is another one of those things that messes with the voice in your head. “I want the first row”. OK, that’s array[0]. This can be very confusing, especially for beginners.

A nice way¬†to solve this problem is to include table headers in your array. By putting column labels at the top, the rest of the array is shifted one row down. The numbers align. It’s makes more sense.

If you’re using “Mr. Data Converter” to convert your data to web-friendly formats, such as¬†JSON and XML,¬†there’s a “First row is the header” option.

Conclusion

There are lots of ways to screw up in programming. The more you learn, the more involved¬†your projects become. This leads to more complicated problems to solve. If you think you’re having trouble battling with problems as a beginner, it doesn’t get better ‚Äď it gets worse. What gets better is you. One of the¬†tricks of being a good programmer is identifying problems and then figuring out ways to solve them. If you’ve experienced any of the problems in this list, congratulations. You’re on your way to becoming a better programmer.