multiple ways of doing it makes it confusing

What i find most complex regarding c/c++, is that there are so many ways of doing one thing. Theres a C way, then a C++ way, now a c++11 way, then the boost way, etc. etc. At what point should a beginner go?

I think thats what makes it so complex. Which way is faster? Which way is better? Which way is outdated? Which way should a beginner start at? What do those advanced use?

for example:
i made a function to join a vector or strings into one string based on a certain string separator. I could never remember that function. Every time i want to use it i have to copy and paste it over to my current code as i cannot just remember to write it out. So then i read about boost, and boost has an algorithm already for joining strings. Now if boost has it already, why would you make your own? Why go through the trouble? Especially since your own could be slower or less efficient than boosts'?
Last edited on
It depends, C ways are nearly always not endorsed due to the C++ way addressing common problems and design concepts the C way is lacking.

In regard to C++ 98 and C++ 11, it's best to thoroughly understand 98 when using 11, otherwise if you ever need to work on something which uses 98, then you'll be at a loss. Knowing where C++ 11 can help you over 98 is extremely beneficial. For example you want to use auto in a loop using iterators, you should know the datatypes instead of relying on auto but are using it to clutter code and increase your rate of coding.

Some of C++ 11 should only be used under certain conditions, these are things which go against what is considered good practice. These could be initialising the value of a variable in an instance of a class in the class declaration rather than in the class constructor.

Other things in C++ should be used, such as improvements to the standard library or language which address common problems. Or new features such as the new threading support in C++ 11.

So to avoid getting into bad practices, program like you would in 98, but use C++ 11 if beneficial, while sticking to the older standard when possible.
Stick with C++11, and when you can't, stick with boost.
C++ just like many other mature programming languages and technologies offers multiple ways of doing the same thing, which may all vary in readibilty, efficiency, complexity and flexibility.

In general, technology only moves forwards and never backwards - so you can usually assume that a "newer" technology probably has benefits. In the case of C++, the language has been refined and extended in C++11 compared with C++98 - this is a really good thing because it means there are plenty of new tools and extensions which should help you create cleaner, leaner and more consistent looking code.

It's a really good idea to learn how to use all of the C++ Standard library (both for C++98 and for C++11); most of the time when the standard library gives you a solution, it's much better than rolling your own.

Some programmers might even go far as to say that if you ever need to write your own 'for' loop in order to work with strings/vectors and iterators, then you are doing something wrong because the C++ Standard Library has superior alternatives (This is even more true in C++11 with the addition of Lambda expressions).

Ultimately programming is about decision making - you should choose whatever solution feels the most simple and elegant whilst meeting your needs. Programming language features and libraries are designed to make your code more natural and expressive - so if a bit of code looks messy (e.g. long code which uses lots of control statements) then maybe there's a better way to do it using libraries. Also, don't worry about 'memorising' standard library functions. You will never remember them all - that's what reference books and google are for!

Hand-rolled code is nearly always inferior to out-the-box solutions because the risk of introducing bugs and unintended side-effects or quirks is far greater than using something which has been thoroughly tested and bullet-proofed by thousands or millions of other programmers (This is where both Boost and the Standard Library really shines).

There are loads of useful tools in C++11 which are well worth getting familiar with - they might just save you a huge amount of effort later on in debugging and tidying up your own code. Very often these libraries lend themselves to designing your code in a very modern, clean "C++ style", which should hopefully keep you away from some of the dirty old-fashioned C++ habits and anti-patterns from the 1980s and 1990s.
Last edited on
Theres a C way, then a C++ way, now a c++11 way, then the boost way, etc. etc. At what point should a beginner go?

If you want to get into commerical development then you need to gain some familiarity with all the approaches. The ideal way will not always available, so you cannot just know the one, best way to do something.

For example, I have worked on legacy codebases which are still using Visual C++ 6.0 and Visual C++ 2005. In the first case there was no way time and money was going to be spent on porting the code to build with newer tools; but it needed the occasional bug fix and extension. In the latter case it was licensed 3rd party libraries which used C++ linkage which made the move problematic. So you'd be struggling if you only knew the C++11 way.

I also come across C libraries regularly, so need to know the C, too (though I still find it very clunky.)

why would you make your own?

When working as a commercial developer, or on open source or your own actual projects, you should always use the appropriate library whenever possible.

But for educational reasons, it is a good idea to reinvent the wheel. It's a way to learn about the language, and can even help you understand how the libraries work (e.g. implement your own version of an standard container, without looking at the real code.)

It can also help you understand the language by implementing functionaility in different ways, to help you understand how things fit together.

Of course you should do this in a knowing way, also finding out what the appropriate library solutions are.

Andy
Last edited on
Topic archived. No new replies allowed.