-9

It seems that lately pretty much every C++ question on SO that involves any pointer indexing whatsoever leads to the comment "use vector!" It doesn't matter if the OP is using the pointer correctly or incorrectly or even if the OP's problem is related to the pointer at all.

The latter is the worst kind. See, for instance, this question. OP has a syntax error, first comment (which has 5 upvotes) tells OP to use vector because his class is broken, even though the lack of vector use is not the problem.

While vector is a really useful container, and there are certainly lots of cases where OP should use it, it's not the answer to every question ever, and I think it lowers the quality of SO to basically have "use vector" be our version of "first!" Does anybody else feel the same way or am I just a grumpy old 20-something who actually thinks that pointers can be useful too?

5
  • 6
    It's a comment, not an answer. Should we avoid giving advice because it doesn't answer the OP's question? Then they'll never learn. See also Pounding A Nail: Old Shoe or Glass Bottle?
    – user3920237
    Commented Nov 8, 2014 at 23:43
  • Yes, I noticed that as well, and I don't even follow the C++ tag systematically. I don't think you can do much about it beyond downvoting the answers if you don't like them. Commented Nov 9, 2014 at 0:07
  • 2
    Who's supposed to decide what's excessive, and what's the right amount of warning against writing crappy code? Commented Nov 9, 2014 at 0:40
  • 1
    This cargo-cult attitude is endemic. Same as 'use a timer instead of sleep', 'you must change your network design to async' and 'you must cleanly terminate all secondary threads with join() before terminating your process'. It's all bullshit, but it gets votes from clueless voters 'cos it's cool. Nothing wrong with std::containers, but the explicit, or implied 'must' is extremely annoying. Commented Nov 9, 2014 at 1:10
  • @Martin I agree with it being cargo cult if a reasonable justification is lacking. See my answer.
    – user3920237
    Commented Nov 9, 2014 at 1:22

3 Answers 3

5

Why is this a problem to you? vector is how you solve most of the real problems in many questions. Sure, a (bad) question might be about a syntax error, or about an algorithm, or about whatever else, but if no-one points out what is really wrong with the code, OP will never learn. Especially since the majority of people who ask here don't read (good) books and would probably never hear about vector otherwise.

"Using a pointer correctly" means you assume expert-level knowledge, but usually it's clear from the question that the person asking it doesn't have the level of knowledge to be expected to usually get pointer arithmetic and dynamic memory management right.

Also you only mentioned comments. Comments are exactly that - they are meant for commenting on a question. "Use vector instead of manual memory management" is a comment on a question. Again: if it's not given as an answer that doesn't answer the question, the problem you claim exists doesn't seem to be real.

3

If an answer is particularly unhelpful to the OP's immediate question, downvote it. If an answer has some hope of actually being useful, and you agree with it, upvote it.

Comments are so temporal, I wouldn't even worry about them being upvoted at all. They don't bestow anything other than someone else's passive agreement. The answer is all that matters.

1
  • 1
    I wouldn't go that far. Only upvote if it answers the question and is useful as-is. Commented Nov 9, 2014 at 0:13
2

While vector is a really useful container, and there are certainly lots of cases where OP should use it, it's not the answer to every question ever,

You're right, it isn't, but the C++ community doesn't promote the use of std::vector because it's their favorite container ever (it's Stephan T. Lavavej's though) but because it is the superior alternative to whatever crappy code the OP has written. Yes, the code in the specific question was a typo (and it should be closed as such), but he has learned nothing from the exercise. If there's anything the OP is to learn, it's that he should stop what he's doing and adopt more useful practices. To quote an unattributed Chinese proverb:

Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime.

For a blog post on the matter, see Alex Papadimoulis's article entitled "Pounding A Nail: Old Shoe or Glass Bottle?".

Note that the user's comment you're referring to doesn't just blindly say use std::vector, he offers a reasonable justification:

Use a vector of vectors or a specialized matrix. Your class doesn't behave properly. Those would make it behave properly and let you get rid of your destructor to boot.

Therefore it isn't just noise, it actually contains useful information.

A user I really like is Basile Starynkevitch because he seems to have mastered the ability of turning boilerplate comments (i.e., "use a debugger") into helpful information. See his comments page and his highest voted answer. All around, it gives good advice. Enable warnings, become familiar with widely-used build systems, learn how to use a debugger, and so on. We could say "that doesn't answer the question, it's just noise" but then the OP will just continue to write crappy code and learn nothing.

1
  • The correct quote is "Give a man a fish and you feed him for a day. Teach a man to fish and he will buy an ugly hat."
    – Jack Deeth
    Commented Nov 30, 2016 at 14:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .