Friday, June 8, 2018

Two completely stupid advices for beginner programmers

Whenever someone asks on some online forum that he would want to learn how to program, or how to learn better programming, almost invariably you will see two kinds of advice given, both of which are absolutely and utterly stupid. I have seen it done over and over, and I will never get tired of heavily objecting to both. The first one is way more common (and applies to pretty much all programming languages), but the second one is also quite common, especially when we are talking about certain particular languages (like C and C++).

The first advice is the archetypal "look at the source code of open source projects out there, to see how they are made."

This is absolutely stupid advice because it's extremely inefficient, will teach you very little, will take you a lot of effort to understand and learn anything, and may teach you lots of bad habits (such as cargo cult programming).

I always use the same analogy to object to that advice: It would be like if someone asked how to learn to cook, for someone to advice "go to a restaurant, order a meal, and look how it has been made."

That would make no sense. It's hard to see from the end product what the process of creating it was. Likewise in programming seeing the implementation of an algorithm tells little about what the algorithm is, or how and why it works. Even if the beginner programmer would understand what the algorithm is doing, he would probably not understand the theory behind it, or why it's done that way, and it becomes difficult for him to apply this knowledge to other situations. And even if he studies the implementation enough and finally understands the algorithm, it will have taken a lot more time and effort than if he had read the theory behind the algorithm in the first place (and even then, he may still be left with things he doesn't understand about the algorithm, as well as misconceptions about it.)

A much better and efficient way to learn programming is, of course, to actually learn programming, not looking at other people's source code. This learning is better done with books, tutorials and programming courses.

The second completely stupid advice you sometimes see, especially when we are talking about certain languages like C and C++, is to study assembly language first.

This advice is so stupid that it baffles my mind. Yet I have seen it many, many times, especially on certain forums.

Learning assembly gives you exactly zero insight into either programming in languages like C and C++, as well as programming in general. There is pretty much zero advantage in learning assembly, when your intent is to learn those other languages as your first language, or to learn programming in general. The only thing that learning assembly would do is to drive you into a useless wild goose chase for no benefit.

When these people are asked why anybody should learn assembly, if what they are trying to do is learn C or C++, the answers are complete non-sequiturs. They say, for instance, that it helps you understand how a program manages memory.

Firstly, a beginner programmer doesn't need to know how a program manages memory. Secondly, even if he did, learning assembly is neither necessary nor useful for this. It's not necessary because it can be perfectly well learned directly, without the burden of knowing asm, and it's not useful because there's actually nothing in assembly that somehow makes you more knowledgeable about memory management.

In fact, even when you are learning really low-level advanced stuff (which a beginner programmer doesn't need to know), such as how CPU pipelines and caches work, what cache misses are, how to optimize for optimal cache performance and so on, you need zero asm knowledge for this. All of it can be learned and understood with zero knowledge of asm.

The only useful thing that knowing asm gives is if you ever want to see what kind of asm code the compiler is producing from a given source code (which in a few instances can be useful for very low-level optimization). However, a beginner programmer wouldn't need this for a long, long time.

Continuing with the cooking analogy, suggesting asm for a beginner programmer would be like telling a beginner cook to buy a book on particle physics, because food is made of elemental particles. Yeah, it is, but that knowledge gives literally zero help in learning how to cook.

No comments:

Post a Comment