15 points · by backslash_16 · 10y ago · 20 comments
I got the Bjarne Stroustrup book Programming: Principles and Practice Using C++ as a gift and now I'm trying to figure out if I should keep digging into C, jump ship to learning C++ or take a look at Rust.
I started learning C because I wanted to dig into a non GC language and get a little "closer to the metal", not because I need it for my day job. My friend got me the C++ book because he said I should spend my time learning a language that I might end up using in a job but that's not my primary concern right now.
Lastly I started looking at Rust because the HN search (https://hn.algolia.com/?query=rust%20c&sort=byPopularity&prefix&page=0&dateRange=all&type=story) returned a lot of interesting comparison beween it and C/C++. They all make it sound like Rust is an easier to learn systems language which lowers the chance of making memory related errors and has a few nice features to make the code easier to read and understand.
Does anybody have opinions on which language I should learn for the purpose of understanding how code interacts with the computer at a low level better, manual memory management, and learning how to deal with pointers (instead of C#/Java references)? Any personal preference out of all the languages?
Rust is interesting, but I think it will remain an interesting language outside the mainstream without any major commercial backers (Mozilla does not count). In addition, C++ is taking a lot of the ideas from Rust and incorporating them along with tooling that will provide 95+% of the value of Rust while still keeping the advantages of C++ (see https://www.youtube.com/watch?v=hEx5DNLWGgA)
What is interesting with C++ is that although a lot of vendors are pushing their own languages - Microsoft with C#, Apple with Swift, Google with Go, Facebook with Hack, Mozilla with Rust - they are all deeply involved with C++ because C++ powers their mission-critical code. Another way of putting it would be that despite all the promotion if all of Mozilla's Rust code were eliminated tomorrow nobody would notice, but if all of Mozilla's C++ code were eliminated, it would cease to exist.
Also, C++ is rapidly evolving and by learning Modern C++ 14 now, you can avoid a lot of the mistakes of C++ that were common in the past and write safe, elegant code. C++ is also experiencing a renaissance (see https://www.jetbrains.com/cpp/cpp-today-oreilly/). C++ is now the one high-level language that is natively supported by the system vendor across Windows, Linux, Mac, iOS, Android, Windows Phone, XBox, and Playstation.
But for a small team for a new project, it is probably easier and more cost-effective to use a language that is safe from the ground up, rather than relying on external vendors. (Most of these tools have a free option for FLOSS projects, but you might need to develop something closed source)
I honestly don't know if this is something that can tip the balance, or if this is a "detail". Time will tell.
Microsoft will be releasing the static analysis tool as a free cross-platform tool (and I think open-source IIRC). They shipped a preview for Visual Studio 2015 with Update 1, and are planning on shipping a cross-platform version soon. As tools like this become free and ubiquitous, it will greatly improve C++ code safety.
Rust is more complicated than C and it tends to insulate you from the low-level details. The way it combines low-level control with high-level abstractions is really interesting, but from an educational point of view I think you'd get more out of it after you understand C pretty well.
C++ is a giant, sprawling mess. It can be very useful but I'm not sure what it will teach you other than how not to design a language. :)
It is easier to learn C++ if you already know the distinction between value vs. pointer types, if you're already familiar with C syntax, if you already know how to manage memory manually, and if you already know how to use functions, function pointers, and opaque types to create an API. C will teach you all of those.
It is easier to learn Rust if you already know about RAII, ownership, generics, both run-time and compile-time polymorphism, and move semantics. C++ will teach you all of those (in its C++11 and C++14 variants, at least).
Learning C as a stepping stone to C++ is a waste of time. You might think it's a good idea, because C++ is based on C, and most of the features of C are available in C++. But writing good C code requires idioms that are useless in C++.
For instance, suppose you need to write a function that opens a file, reads the contents into a some data structure, and then returns a result. Before the function returns, it needs to free the memory in the data structure and close the file. If you write this in C, you either have to make sure you only return at the end, or use nested "if" statements, or use "goto" statements. But in C++, you don't have to worry about this, because the object destructors will handle it.
But....if you actually approach it as an opportunity to expose yourself to new ways to think about problems, I don't think that would ever be a waste of time.
I disagree that you'll never use C idioms in C++ or Rust, but even IF that were true, there's absolutely no way that having a better understanding of what the underlying hardware is doing is a bad thing.
This sounds really great, along with everyone's advice about learning C++11 and C++14 when I get there. Although C++ having a lot of language features (like destructors built it) is appealing. HN definitely helped me figure out what to do.
I've really liked what the C book has taught me so far, because nothing is hidden, especially the part about building an object oriented programming language and the side projects of communicating over ports.
Before that, I academically knew that a method is just a function with the object passed/bound as a "hidden" first parameter, with a keyword to make referencing it easier but building it solidified it.
After you learn C, go to Rust. Rust will just teach you more than C++.
If you decide to learn Rust, the Book[1] is, I think, the best way for you get started, seeing as you already have experience programming.
While this is true, it's just that only after knowing C++ pain points one can appreciate Rust more :o)
if you look at the C++ code on there, much of it is written in the style of C. If you can write newer code and do not have to maintain old code, C++ would be a good second. There are still tons of high paying jobs for C++. Some pay 400K+ in the NYC area
Just picking a low-level systems language for my own fun and edification, I'd still learn C because it is so ubiquitous, but I for one hope that something more like Rust is the future.