Live data from Hacker News

C++ 11 Auto: How to use and avoid abuse

acodersjourney.com

41–47 of 47 posts

Re: C++ 11 Auto: How to use and avoid abuse

#41
post #38
post #37

Earlier quoted context omitted.

On my case, they provide all the necessary automation features I care about.

Out of curiosity which Language and IDE do you use?

Java, Scala, F#, C#, C++, JavaScript, SQL.

VS, Netbeans, Eclipse, Android Studio, SQL Developer.

Re: C++ 11 Auto: How to use and avoid abuse

#42
post #6

"auto a = ConjureMagic();" "SetMagic(a);" The problem here is actually is an old one of failing to separate a getter from a command. It looks like ConjureMagic is causing side effects and modifying the state of whatever class it belongs to. This is also the reason one can't answer the question "what the heck is a?". If the "ConjureMagic" is only a getter and does not modify class state, it may probably need a better…

Having Getters for each member variable always seems fine and reasonable. It's when you have getters that do "magic" that I feel a little.. unsure. Like if it's taking a member and returning it in a different unit that seems kosher... But there is a fuzzy line where at some point the Getter is doing too much work to genuinely be a getter. It gives a false impression for the internal structure of the program. But conv…

no, having getters for each member variable is a sign of pervasive action in distance and lifetime problems.

Re: C++ 11 Auto: How to use and avoid abuse

#43
post #10

Earlier quoted context omitted.

I would be very surprised if someone can meaningfully review C++ templates without an IDE. Really not reviewing a program inside an IDE is a poor idea in general.

Some of us still use emacs and vim.

To clarify, with plugins such as ctags, rtags and YouCompleteMe. I would never be able to write large C++ programs in vanilla vim. Maaaybe plain C, but definitely not C++.

Re: C++ 11 Auto: How to use and avoid abuse

#44

I'd be more than annoyed if I saw someone name a function "xxInteger()" because it returns an integer. I don't actually think the first example was that bad, modulo some context. Sometimes even knowing the type for this kind of thing isn't super important to understand what the code is doing; ala opaque types.

Coming from the Apple ecosystem, descriptive names are par for the course. Most code I end up with is written by autocomplete anyways, so writability is less relevant to me. However, when I go back and read the code later, I want to know as much of the intent of the piece as possible.

Apple publishes some very good guidelines for Objective-C and Swift, the latter here: https://swift.org/documentation/api-design-guidelines/

Generally, I'd name this method according to the role of the result, and not it's type. While "xxInteger()" isn't good, "numberOfXX()" returning an integer type does improve readability and would still be understandable assigned into an auto.

Remember, you're not writing code for you, you're writing code for future you. Or worse, for the next guy who comes into the codebase. They should be able to derive your intent without having to figure out what each method signature is, and reading all the API docs due to lack of code readability.

Re: C++ 11 Auto: How to use and avoid abuse

#45
post #22

Earlier quoted context omitted.

Would you mind giving some examples? I don't find STL to be crap at all.

std::unordered_map ::iterator it = hashmap.begin(); vs auto it = hashmap.begin(); I find auto useful for cutting down some of the verbosity of templates STL containers, but I can see how over use can lead to code requiring much more referencing if maintaining code that rarely defines types.

Oh no, I agree with that. I was specifically talking about fenesiistvan's point about his STL wrapper. I was wondering what his use cases were.

No doubt auto is extremely useful in a case like the one you mentioned. In fact, even more so in here:

for (std::unordered_map*>::const_iterator it = a.begin(); ...)

Re: C++ 11 Auto: How to use and avoid abuse

#46
post #45

Earlier quoted context omitted.

std::unordered_map ::iterator it = hashmap.begin(); vs auto it = hashmap.begin(); I find auto useful for cutting down some of the verbosity of templates STL containers, but I can see how over use can lead to code requiring much more referencing if maintaining code that rarely defines types.

Oh no, I agree with that. I was specifically talking about fenesiistvan's point about his STL wrapper. I was wondering what his use cases were. No doubt auto is extremely useful in a case like the one you mentioned. In fact, even more so in here: for (std::unordered_map *>::const_iterator it = a.begin(); ...)

I am used with the old Delphi / C++ Builder style. I think that it is much more clear than than bot the STL and the new trendy C11 style.

So my hashmap handling looks like this:

MHashMap userlist = new MHashMap>();

HUser *user = userlist->First();

while(user) { user->DoSomething(); user = userlist->Next(); }

Re: C++ 11 Auto: How to use and avoid abuse

#47
I think one of the key things that makes 'auto' much more painful than 'var' is that in Visual Studio, the intellisense in C# is much more reliable than C++. Any ambiguous statement like the first example is quickly and resolved in C# with a quick F12. In C++, this doesn't always work especially when you're using any kind of cobbled together or hybrid build system.

A lot of times, the type is unimportant but when it is, this kind of usage can bloat a simple task into 5 to 15 minutes of hunting through headers or grepping.

It's strange to think of an IDE feature so impacting a language feature. The stack is not supposed to affect in that direction but this is one case where it really does.

Post reply on HN