Live data from Hacker News

An empirical study on the impact of C++ lambdas and programmer experience

dl.acm.org

61–70 of 112 posts

Re: An empirical study on the impact of C++ lambdas and programmer experience

#61

Earlier quoted context omitted.

Basically anywhere where you would have used a callback in C code could probably benefit from a C++ lambda. It's easier to see what's going on, you don't litter your code with hundreds (or thousands) of tiny functions, and the compiler can easily inline everything. The fact that you can capture whatever you need makes it super easy to use inside a class if needed (eg; you need to access class members). It seems reall…

I'd much rather have a lot of smaller functions with single responsibilities, but then being middle management I worry about things I didn't when developing. I need the code to be SOLID, I need the time to market to be as small as possible and I need to be able to replace any developer with any developer on a moments notice. When students don't know lambdas you're costing me money by using them, because you made the…

> I need to be able to replace any developer with any developer on a moments notice.

As unpleasant as it is, this is all too widespread, because it seems like common-sense on the surface. "A little knowledge (on the part of management) is a dangerous thing."

Re: An empirical study on the impact of C++ lambdas and programmer experience

#62

This is ridiculous. C++ lambdas (and std::function) don't replace iterators except for the most fervent disciples of the Church of Haskell. They replace single-function interfaces in cases where you would have had to put together a custom struct that did exactly the same thing as a lambda with capture but in about 15 more lines.

Yes, especially irritating in C++2003 here at work! you realise how useful the capture list is with lambdas when you have an in-function struct that can't see outside its own scope so you need to pass everything in (and won't have access to private portions of the outer class).

Lambdas really are great.

Re: An empirical study on the impact of C++ lambdas and programmer experience

#63

Earlier quoted context omitted.

Basically anywhere where you would have used a callback in C code could probably benefit from a C++ lambda. It's easier to see what's going on, you don't litter your code with hundreds (or thousands) of tiny functions, and the compiler can easily inline everything. The fact that you can capture whatever you need makes it super easy to use inside a class if needed (eg; you need to access class members). It seems reall…

I'd much rather have a lot of smaller functions with single responsibilities, but then being middle management I worry about things I didn't when developing. I need the code to be SOLID, I need the time to market to be as small as possible and I need to be able to replace any developer with any developer on a moments notice. When students don't know lambdas you're costing me money by using them, because you made the…

This is amusing and I think (hope?) it's sarcasm.

Interestingly, you can think of lambdas a small functions with single responsibility, which happen to be easy to find (they're inline right there in the code and you don't have to hunt around for them).

Re: An empirical study on the impact of C++ lambdas and programmer experience

#64
post #20

Earlier quoted context omitted.

Unfortunately most teachers aren't like Kate Gregory, and I fully agree with her, they should stop teaching C -> C++. Already in 1994 it was obvious to me that it leads to bad unsafe code and worse, the mentality to micro-benchmark each code line.

There's pitfalls to everything. I find that a lot of people that only learned C++ end up writing nice-looking safe code that's very well organized but has awful memory usage and dubious performance characteristics. It's not that they're bad programmers or made poor algorithmic choices. It's that they've been indoctrinated with the fear of "premature optimization". Moreover, they don't really know any better if they d…

Out of interest, I never learned C but went straight to C++. What in particular about C++ makes you believe that you'll write memory hogging programs?

Re: An empirical study on the impact of C++ lambdas and programmer experience

#65
The only true conclusion of this study is that students find C++ a bit intimidating.

Which is not surprising at all, because C++ is a very complicated language !

It takes years, even decades before you can master this language - even then you probably won't be using all of its features.

Back in the day, it took me a long time before I could truly grasp the C strings and pointers in general.

Yes, I could write code with pointers and C strings (new char[length + 1] and delete[] str; were my buddies!), but sometimes I was programming with my eyes closed and fingers crossed, because the implications of sharing raw pointers were too complex, especially if threads were involved.

Same with these new features. They may 'look' more modern due to the updated syntax, but the underlying concepts that they are hiding still need to be well understood.

So not surprising that students struggle with getting it right the first time. The good news is that with a bit of experience this becomes easier, as with anything else..persistence is key.

Re: An empirical study on the impact of C++ lambdas and programmer experience

#66
post #4

I used to really like C++ ... back in 2000. That is before they added the kitchen sink into the language. I prefer my computer languages to be like Chess - a few rules but efficient and expressive. Like C. Why? Because everyone can read the code others on the team wrote, without being a language lawyer and knowing tons of esoteric features and magic. That has implications for maintainability and team productivity, an…

Admittedly there is a lot to learn to truly master C++. But I find RAII (and particularly move semantics) far more usable and manageable than the C-style C++ code I have seen.

