C++, I love you.

Pages: 1234

C++ allows you to write code in any way that the compiler can understand, which means that if you want people to abide by specific rules, you need to tell them to do it, because the language won't enforce any specific style by itself


Looks very PHPish (in PHP they didn't agree on naming convention or error handling convention.... well, just like in C++). And that is a very bad thing, because it ends up in everyone creating their own coding style, and if you have a big project using many libraries, whatever you do, your code looks like a mess.
Last edited on
cubbi wrote:
Read a book.
rapidcoder wrote:
Which one? There are so many


The most practical is Sutter/Alexandrescu's "C++ Coding Standards: 101 Rules, Guidelines, and Best Practices", from 2004, I think.

If Lakos ever makes a new edition of his Large Scale C++ Software Design book (1996, long obsolete), it would be another recommendation - I've read his recent (2008-2012) ACCU lectures, and they are good, and cover a very different ground.

What books do you think of when you say "there are so many"?
Last edited on
c++ imposes little, making the programmer a little more responsible. also it offers a lot. i love it.

What books do you think of when you say "there are so many"?


http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=C%2B%2B
7000+ results.


The most practical is Sutter/Alexandrescu's "C++ Coding Standards: 101 Rules, Guidelines, and Best Practices", from 2004, I think.


Why this one? It is just a bunch of opinions of some theoretists sitting in a C++ committee. And most haven't been tested in large scale development. Successful software companies don't use these guidelilnes.

Last time the committee theoretists created the infamous "export" feature that coudn't be implemented, exceptions that cause more problems than solve (most companies abandoned them) and this time they almost introduced crazy-complicated "concepts" feature (NIH again - they could just grab the design from Java / C#). And now they also advocate using of smart pointers almost everywhere which is freaking slow and gives false illusion of safety (cycles), and they advocate using of lambdas everywhere as the "new style of coding" in a language without a GC (you must be really insane to write large scale code like that). So: how can I trust them? I think Google's guidelines are much more trustworthy...

Last edited on
and they advocate using of lambdas everywhere as the "new style of coding" in a language without a GC


Does a garbage collector really make it 'OK' to litter closures around a code-base? Seems like just more garbage to clean up (especially in Java's case)...

Ah, and I do fail to see how properly using lambdas to interact with the STL algorithms differs from using plain old functions. Care to elaborate?
Last edited on
The sole purpose of lambdas is to capture the local execution context and pass it somewhere. If lambda function outlives the context, you've got a big problem. GC avoids this completely and also makes using lambdas much more convenient (noticed why C++ lambdas syntax is so ugly? because of lack of GC).
7000+ results.

Are you trolling again? "C++" is not the search keyword that would give you accepted guidelines. It's like searching "Chemistry" when you want to learn about lab safety protocols and equipment handling guidelines ("always add acid to water, never water to acid")

Why this one?

Because it contains a few of the simplest, most basic, practical guidelines I've seen (perhaps too simple, and limited to pre-TR1 C++03 language, but it's the first thing I recommend any intern)

How can I trust them?

If you don't want to rely on authority or reason, I suppose you could actually work a decade as a C++ developer and learn all these rules by trial and error. Reading a book is much faster.
Last edited on
1
2
3
void Process(float *inLeft, float *inRight, float *outLeft, float *outRight) {
    //process data
}


Can you effectively do this in Java?
References are a primitive type in Java, and References can only refer to class instances (not primitives). This means no references to references :(
It wasn't until I spent 3 days straight finishing some CS coursework that I realised I prefer C++ quite a bit over Java. It's just far more flexible; which is an aid and a hindrance in different situations but I'd rather be able to have the choices.

Also Java has a very ugly template system and I like that sort of generic programming on the odd occasion!
Last edited on
Java doesn't have a template system...it has this thing called "Generics" which lets you get Specific about the type you're dealing with and the compiler will do some invisible casts for you, but that's about it.
Yeah, a really ugly 'template'-like system then.

The alternative is using Object everywhere (which is quite unsafe and also requires a huge amount of casting).
The sole purpose of lambdas is to capture the local execution context and pass it somewhere.


There is the no-capture lambda syntax that to my knowledge behaves just like a function.


GC avoids this completely and also makes using lambdas much more convenient (noticed why C++ lambdas syntax is so ugly? because of lack of GC).


I'm finding it difficult to picture how Java's (for example) "lambda" equivalent is any less ugly. The overhead involved in doing lambda operations in Java seems on the surface to be quite heavy...but then again I've never inspected the bytecode, so I can't say for certain.
I don't know about Java's generics, but C#'s are awesome. One of the best things is that you can do this:
class Generic <T> where T : byte, short, ushort, int, uint, long, ulong
if you want T to be an integer of unspecified type. Or if you only wanted signed or unsigned types you could do that. I'm not sure if it exists in C#, but it would be better if all the signed integral types inherited from a SignedInteger class while the unsigned ones inherited from an UnsignedInteger class, both of which inherited from an Integer class which itself inherited from a Number class (which would then inherit from Object, because all classes inherit System.Object, directly or not, explicitly or not).

edit: C# also has elegant anonymous functions, mostly for events:
MyObject.EventOccurred += delegate (object sender, EventArgs args) { /* handle the event */ };
and the less elegant
Func <int,int> foo = x => x*x;
Last edited on
Veltas wrote:
The alternative is using Object everywhere (which is quite unsafe and also requires a huge amount of casting).
That's what Generics are...the compiler just hides it all from you. And the worst part is that they're still unsafe because of erasure.
Last edited on
C++ programmers seldom need GC, because we don't create so much garbage

If you don't want to rely on authority or reason, I suppose you could actually work a decade as a C++ developer and learn all these rules by trial and error. Reading a book is much faster


No, you seem to have not understood me. What I'm saying is, many smart companies / developers have actually *spent* that decade of C++ programming by trial and error and came to completely divergent rules. Some say you should write C++ like C with classes, some uses heavily templates and loathe on seeing a raw pointer, some choose something in the middle like Google (which is pretty reasonable). That is why you really never know what to expect from someone who titles himself a C++ coder (even if you know he is a smartass).

A similar problem does not exist for Java. Smart Java coders just know whole language and use all the constructs when required (except the ones purposely deprecated by Sun/Oracle, but there are really very few).

I work on a big commercial project with over 30 people involved, which already is a huge commercial success (doing over a $1M per quarter and having some top-500 companies as customers) and I don't remember us having a discussion on particular style of coding, even though we have very different backgrounds - some from academia, some were diehard C++ coders, some would immediately switch to LISP if allowed to. In the previous company I worked for, C++ guys could endlessly discuss whether you should use iostream vs stdio or how you should structure your header files, or which things to pass by pointer, reference or value, instead of doing real work - things that for Java coders are just obvious and trivial.


That's what Generics are...the compiler just hides it all from you. And the worst part is that they're still unsafe because of erasure


Unsafe? Only if you deliberately break the rules and ignore compiler warnings (casting back to old-style non-generic variants). So this is exactly the same as in C++ - if you use reinterpret_cast you are on your own. And even then, in Java it ends up by throwing an exception, so this is nowhere near the "unsafe" level you can get in C++.

At least generics are a part of the type system and not a textual substitution (macroprocessor) magic like in C++.


Java doesn't have a template system...


C++ templates are a poorman's generics imlemented by a macroprocessor, which accidentally turned out to be turing complete and became a (pretty limited) metaprogramming framework. They are even not a part of the typesystem. However, having these features separate - i.e. properly designed generics + proper metaprogramming framework is a much cleaner and practical solution (D, Scala).
Last edited on
No, you seem to have not understood me. What I'm saying is, many smart companies / developers have actually *spent* that decade of C++ programming by trial and error and came to completely divergent rules.

No, you've been misinformed or confused by some of the particularly bad or outdated examples of corporate style guides.

The amount of misinformation, from college courses and BullShieldt-style "textbooks" to urban legends and "common knowledge" is indeed a major problem for C++. Programming languages that have a central authority, such as Java, I suppose, do have the upper hand here.

Some say you should write C++ like C with classes, some uses heavily templates and loathe on seeing a raw pointer, some choose something in the middle like Google (which is pretty reasonable).
C with classes died in the 80s. Raw pointers have quite a few uses, they are just not related to their C uses. Google's C++ guidelines are crap, which appears to be driven by poorly-justified corporate requirements and some archaic code compatibility. As I mentioned, it doesn't stop Google developers from using (and improving) actual C++ where not constrained by those requirements.

That is why you really never know what to expect from someone who titles himself a C++ coder (even if you know he is a smartass)
All it takes is a simple interview.
Last edited on
C++'s template system seems to do the job it was designed for very well. It's implementation is very obvious to most programmers using it because it is all compile-time dynamics.

Java's generics system is probably more powerful, probably slower, and I know for sure it's harder to use. Some people can use Object but what I was talking about was Java's generics system that uses the same kind of syntax as C++'s template system.
Last edited on
closed account (o1vk4iN6)
I'd say Java's generic is probably less powerful. It is used only for that, generics, I can't see it being used for anything other then that compared to what can be accomplished with C++'s templates.
Pages: 1234