DEV Community

Cover image for I Think I Overestimated How Hard C++ is.

I Think I Overestimated How Hard C++ is.

Hi, name's Sean.
・2 min read

It's like I'm a Noob Again

I'm new to C++. I've been avoiding it for a while, I even learned other languages to get my mind off it. It bugged me that I thought I couldn't learn it. I tried hard with C and failed. I wasn't used to how not readable it was. It was very difficult to read unlike, ruby, python, and js. I couldn't understand writing everything in the main() function to execute the code. I felt kind of stupid, and I didn't like that so I didn't try again.

A Little While After...

I was kind of forced to spend more time coding now that we're quarantined. So, I decided to try being a noob again. I realised that if others could do it I probably could too. So today is officially day one. I tried not to think about how it wasn't going to be readable and tried C++ instead.

Turns Out... It's Not As Hard As I thought

C++, IMHO, is WAY more readable than C. It's still less readable than python, but it's manageable for me. Not only that, but like a million other things became super easy. Like looping on a condition, I can just use a label and call the goto statement. If I'd known that this is what I'd be doing I would've tried C++ a long time ago.

Did anybody make the same mistake I did? Do you have any advice concerning learning C++?, 'cause I'd love to hear it. Please write about it in the comments.

Discussion (9)

stojakovic99 profile image
Nikola Stojaković • Edited

C++ is a beast of a language. I'm currently in the process of re-learning it (it was my first language 8 years ago when I started to program) and I can say that learning C++ today is much easier than it was 8 years ago and there are few reasons for that.

  • C++ evolved a lot. Back then even C++11 didn't have much support by compilers, and C++ before C++11 was a mess.
  • IDE support is much better too. Back then I remember I was using Dev C++ and Code::Blocks. Right now I'm on CLion and I wish something like it existed much earlier.
  • We now have C++ Core Guidelines. This means there are at least some established practices we can follow in writing C++ applications.

There were some bad decisions made in the past that have made C++ huge but overall it's quite great language which got undeserved bad reputation, mostly by the people who try to use it in a wrong way or those who are too scared by the power it gives to you (and with the great power comes the great responsibility too).

katnel20 profile image
Katie Nelson

I waited until I was proficient in C before I tried C++. I think that helped quite a bit.

ahferroin7 profile image
Austin S. Hemmelgarn

So, I regularly call C++ a difficult language. It actually has to do more with how easy it is to write bad, dangerous, or horribly inefficient code in C++ compared to many other languages, not some difficulty in doing perfectly normal things.

Your looping on a condition example is actually a good one. Yes, you can construct a loop with a conditional goto (that's actually what the compiler reduces it to anyway in assembly code), and you can break out of a normal loop with a goto statement, but that doesn't mean that either is 'good' code from a readability perspective. for and while loops exist for a reason, as do break and continue. Using those instead of putting together the equivalent construct using conditional goto statements makes it much easier for other people to understand what's actually going on in your code.

seanolad profile image
Sean Author • Edited

While that's true readability, in my opinion, is less important than functionality and using what others call "Dangerous" code seems generally helpful to me. Now, I do realise how easy it is for someone else to not realise what is happening in your code, especially if you mix default statements, constructors, structures, and overload operators. But shouldn't the reader just examine the code judiciously? When writing any code(except python, ruby, and JS) I don't make a point to write readable code, but functional, concise, and efficient code. Though, I'll keep what you said in mind.

ahferroin7 profile image
Austin S. Hemmelgarn

I don't make a point to write readable code, but functional, concise, and efficient code.

That's a very bad attitude to have when writing code. Anything higher level than raw machine code or bytecode for a VM runtime is intended to be read by humans, not computers. It's the whole reason that everything from assembly code up exists. And if it takes longer for someone to understand what your code is doing than it takes them to read it twice, chances are they're not going to correctly understand exactly what it's doing (and even if they do, they've probably spent more time than they should have getting to that point). This becomes even more of a reality when dealing with people who are either new to programming or have overspecialized in a particular type of coding (which is unfortunately more common than it should be). Also consider yourself 10 years from now digging this up and trying to remember what it does (chances are you will not remember exactly how it worked, even if you're an expert in C++ by then). This is why coding style is important, but it's also why it's preferred to use the highest-level construct possible that doesn't make the code more ambiguous.

Circling back to the loop example, the following is indeed perfectly understandable to most people:

loop: do_something();

if (check_something()) {
    goto :loop;

However, once it gets past a few lines, it becomes much harder to track what's going on because you have to find where that label is in the code without any obvious hints to help you. In contrast, the following exactly equivalent loop code is both more concise and can be much more easily followed when the loop body ends up being thousands of lines (not as unusual as it sounds):

do {
} while (!check_something());

The do {...} while (...); approach is also preferred for two other reasons though:

  • Good editors let you jump directly to matching braces/parenthesis. With the do {...} while (...); construct, this trivializes finding the other end of the loop (provided of course that your braces are properly balanced). In contrast, for your goto approach, you can only easily find the starting point of a loop from the end, and even then it's not as easy unless you make the effort of having all your labels be unique within the file (not just within the function as the C++ standard requires).
  • Exceptional editors allow for code folding, letting you collapse (language defined) blocks of code into a single line so you can make more efficient use of screen space and not be distracted by code in scopes other than what you're working on. This works perfectly fine with the regular do {...} while (...); loop, but does not work at all with a manually constructed goto-based loop.
Thread Thread
seanolad profile image
Sean Author • Edited

Okay... that was long as HELL. I get what you're saying but all the points you made are still opinion based, or incorrect. The reason why l different languages were developed was not really out of necessity to be readable than it is to be time efficient. People can't read 0's and 1's as quickly as english or any other language for that matter. Usage of the goto statement over and over, or over huge spans of code is just as inefficient as many different loops and if statements. An experienced developer would work around that. Yes, using the do... while loop is favoured, but that doesn't mean usage the goto statement is dangerous. The whole point of having multiple methods to achieve the same outcome is to allow for different styles. Suggesting that any coding style that reaches outside of the standard scope is a "very bad attitude" is just genuine stupidity. True, readability is recommended in more than one community, but code that is readable is often not optimized, and no offence to those who don't agree, but that's kind of the trump card of c++, optimization. Now before you start talking about how optimized code is more difficult to debug, I respect your opinion, but I just DON'T agree. If I take devil's advocate and look at it from your perspective, yes readability is important, yes there are other more readable methods in c++, yes it's easier for others if you use readable methods, AND YET there is still a HUGE thing you completely overlooked. COMMENTS, they are LITERALLY there for EXACTLY what we're talking about. READABILITY. Understanding the code someone writes is hella easier with comments. If you write ANY code with the intention to return to it down the road you should use comments. Your code DOESN'T even have to be a little bit readable, if you put comments at every stage in your code you will be refreshed enough to understand what is going on, and a third party would too. I've said all I have to say, good day.

Thread Thread
maoitsme profile image
Comment marked as low quality/non-constructive by the community. View Code of Conduct

you're not very bright, are you

patarapolw profile image
Pacharapol Withayasakpunt

Why do you personally learn C++? Is there a framework you need?

seanolad profile image
Sean Author

No, I was interested in learning C before. I didn't do what I set out to do so this time I want to get it done. Also, many senior developers insist on C++.

Forem Open with the Forem app