As an example, you do not have to be a language lawyer to understand that C-style casts are horrible and dangerous, and really will lead to colossal implications for maintainability, team productivity and the bottom line for a business (it is a static_cast or reinterpret_cast? Who knows?!)

If you're employing people who only know half of the language or never finished chapter 1 of Stroustrup's C++ book, then I would say it is a problem with your employees than the language.

Re: An empirical study on the impact of C++ lambdas and programmer experience

#67

The only true conclusion of this study is that students find C++ a bit intimidating. Which is not surprising at all, because C++ is a very complicated language ! It takes years , even decades before you can master this language - even then you probably won't be using all of its features. Back in the day, it took me a long time before I could truly grasp the C strings and pointers in general. Yes, I could write code w…

Yes C++ is complicated and yes students struggle with programming. It takes years to understand how to put a decent program together (separation of data, logic and transport etc).

As for your guesswork with C-style strings, you should have been using std::string and using copy constructors to pass work into a thread (eg. who owns the data the thread is working on?). With modern things you can move the data into the thread instead of copying it.

I would recommend students working on their own personal projects as a good pasttime as it further helps anyone understand how to write a decent program and learn the language. Also, tell them to read Stroustrup's book.

Re: An empirical study on the impact of C++ lambdas and programmer experience

#68
post #3

> After instructions, participants were given printouts of sample code they could refer to while solving tasks. Group Lambda got code of a C++ program using lambda expressions and group Iterator received code of the same program written using iterators. They then had time to study the samples before starting the tasks and could refer to these samples later. These samples do not appear in the paper, so we don't know w…

I agree that the "iterators" in the paper are not C++ iterators as found in the STL or language spec.

Re: An empirical study on the impact of C++ lambdas and programmer experience

#69

Earlier quoted context omitted.

"Idiomatic" doesn't necessarily mean better. I think objectively it's hard to argue that "item != market.end()" is superior to "iter.hasNext()". The latter accurately reflects the programmer's intent, while the former specifies an unnecessarily specific (and poor) implementation of the intent. First of all, using something like "market.cend() != const_iter" instead is arguably better practice (imagine you unintention…

> But programmers shouldn't need to consider whether the iterator is const or not when they just want to know if the loop is done. and they don't: http://en.cppreference.com/w/cpp/container/vector/end - there's an overload returning a const_iterator. You don't need to use 'cend'. And since insertion and deletion potentially invalidate iterators, 'hasNext()' is just as bad.

Yeah, maybe I didn't think the const thing through. I guess I was thinking that the benefit of using cend() is the double-check to make sure the iterator was declared as a const_iterator (when appropriate). But using "hasNext()", just like using "end()", you would lose the double-check.

Perhaps I could instead claim that "item != market.end()" redundantly specifies the vector, "market", and thus presents an unnecessary opportunity for mistakes? For example:

    std::vector x_coords;
    std::vector y_coords;
    ...
      
    for (auto y_iter = y_coords.begin(); y_iter != y_coords.end(); y_iter++) {
        for (auto x_iter = x_coords.begin(); x_iter != y_coords.end(); x_iter++) {
            ...
        }
    }
Notice the "x_iter != y_coords.end()" bug. I assume this will usually trip an assert if it is encountered in debug mode, but not in release mode. Of course you could just as easily mix up "x_iter.hasNext()" with "y_iter.hasNext()", but the removal of redundancy means one less opportunity for a potential mistake. Right? Hmm, I guess that's really an argument for losing the iterators altogether, which I guess was kind of the point of the study.

So then wrt iterator invalidation, I have a question. Consider this contrived scenario:

    for (auto y_iter = y_coords.begin(); y_iter != y_coords.end(); y_iter++) {
        if (5 == std::distance(y_coords.begin(), y_iter)) {
            y_coords.resize(3);
        }
    }
With conventional implementations of std::vector, the "y_coords.resize(3)" will presumably "invalidate" y_iter. And the "y_iter != y_coords.end()" will result in undefined behavior. But you could imagine "safer" implementations of vector that would instead throw an exception (or terminate or whatever). (Or you could actually download one of them at the link I gave.) So the question is, if this "safer" implementation supported "y_iter.hasNext()", would it be better for it to throw an exception (or whatever) in this case, or just return false?

Re: An empirical study on the impact of C++ lambdas and programmer experience

#70
Paper says that "those in lambda group received no benefits in regard to [...] compiler errors".

Really, would anyone expect lambdas to improve compiler errors? Also, isn't that a problem of the compiler, and not of the lambda idiom in C++ or C++ language itself?

I mean, idk about clang, but at least compiler errors from msvc and gcc have always been hard to understand, with or without lambdas.

Post reply on HN