Unpopular Opinion #1: Teach Raw Pointers First

4.7
(99)

Being the administrator of a large programming Discord server, and the owner of a YouTube channel with a large audience of programming beginners, it is with some frequency that questions about the basics of programming are asked. In fact, it’s often the same questions. This is perfectly fine, after all, the ethos of One Lone Coder can be encapsulated in the phrase:

“There is no such thing as a stupid question”

An attitude which I wholly endorse, and encourage others to actively embrace because at least the asker is actually asking – a necessary step on the journey to acquire knowledge.

And so it was with admittedly brash vitriol that I lambasted several members of the community the other night, for which I can only apologise, as it was uncharacteristically impetuous of me, though I stand by my lambasting. Before we get there, please consider this code:

descriptor<utilises: a>
{ 
allocate : 23[a] 
<for 'all' in a> set [traits:on]?"numeric type"
    === literal_cast<set.trait>(12);
}
end

Now, tell me which modifications to the code will ensure that allocations remain aligned to 32 byte boundaries, in a contiguous fashion? C’mon it’s really simple!

If you know the answer, you’re lying. If you don’t know the answer, I’m not surprised. You haven’t encountered this language before, you know nothing of its semantics, structure, flow or algorithmic composition. You have just become the programmer you once were, when you first started.

So then, why when someone asks the innocent and innocuous “How do I declare a 2D array in C++?” is the immediate response:

std::shared_ptr<std::array<std::array<int, 500>, 500>>

Let’s just do a little reality check here. Firstly, the question itself implies the asker is not familiar with the language at a fundamentally basic level, so why should it be assumed that requiring the asker to first understand:

  • the standard template library
  • templates
  • object oriented programming
  • compile time memory descriptors
  • smart pointers
  • the concept of ownership

…is acceptable? The guy just wanted a 2D array!

This is a confrontation I often have with my fellow community. It’s great to be able to help people, it’s satisfying, and it makes you look great in front of others – and hey, it’s a chance to flex your skills and knowledge under the guise that in the long term, you are doing them a favour!

Well, sorry, you could not be any more wrong.

Not only is showing off in such a manner obnoxious, it labels you as being confusing in the eyes of the asker, and as a twit in the eyes of your peers. An array is fundamental, and if you are being asked how to create one, chances are the asker doesn’t really know anything about arrays. So don’t bog down your explanation in elitist, pedantic wafflery, just show them how to make an array!

// Ask for some memory
int *a = new int[500*500];

// Do something with it
a[y * 500 + x] = 6;

// Give it back
delete[] a;

Put aside the snobbery, and understand there is a beautiful elegance to this from the perspective of someone who is learning. Every single byte of source code in that snippet contributes to the most minimal form required to understand such an important concept.

  • A variable ‘a’ contains a memory address
  • That memory is requested, and the address put in ‘a’
  • The memory is indexed somehow, starting from its beginning
  • When finished, give the memory back

Surely, for such a basic question, understanding this sequence of events and their associated outputs is significantly more beneficial to the asker, than requiring them to concern themselves with ownership, templates and objects? But alas, empirically it would seem not because invariably the following argument is raised:

It’s better to teach people the correct way from the start, so they don’t form bad habits…

Oh be quiet! You’re embarrassing yourself and patronizing the asker. Firstly, there is no way you learned about arrays (or the fundamentals of programming) according to some “standard” first time around, so stop being a hypocrite. Secondly, you have assumed the asker is too dumb to understand the consequences of their code. And most importantly, thirdly, you have assumed the asker doesn’t have the capacity to adapt and change as they learn and gain experience – which clearly they do, as they are asking the questions in the first place!

Rate this post!

Average rating 4.7 / 5. Vote count: 99

No votes so far! Be the first to rate this post.