3 ms·
> Yep, they just don't care and keep throwing pointers instead references or std::vector/array, #define instead of const, char* instead of std::string and so on
by seanwilson 9y ago
> Yep, they just don't care and keep throwing pointers instead references or std::vector/array, #define instead of const, char* instead of std::string and so on.
> I have been preaching how to write proper C++ code vs "nicer C" since the late 90's, but without much success, specially to enterprise developers.
In no practical sense is it easy then. Even if you find it easy to write perfect code, you still have to work with other people that don't and use libraries which have problems.
C++ has so many ways to shoot yourself in the foot I literally cannot think of a mainstream language that is actually more difficult to master. Would you say it was easy compared to Python, Java or JavaScript to write safe code in? Those languages have no undefined behaviour and they all have automatic memory management compared to C++.
- pjmlp 9y agoIt is harder yes, and actually nowadays I spend most of my time on Java/.NET, just diving into C++ for system stuff. However, the mechanisms for safer code are there, if one cares to use them in first place. The usual set warnings as errors, CI build with static analyzers that break on failure, unit tests with sanitizers that break on failure. But many companies don't care to set this workflow. They also don't care for doing that for Python, Java or JavaScript as well. JavaScript might have automatic memory management, but it has its own collection of WTF moments and implementation issues across browsers. Java is way better, yet there is a reason why Java Puzzles are so entertaining. While a tracing GC is much more productive than RAII/smart pointers, sometimes you also need to track down those references that prevent the collector to do its job. Android developers have lots of "fun" doing that regarding context and activities. One should not forget that safe languages only get us free from memory corruption, regarding safe code. There is still the whole spectrum of logical errors, specially bad in dynamic languages. In any case, until we finally get a mainstream OS written in Swift, .NET Native, Rust or D, we need to make the best use of our tooling for low level system coding activities and in this regard C++ is so much better than C.
- seanwilson 9y agoI agree tools and workflows exist to make it easier to write good C++ code but it's still an order of magnitude harder to do this compared to other languages. I'd only use C++ if there was no other practical choice. It's simply far too much effort to make your code robust.
- pjmlp 9y agoSo what is your alternative when you need C++ like features for systems coding, C?! Because as much as I would like to use a safer alternative like Swift, D or Rust, as an ex-Pascal/Oberon user, they aren't there yet.
- adrianN 9y agoThere is always Ada if newer languages are not there yet for your application.
- pjmlp 9y agoAda is only a solution if it can fulfill the project requirements regarding the libraries, tooling, supported CPUs, OSes and IDEs.
- adrianN 9y agoSure. But now you're retreating to a rather small set of applications. Ada has nice tooling and supports a pretty large number of platforms. I don't think you'll find many niches where not one of Rust, D, Swift, Ada, Java, Go, or the dozens of other memory safe(r) languages could do the job and only C or C++ are adequate.
- pjmlp 9y agoPractical example, we use C++ as systems language for projects done in Java (Standard, Android), .NET (WPF, UWP, Xamarin), Swift/Objective-C (iOS). There isn't much Ada tooling available for those environments, and even using GNAT would be increasing project costs regarding having to implement ourselves stuff that we get for free with the SDK tools that support C++ out of the box.