News for hackers. Stuff that matters.
Re:Talking to C Programmers about C++ [video] by gravypod on Sunday September 25, 2016 @08:29AM (#12575040)
The problem with C++ is the constant introduction of unneeded features that are infinity obscure in necessity.

People will say "Oh just don't use that feature" but that's not how this works. If something is there it is used. In reference to C, I don't think I know of a single feature or release that had major changes in how I wrote C. Maybe C99 and allowing me to declare in a for loop.

I've been using C++ for class this semester and the only thing I like is reference types. Makes pointers much more concise. Other then that it can all go in the bin. In order to keep my C++ project clean I've needed to avoid a huge set of features and keep it in a data-oriented design.

Re:Talking to C Programmers about C++ [video] by jonstewart on Sunday September 25, 2016 @09:13AM (#12575161)
The problem with posts about C++ on Hacker News is that people who aren't experienced C++ programmers (you've been using C++ for a semester? do tell) feel compelled to write posts explaining the problems with C++.
Re:Talking to C Programmers about C++ [video] by yosefk on Sunday September 25, 2016 @09:28AM (#12575214)
As someone who's had to deal with C++ since the last century, I find that one problem with C++ is that people who aren't experienced C++ programmers can't get shit done, especially in a code base where experienced C++ programmers have demonstrated their erudition and knowledge of C++ arcana in all its glory. In fact, the intersection of people who had the patience to learn enough C++ to get shit done and people who also have the patience or basic desire to actually get shit done (as opposed to programming wanky wrappers and stuff) is annoyingly small (this however is not true everywhere, I'd expect no such problem to exist with game programmers, for example.)

The fact that a language is terrible for newcomers is a real problem, and a newcomer is a very good judge of that even if they aren't a very good judge of other aspects.

Especially loathsome are those people who teach, say, undergraduates majoring in physics some C++ as a way to introduce them to programming. You might think that someone who manages to deal with the math needed to do physics would be able to absorb C++, and often you'd think wrong; one physics undergrad told an experienced programmer after trying to understand what the fuck cout<<x actually does, "your head is full of garbage!", by which he meant that the amount of completely arbitrary rules that you need to memorize was not something that dealing with math and science prepared him for.

From the above I conclude that people who have a semester's worth of experience in C++ might not be C++ experts but they certainly know enough to burn the instructor who chose C++ in the first place at the stake.

Re:Talking to C Programmers about C++ [video] by Koshkin on Sunday September 25, 2016 @10:26AM (#12575453)
I agree. To truly learn and appreciate programming, you have to be able to enjoy it, first and foremost, and, unlike, say, Pascal, Python or JavaScript, the languages like Java, C, and especially C++ make that extremely hard for a novice programmer. This is because before you even begin to appreciate the ideas based on which those languages were designed and the languages' sophisticated features, you need to face the problems that these languages are trying to solve, and that takes time and programming experience.

It may sound strange, but I find assembly to be an appropriate language to learn programming. Not only your focus will be on what you are doing rather than on the language itself, you will get a feeling of what is really going on, and you will quickly appreciate - and fully understand - the the reason why C is useful and that it is a good, well designed programming language. (In the same way, with enough experience using C, one comes to appreciate C++.)

Re:Talking to C Programmers about C++ [video] by sseagull on Sunday September 25, 2016 @10:18AM (#12575414)
> what the fuck cout<<x actually does

I'm not sure where that conversation would go. It prints to stdout. Would the student say the same thing about printf("%d", x)? How far down the rabbit hole did he want to go?

Stream insertion syntax os pretty bad, though, I'll agree with that. Especially when you want to print floating point.

Re:Talking to C Programmers about C++ [video] by gravypod on Sunday September 25, 2016 @10:59AM (#12575598)
The conversation then shifts to how it does this? What if I want to print to something else? Why if I want to read differently from a stream?

When you provide a shorthand that's all well and good but when you TEACH a short hand it's what's ONLY used by the students.

The students will assume there is no other way to read and write then using >> and << which is horribly incorrect. These are largely inadequate measures for reading and writing in my experience as you probably want to do formatting which printf just does better.

So many of the students in my CS class this semester can't do the first lab, which in all essence, is a tokenizer because they don't know that there are other ways for writing to, or reading from, a stream!

A short hand should only be used in a majority if it can do everything the long hand way can do.

Also for anyone interested what my first C++ project looked like you can see here:

https://web.njit.edu/~gwryan/CS280/CS280-Program-1-Fall-2016...

https://git.gravypod.com/gravypod/school/tree/master/cs280/h...

It's not my best work, but if any C++ people want to notify me of things I'm doing that's wrong I'm more then happy to learn.

Re:Talking to C Programmers about C++ [video] by sseagull on Sunday September 25, 2016 @11:29AM (#12575721)
I will agree with the point that the other methods of reading/writing from streams aren't taught early enough, and that formatted output is pretty bad with streams. I've actually written a wrapper around snprintf for printing in c++ (I should put that on github sometime)

Your project looks good. Two minor observations: 1.) You have 'using' statements in a header file. This is fine for a small project, but is considered bad form in larger projects (every file that includes that header will end up including the 'using' statements. This can cause collisions in large projects).

2.) Const correctness is a good habit to get into. (Ie, only pass non-const references if you are actually changing the object in the function)

Re:Talking to C Programmers about C++ [video] by gravypod on Sunday September 25, 2016 @11:39AM (#12575765)
I wasn't sure how const worked in this language so I decided not to mess with it yet.

Coming from Java I have a blanket policy that every variable is final unless needed to be modified. I've run into those pain points.

Your first observation is one I have some into as well but I haven't been taught how to do this.

For example if my header file needs to define the method:

   void test(std::string something);
Is it bad form to them define the header in my cpp file as such?

   void test(string something);
Isn't that also bad form? Coming from C some things like this could be done but very much shouldn't have been done.

I know for a fact that namespacing is one of the hugest benifits to large projects I just don't know how to use the tools provided by C++ to do it correctly. Java? Yea I can get anything you want nice and tucked away. But C++ with the way headers and namespacing works its difficult to obviously see what I want to do and how to get it done.

Any examples?

Re:Talking to C Programmers about C++ [video] by jonstewart on Monday September 26, 2016 @09:53AM (#12581791)
I'm not sure I understand the question completely. Do you mean "declare" instead of "define" in "if my header file needs to define the method"? Or are you asking about declaring the parameter to test as "std::string" in the header but then only as "string" in the .cpp definition, because of the presence of a using statement?
Re:Talking to C Programmers about C++ [video] by MichaelMoser123 on Tuesday September 27, 2016 @09:41AM (#12589485)
> trying to understand what the fuck cout<<x actually does

You can always do fprintf instead of that; i almost never do C++ streams for io - there is always a way to avoid them.

Re:Talking to C Programmers about C++ [video] by gravypod on Tuesday September 27, 2016 @11:38AM (#12590399)
I have also never seen a codebase that has used cout piping that was worth any salt.

But then why is it the first thing we shove into peoples faces when teaching them the language?

Re:Talking to C Programmers about C++ [video] by MichaelMoser123 on Saturday October 01, 2016 @05:34AM (#12617186)
i think they want to make an impression: C++ is supposed to be a new language, and not just C with a class system + templates bolted on top of it. i guess streams is supposed to be the new way of doing IO, never mind the circular inheritance mess and virtual function calls for getting new data.
Re:Talking to C Programmers about C++ [video] by pjmlp on Sunday September 25, 2016 @10:27AM (#12575456)
I was part of the C++ teaching group on CS labs on my university back in 1999.

The majority of them surely were able to get shit done in C++ at the end of the semester.

Re:Talking to C Programmers about C++ [video] by _yosefk on Monday September 26, 2016 @10:47AM (#12582249)
Was it their first language though, and more generally, what did they already know?

Don't get me wrong, I remember the people who taught me C++ rather fondly. It's just done really terribly really often.

Re:Talking to C Programmers about C++ [video] by metaobject on Sunday September 25, 2016 @09:42AM (#12575268)
This problem is not limited to HN, I see it everywhere. And wrt to the notion that every feature of C++ (from simple, to complex, to highly complex) will be used, I've been using C++ professionally for ~13 years and there are certain features I've chosen to stay away from since they don't add any benefit to the problems that I solve (or to the way I solve them). And I feel fine about that bc I'm very pleased with the performance and abstractions I can create with C++.
Re:Talking to C Programmers about C++ [video] by MereInterest on Sunday September 25, 2016 @09:58AM (#12575329)
As somebody who is relatively experienced with C++, but primarily on small projects with few developers, which features do you tend to stay away from?
Re:Talking to C Programmers about C++ [video] by Koshkin on Sunday September 25, 2016 @10:55AM (#12575593)
I found it helpful to think about (the modern) C++ as a combination of two languages: one is a relatively straightforward language for application development, almost on par with Java or scripting languages such as Python; the other is a very sophisticated tool for implementing universally useful, generic classes and libraries.
Re:Talking to C Programmers about C++ [video] by gravypod on Sunday September 25, 2016 @10:52AM (#12575578)
So my opinion on the difficulty of learning features in a language are not valid since I'm just learning the features?

Bravo.

My problem is not with what I think you are attributing to me but rather other issues with the language and tool chain. There seems to me very little that can be done in C++ that can't be done cleaner in C.

It's evident that most people who use a language aren't going to have time to spend years becoming a master at it. They are going to join a team and say "So to do my assigned task I need to do X, what features let me to X the cleanest" and this is how I've taken on learning C++.

Sadly, no features that are new have yielded themselves to any problem I've seen. I haven't encountered it.

Your solution to rebut my argument is that "you need to be at least 10 years into the language to know anything kid!" yet you provide no rebuttal.

Please do provide an argument not an insult at my age the next time you'd like to raise a disagreement with my ideas.

Re:Talking to C Programmers about C++ [video] by tossaway1 on Sunday September 25, 2016 @11:06AM (#12575627)
Well you didn't express your criticism a the reason C++ is difficult to learn, you described it as "the problem with C++" and it sounds like you don't have enough familiarity with the language to make that sort of assessment.

C++ isn't a great choice for many purposes, but for some purposes it is the go-to language - and for good reasons, not just because of inertia as some like to suggest.

As with all forms of craftsmanship, you need to choose the right tool for what you're trying to accomplish.

Re:Talking to C Programmers about C++ [video] by gravypod on Sunday September 25, 2016 @11:35AM (#12575745)
Here is why C++ is difficult to learn. It's like my experience of dealing with Java's inherited state except at every point in the language.

For people who have never touched java's inherited state from extending abstracts or base classes please don't fall into the traps I did. I was writing a redundant networking library that was meant to load packets on the fly so I had some cleaver "c++ level" abstraction written out for it. It only made sense to me because I relied on horribly complex features of Java. I'd say the most complex feature of Java.

Read through this to get an idea of what's going on. You'll notice that this is one of the sections with the least detail in the entire Java Primer thing. This is because in my opinion even the Java developers don't know all the rules.

https://docs.oracle.com/javase/tutorial/java/IandI/multiplei...

Now I don't think I know these rules either. I spent 2 months sorting out this mess and ended up with a polished product. But guess what? I could never go back there and fix something "quick". I'd need to do a bit of reading, read through my source, experiment a bit. This is not productive nor good standards and because of this I try never to rely on inherited state in Java anymore.

The same goes for every pain point of C++. Sure an expert, someone who lives, breaths, and dies by C++ might know exactly what is going on from the get go but that can't just be the assumption for every programmer. It is my opinion that if a language designer expects someone to read up on the language before going in and changing things they have failed at one of the cornerstones of language deign and implementation.

Everything should be "of course" levels of ingenuity but in C++ everything is "what the fuck".

For instance, please read my comment on RAII.

You can also take the multiple inheritance problems I've had in Java and directly apply them to C++.

You've also got strange behavior, when coming from C, for dealing with strings. For instance, where does an std::string get allocated? Is it on the heap or stack?

For instance am I opening myself up to bugs by doing this in my latest CS lab?

https://git.gravypod.com/gravypod/school/blob/master/cs280/h...

There's no apparent flow or rhyme to anything in this language. It just seems to be complexity bolted on simplicity and called "an industry standard".

Also operator overloading. I'd say it's a great thing for defining numeric and mathematical constructs that actually implement the specific operators as you would expect. For instance speaking as someone who has implemented their own basic physics library in java that is a godsend. But why, in the name of god, would you ever think of using numeric operators on non-numeric constructs? That's just inconsistency again leading down the further train of C++ needing to be learning in full before used.

Further, namespacing is taught poorly. I've had to do my own reading and settle on a standard that I think is semi-sane at least.

Rather then using the entire std namespace the same as my class I've settled on specifying namespaced elements I want to use. This is not how anything is taught in any college I've examined.

An example of what I do:

https://git.gravypod.com/gravypod/school/blob/master/cs280/h...

What's the point of using a namespace if you are just going to blanket import the entire namespace? Also if that's not the SOP for industry why is it taught as such in college?

There are many many if-ands-and butts about C++ and I can't go into all of them as I don't know all of them.

I do like reference types though, that's nice.

Re:Talking to C Programmers about C++ [video] by jonstewart on Monday September 26, 2016 @10:17AM (#12581967)
You've admirably demonstrated the willingness to ask questions and learn in this thread, so I will try to rise to the occasion. I'm sorry to have been dismissive with my original comment... please understand that C++ takes a lot of abuse from inexpert programmers on HN. For example, I never would criticize Ruby without spending more time than a semester's class using it.

>> string token = "";

No, you aren't opening yourself to bugs, but it's unnecessary. A default constructed string is already == to "", token.empty() will return true.

get_token_type() should likely take a const reference to the string, as all of the operations on the argument are const. I saw elsewhere in the thread where you said you've avoided const so far. Honestly, const may be C++'s greatest feature; it is far, far stronger than Java's "final" (or Scala's equivalent "val").

Looking over this code from a high level, I'm not sure why you have get_token_type() with a string parameter and a char parameter, when the former just forwards to the latter. Couldn't you have a second char variable, "prev_char" or "last_char", in get_next_token() and then detect when the type of it differs from the current char. Then the only operation you'd be doing on the string "token" is appending to it. I could be missing something, though, as I've only taken a quick glance through the code.

I would generally agree with you that C++ is poorly taught almost everywhere. I think it was a win for CS departments when they transitioned from C++ to Java for introductory courses (and now many are transitioning to Python, also a win). I am fairly convinced that C++ programming is a craft and is therefore best learned under an apprenticeship model, of routine practice with expert review and guidance, coupled with some self-study.

Re:Talking to C Programmers about C++ [video] by Koshkin on Sunday September 25, 2016 @11:09AM (#12575640)
There are many insightful comments in this thread (as well as others) that may provide arguments you are looking for.
Re:Talking to C Programmers about C++ [video] by gravypod on Sunday September 25, 2016 @11:19AM (#12575690)
I've been reading some of them through and some of them are actually fairly well rounded. Let's see how they respond to the questions I've posed to them.

I'm specifically interested in the RAII question to see how they justify fundamentally different behavior for syntactically similar code.

Re:Talking to C Programmers about C++ [video] by Koshkin on Sunday September 25, 2016 @11:39AM (#12575764)
You seem to dislike the idea of the default constructor, not RAII. Default constructors, at least for relatively simple types, are meant to be minimalist so as not to incur any significant overhead. Which makes them comparable in this regard to the "default constructor" of 'int'.
Re:Talking to C Programmers about C++ [video] by sseagull on Sunday September 25, 2016 @09:25AM (#12575209)
I've been using C++ for a long time (10+ years). At one point (around C++11), I decided to really embrace the advanced features (template metaprogramming mostly) and stop trying to make the language something it wasn't.

It can be complicated, for sure. But there is a certain plesantness that comes with letting the compiler do a lot of work for you. It can almost be like the switch to python - just let the compiler figure stuff out.

This results in a lot less duplicate code, etc. The cost is some pretty bad syntax and compile time, though.

Try to keep an open mind

Re:Talking to C Programmers about C++ [video] by gravypod on Sunday September 25, 2016 @11:16AM (#12575679)
I mean I've been programming for some time and I come from a strong Java background. I get the basis of what C++ is trying to do with their new features but I think it's horribly executed when looked at in comparison to other modern languages like Java and Rust.

Speaking from a very strong Java background after Jigsaw I think Java will outdo the generics capabilities present in C++ in every way. Java 9 is bringing some crazy features to the mix. From what I understand they are getting rid of the boxing to object from generics to keep types static through lists and other generic elements. That's amazing and something C++ still does as templates are basically massively duplicated code in your binary while the JVM will get this for free at runtime.

Rust is also just out of left field as they are doing amazing things with their "cost-free abstraction"

Re:Talking to C Programmers about C++ [video] by hackits on Sunday September 25, 2016 @08:54AM (#12575111)
When I think about features in C++ that I would love to have in C, the only thing that comes to mind is constructors and deconstructions for RAII.
Re:Talking to C Programmers about C++ [video] by gravypod on Sunday September 25, 2016 @11:12AM (#12575655)
My issue with RAII is that it's fundamentally not what you expect to happen.

If I say:

  int i;
I don't expect that to create an integer. I expect that to reserve memory in the program's space for an integer.

In the same way I don't expect this to call malloc:

   int *i;
I just expect that to hold the space in the program data where and int pointer can be stored.

Furthermore I don't expect this to run any code (just as the other two).

   string s;
I don't expect that to construct anything, that's just a place for me to put data. This way, we know not to use data before it's initialized.

In C++:

   int i;
   if (i > 10) printf("Greater then 10\n"); 
Is that going to always work how we expect it?

How about:

   string s;
   if (s.length() > 10) printf("Len is greater then 10\n");
That is going to always work how we expect it since it was constructed.

This is fundamentally different then uninitialized integers or primitives so I dislike it's use.

I strongly dislike the use of this term but it in my mind is an "Anti-pattern" as it promotes two separate ways of thinking about variables that are fundamentally completely different.

Info taken from:

http://www.cplusplus.com/reference/string/string/string/ http://www.cplusplus.com/forum/general/67249/

Now I'm no expert in education but this knowledge has taken me years to learn through my life and I don't think I'd have been able to do it if I didn't LOVE computers and programming.

I don't know how someone would think that this is good behavior because of it's inconsistencies between two outwardly similar cases.

Re:Talking to C Programmers about C++ [video] by hackits on Sunday September 25, 2016 @11:25AM (#12575708)
Hi gravypod.

It depends. Oh I hate that word.

When I see int i; without context I don't know anything about where memory is allocated.

It can either be the following. 1) Stack allocated 2) Heap allocated 3) Is in the read only data segment.

Just starting with a int, it can be any of the above three, two that reserve the memory on the heap/stack and another that is only a read only part of memory. The heap is the only place where I would need to call malloc/free.

Though for most common libraries when I see string s; They mostly have a pointer null terminated string in memory, and the primitive data types residing as the member's of string.

RAII helps in the situation where instead of having to do tedious code such as.

String s; allocate_string(&s);

// do something with string. free_string(&s);

It can all be done via a standard convention of calling the deconstructor or constructor to initialise and free itself.

Re:Talking to C Programmers about C++ [video] by gravypod on Sunday September 25, 2016 @11:51AM (#12575814)
Isn't "It depends" part of the problem?

For instance my professor when explaining to the other students why we use context-free grammars for parser he said "we always want a language to work how we tell it to. An if doesn't mean something different depending on where it is"

While I do agree that he is incorrect and that is a very close minded, and out dated, view of language design he is accidentally correct in another sense.

I'd like to ask "why" C++ chooses int i is different based on the location. I know the answer myself, but my peers don't and probably wont until years of pain in industry while parroting the same nonsense that C++ is the best language same as all of the other loons who think that there is a "best language".

Why is it ok for struct packing, for aligning data, and for initial memory state to decide how the program behaves?

In C++ it does, in C it's more sane but it sill happens.

> RAII helps in the situation where instead of having to do tedious code such as.

How is:

   string s = "";
and

   int i = 0;
Tedious? That seems like the sane alternative as it puts the behavior in the hand of the programmer. In nearly every language this is decided as the preferred way of allocating variables. Define AND assign to create something useful.

I don't think I understand how adding more case-specific behavior that doesn't apply to everything should be considered sane. It's more edge cases that aren't needed.

It's much simpler to say "we are just going to hold a slot in memory for any object, you need to initialize it" or to say "we are going to have default initialization for EVERY object even primitives" but it's horrible to say "we're going to do a combination of both and you have to figure it out"

Re:Talking to C Programmers about C++ [video] by jonstewart on Monday September 26, 2016 @10:01AM (#12581852)
I think part of your expectation may be coming from Java, where only fundamental data types are stack allocated and everything else has reference semantics to heap-allocated objects.

For the sake of syntactic consistency, one could argue that C++ should mandate "()" as a suffix to declarations to invoke the default constructor. However, what good is an allocated object that's _not_ constructed? For many classes, of course, the default constructor is trivial, but for others it isn't. The purpose of the constructor is to provide invariants that the code can rely on--it isn't obvious to me what common use case you could consider for an object that's allocated on the stack but not constructed, and it seems like it's asking for trouble.

Also, since C++ inherits C's syntax and semantics for fundamental types, "int i;" must be legal. So we cannot abolish the form.

Re:Talking to C Programmers about C++ [video] by Koshkin on Sunday September 25, 2016 @10:38AM (#12575511)
Problem is, using destructors (and RAII) may have, in general, certain performance implications that C may not be willing to deal with. This is somewhat similar to demanding that C had a garbage collector built in. Note that, being lazy, memory management by means of garbage collection may turn out being more efficient than using the (eager) destructors.
Re:Talking to C Programmers about C++ [video] by hackits on Sunday September 25, 2016 @10:51AM (#12575572)
Don't see how there could be any performance implications with having RAII within C. It would be a simple method call with the this pointer to the struct during complication. Though if your performance consideration is at the assembly code level why use C?

I've had some good/bad experiences with garbage collectors. They do help improve productivity and ROI within the enterprise space where memory isn't too much of of issue. Then when memory does become a issue within smaller memory footprint environments they can be a real pain in the ass.

Re:Talking to C Programmers about C++ [video] by gravypod on Sunday September 25, 2016 @03:39PM (#12576998)
If anything this video shows us that there is probably a lot less of a performance hit actually is take.

That's one of the things mentioned in the source video.

Re:Talking to C Programmers about C++ [video] by KKKKkkkk1 on Sunday September 25, 2016 @11:00AM (#12575603)
The other thing about C++ is that you start by saying "I'll write C and only use this one extra feature from C++." and thereby you open a Pandora box. You decide to use reference types, and all of a sudden your classes are not default-assignable.
Re:Talking to C Programmers about C++ [video] by gravypod on Sunday September 25, 2016 @11:53AM (#12575823)
I just don't use classes. I'd like to but again it's just not a good idea.

I also don't like default constructors see my other post as to why I think they are not reliable behavior to the programmer if they don't have experience in the language.

Again I didn't lie when I said "I am using C in C++" as I am using C in C++.