46

I regularly find myself "modernizing" C++ questions and answers by adding notes or changes based on more recent versions. For example, an edit might add lines 02 and 03:

/* 01 */ std::find(range.begin(), range.end(), 123);
/* 02 */ // or, since C++20
/* 03 */ std::ranges::find(range, 123);

Or another edit might replace the following 01 with 02:

/* 01 */ const int size = 100;
/* 02 */ constexpr int size = 100;

This is similar to how JavaScript developers regularly purge old uses of var and replace them with let on this website (which is undisputed and accepted, to my knowledge).

Note: If you're unfamiliar with C++, all you need to know is that the above bits of code are equivalent.

The dilemma - speculating about intent

However, the changes are less benign than just replacing var with let here. I feel like the author likely would have recommended std::ranges::find if they had the option at the time, so it does match their intent. I feel like they would have used constexpr instead of const if they had the option.

However, it's possible that they are an opponent of the ranges library and they wouldn't recommend it even nowadays. It's also possible that they would still write const as a stylistic choice.

The problem with comments

I know that it's possible to leave a comment to recommend a modernization. However, for questions that are multiple years old, especially if there is a long, already existing comment section, I don't see this as practical.

Barely anyone is going to see the comment, and after multiple years, the odds are often slim that the author would update their question. I think the benefit to readers of modernization of questions hugely outweighs the creative control of the author. If they really mind the edit, they can always roll it back, right?

The problem with new answers

If there are only one or two answers on the question, this seems like a good alternative. However, these legacy questions often have 5, 10, or even more answers already. Stack Overflow is rigged against new answers1) because they appear at the bottom of the page, and have to compete with a status quo with dozens, hundreds, or thousands of upvotes.

Even when you provide a better, modernized, beautiful answer, the overwhelming majority of readers won't get to see it. Readers would benefit more from modernizing changes to existing answers than a 15th extra answer at the bottom of the page.


1) Funny anecdote: on the post var functionName = function() {} vs function functionName() {}, all four ways of sorting answers display the accepted top answer first, because it is simultaneously

  • the most upvoted answer,
  • the trending answer,
  • the most recently updated answer, and
  • the oldest answer

Example

This suggested edit modernizes an existing question and was approved through peer review.

Many people have had eyes on it, and the edit was further refined instead of rolled back by other editors.

Commenter @Nimantha has pointed out that this type of edit has been discussed in Should an edit changing "var" to "const" in a JavaScript answer be approved?. To me it looks like community consensus is heavily in favor of not rejecting such edits.

What answer am I looking for?

I am looking for input from the community. However, this should not merely be an opinion, but based on something like

  • past Meta posts and statements of moderators that explain the topic
  • overwhelming consensus that can be seen in existing Meta posts
  • statements in the help center, or other places on this website that explain its purpose
  • what kinds of edits in the review queue have been historically rejected/approved
30
  • 9
    if a user is realy interested into getting the latest answers, they can easily use the sort option "Date modified (newest first)"
    – Vickel
    Commented Sep 1, 2023 at 21:32
  • 2
    @PeterMortensen yeah, take this revision for example: stackoverflow.com/revisions/208105/28. You can take pretty much any old JS post with lots of upvotes, and there is a high chance that var was replaced with let at some point. I've personally seen a number of such edits in the edit queue. Commented Sep 1, 2023 at 22:15
  • 26
    Re "Stack Overflow is rigged against new answers because they appear at the bottom of the page": It is even worse than that. Late answers are looked at with suspicion because they are often used by reputation harvesters (the answers are often created by regular plagiarism (or now ChatGPT plagiarism) and/or are code dumps. Genuine high-quality late answers are often mistakenly put in the same bucket. The result is often immediate downvotes (which makes it nearly impossible to recover). A lot of good late answers are never posted for that reason. Commented Sep 1, 2023 at 22:24
  • 2
    @JanSchultke good find, but most likely quite rare... lol, maybe we need a sort feature improvement (latest answers 1st => Date created(newest first) as a reverse of Date created(oldest first) )
    – Vickel
    Commented Sep 1, 2023 at 22:34
  • 6
    cont' - Another possible reason is a lot of users think Stack Overflow is a forum (Stack Overflow is not a forum), necroposting (it doesn't to a Q&A site) is bad and must be punished by downvotes. Commented Sep 2, 2023 at 14:21
  • 3
    I think I'll just keep doing what I've been doing, and make modernizing edits. I'm surprised how many people think that adding a new answer solves the issue. The help center post explicitly states that updating posts as they age is a good reason for an edit. The system is way too rigged against new answers, and while it would allow me to farm some reputation if I left my own answer, it would be worse for the site if I left the status quo unedited. Commented Sep 2, 2023 at 14:43
  • 3
    One possible problem with the first example (adding lines) is that the code example, if copy-pasted verbatim, may not be runnable anymore, or run slower. Of course the intent of the edit is to have future visitors pick one alternative and remove the other when they are actually using the approach in their own code, but this may very well lead to confusion and adaptation mistakes. It also makes it harder/more time-consuming to verify that a minimal reproducible example from an answer is actually working.
    – Marijn
    Commented Sep 2, 2023 at 15:18
  • 4
    @Marijn each of those problems is solvable, and the solution depends on the context. If the answerer is just offering a tiny code snippet which wouldn't work when copy/pasted anyway, then it's safe to insert additional code. Otherwise, it can be added purely as a comment to an existing code block. Obviously the editor has to check whether they break an answer by editing it; I take this for granted. Modernization usually don't affect any logic; they just simplify code and shorten it a bit by offering nicer syntax. Large-scale changes should have their own answer. Commented Sep 2, 2023 at 15:45
  • 11
    There was an incentive at some point but no new development in that area, to allow marking answers as outdated and even better adding versioning to answers that would make finding appropriate answers for particular version easier: Introducing Outdated Answers project
    – Dalija Prasnikar Mod
    Commented Sep 3, 2023 at 9:42
  • 3
    @JanSchultke: Since we don't have a review queue for answer updates, we have to rely solely on the editor to not break things. I don't think it's always obvious if a change breaks the code. Introducing smart pointer instead of regular pointers, using std::vector instead of array or introducing move semantics has the potential to break code in non obvious ways. Without review or at least separate voting on old/new approach, I find it problematic.
    – BDL
    Commented Sep 3, 2023 at 10:10
  • 3
    @JanSchultke Will be interesting to define in a policy what is considered minor enough to be allowed as edit. At the moment, my opinion is that all unreviewed edits are problematic when they change more than formatting, spelling or grammar. Maybe fixing obvious typos in code.
    – BDL
    Commented Sep 3, 2023 at 10:34
  • 3
    @BDL the help center post says that "add updates as the post ages" is a valid reason for editing posts, and other more significant changes than grammar or spelling. No restrictions are put on unreviewed edits. And in the end, even if an edit breaks something, it can be rolled back, which is the second level of peer review. Nothing is truly unreviewed on this website. Commented Sep 3, 2023 at 10:38
  • 3
    @JanSchultke It is you who brought this to the Meta for discussion. We are discussing it. I am aware that you have the knowledge to do the edits, without making mistakes but I still disagree with that you should do such edits. I also have a gold badge in different tech and I do have the knowledge to make similar edits to the posts there, but I wouldn't do that. Yes, there is a slight difference in popularity, so adding new answers in C++ where there are many existing ones is not directly comparable.
    – Dalija Prasnikar Mod
    Commented Sep 3, 2023 at 15:06
  • 7
    Is this a related Meta post: Should an edit changing "var" to "const" in a JavaScript answer be approved?
    – Nimantha
    Commented Sep 3, 2023 at 15:27
  • 4
    It's easy to cross the line with those changes. Yours look fine to me, but I wouldn't e.g. rewrite for loops using range views. Commented Sep 4, 2023 at 6:05

6 Answers 6

26

Summary

The sort of edit you describe makes perfect sense to me, and I'm honestly a little tired of having to argue this point and related points. But here goes, one more time.

The code vs. the answer

If I am considering changing const int size = 100; to constexpr int size = 100;, then it makes a world of difference whether the question is, say, "how do I declare a value that can't be changed and give it a symbolic name?", or else, say, "how do I partition a chunk of data into subsequences of 100 bytes each?".

In the first case, by writing const int size = 100;, the answer I'm looking at was directly answering the question, and demonstrating a way to solve the problem that was presumably appropriate at the time. So the intent can only be said to be "show off how to use the const keyword to solve the problem". We should not change the code in this answer, clearly, because it serves the vital (to the site) purpose of adding the way to solve the problem in the older language version. If an answer showing the new way is somehow not already written, add it yourself.

But in the second case, the code is demonstrably just giving a name to the otherwise-magic number 100 because it will show up a few times in the following code, or in order to emphasize the role that this number plays in the code. Assuming that the subsequent technique shown is valid for the modern version of the language, the code should use the modern construct for doing so - because the apparent intent is "show this technique with proper coding style" (i.e., not just throwing the number 100 around everywhere like spaghetti). Presumably, when the answer was written, const int size = 100; was the appropriate way to do that; now constexpr int size = 100; is (with whatever advantages that entails in exchange for learning a new keyword and typing a little more). So not only does it preserve intent to make this edit, not making it would allow that intent to decay.

The thing about this is: there is really only one possible question that matches the first case (unless the underlying technique has been invalidated), and countless questions that match the second case. So we should expect that edits like OP describes are ordinarily appropriate.

Note that if a question shows a technique that has been obsoleted, the above does not apply. If it still solves the problem then it's still an answer, and a new recommended technique is clearly a totally different answer. If the technique no longer solves the problem, then the answer should be edited only to note the versions for which it's valid.

But even that's not so important in my view

Of course, if existing answers are bad, it would be better to take credit yourself for a good answer than try to update the old ones in place. But there's a reason we don't use a ND version of the CC licenses. What exactly is the purpose of caring about "the author's intent" in the first place, again? Something to do with the reputation system? Ugh, fine, I suppose living with those utterly broken incentives is less damaging than actively smashing that system apart.

When I have written answers in the past, my "intent" was to provide a high-quality answer to the question. Sure, I might have a specific idea about how to solve the problem; but I would always want to convey that idea in the best, clearest, most useful way possible - using up-to-date language constructs as appropriate. Because that makes me look better as an answerer. If I didn't have that intent, what would have been the point of answering? Hopefully not just to make conversation.

Legacy support

As shown above, sometimes showcasing an answer involves ancillary details. Sometimes an up-to-date version of the ancillary details wouldn't work in an older version of the language.

My argument here is that the ancillary detail is ancillary; a developer who comes across the Q&A and needs to adapt the solution to an older version of the language - where constexpr doesn't work but const does - is responsible for identifying and researching that fact. Why? Because the point of the answer was to answer the question actually asked, which was not how to declare the value, but how to do the partitioning.

In other words, making the code from the updated answer work on a legacy platform is a separate question - and ideally one that has its own canonical already.

Making the pearls shine

If Stack Overflow is truly to be a library of high-quality answers, it needs to - at least in the long run:

  • Put the answers that are best written and demonstrate the best, up-to-date techniques that will be most appropriate for the largest fraction of developers, at the top;

  • Be comprehensive enough to include answers for legacy systems for those who need them, to cover as many people as possible;

  • Be agile enough to keep answers default-sorted in the way that's most helpful to the broadest audience possible;

  • Clear out junk and redundancy.

While demonstrating up-to-date techniques, we should also expect the code to be high quality - in both questions and answers. While many possible changes to code are pure bikeshedding, many are not; features get added to languages for a reason, and experts can normally come to a consensus as to when and how they can improve the code.

Especially when the language is no longer backwards compatible - for example, a Python question about an issue that still affects contemporary versions of Python, but includes code using the 2.x print statement in ways that can't be fixed by adding parentheses. Although I continue to assert - just like last time - that it's absurd to reject the edits to add parentheses, and have myself done it many times without apparent complaint.

People who need to work on legacy systems should be well accustomed to the need to work around the absence of new features anyway - just as the people who successfully migrated away from legacy systems had to get used to adaptations.

In the previous discussion, there was an objection that the review queue is always full, and that reviewers aren't necessarily qualified to judge whether the change to the code is valid. This is a technical problem, not a social problem. We should instead make it easier for curators to track down proposed edits that fit their area of expertise, rather than forcing everything into a "queue"; and we should make it easy to sort edits into those requiring such expertise to evaluate, vs. those that are simple copy-editing of prose.

The coarseness of the sand

Posting alternate answers essentially just to update some incidental detail like this is not an acceptable approach to the problem.

First off, I would flag a new answer like that with the custom reason that it is a duplicate - because it is. Asking people to write answers like this is deliberately encouraging what is acknowledged to be a serious problem. If the only thing "wrong with" the answer is that it says const where it should say constexpr (with good reasoning that the editor can provide), then of course our would-be editor is going to post an answer that is almost entirely redundant.

The alternative is to cut out the explanation and write something like "piggy-backing on user12345678's answer, the code should use constexpr rather than const, like so:". This is a clear abuse of the format, as Stack Overflow is not a discussion forum. Such an attempt at an answer isn't actually engaging with the original question at all, but only with another answer.

The second and deeper problem with trying to make new answers for modernization purposes is inertia. It's hard enough (practically impossible in many cases) to get new, superior answers to the top even when they're entirely new and show off a new technique. The "trending" sort isn't the default to my understanding, and the option to select it isn't very prominent. Even for those who choose it, it may not correct nearly strongly enough (and we cannot customize it). Sorting by new, on the other hand, is clearly a tool intended for curation, not for finding the highest quality answers.

More to the point, people don't vote nearly enough on old Q&A, and especially are reluctant to downvote. We can see this clearly in the moderation tools when we compare what the "anonymous feedback" looks like compared to votes. It's psychologically quite hard to downvote an older, popular answer because

  • It involves going against an apparent established consensus, which is contrary to human nature;
  • There's a perception that it won't make a difference, which is discouraging;
  • It's not even as though that perception is wrong;
  • There's a strong bias against downvotes in general, which largely is because
  • Downvotes are perceived as punitive, no matter what you tell people about them; and there may be a perception that the author didn't do anything wrong, reality just changed in the mean time.

Meanwhile, the popular answer that people don't want to downvote will sit at the top even in Trending sort, such that it takes more effort to even consider a potentially superior answer. (People who want to curate the site really need to get in the habit of sorting by newest.)

Seriously. There have been many times I personally found myself wondering aloud at how such and such an inferior answer could have gotten such a positive response, downvoted it as is appropriate - and then clicked to expand the total to find that I was the first downvoter on something with a triple-digit score. In years. Sometimes when the answer had a blatant problem that couldn't be fixed without violating even my standard of author intent.

Finally: the Trending sort has no concept of "as of YYYY-MM-DD, version X.Y is officially no longer supported, even for security fixes, and feature F is not possible in any newer version". It assumes that time can be treated as a continuous, constant-rate quantity, which is just not how the software world works.

As a consequence of the above issues, there's just way too much inertia in the system. It's not reasonable to expect to fix this mess with the "just post a new, better answer" approach.

1
  • 7
    Wish I could upvote this more than once.
    – Ian Kemp
    Commented Sep 6, 2023 at 10:14
48

I'd suggest writing a new answer, instead.

Sure, it may have to compete with answers that have dozens, or even hundreds of votes, but over time, a good new answer will also get voted on.

Developers are sometimes stuck with older versions of languages / frameworks / environments, so the older code is absolutely valid as an answer.

I can agree that adding one alternative/new version is helpful, but what if there are eventually more versions? Then the answer is going to get messy.
On top of that, you can't up/downvote part of an answer. So if the old code is found to be dangerous, and the new code fixes that problem, how would you vote on the answer?

27
  • 4
    "but over time, a good new answer will also get voted on." it would only benefit me because I would farm reputation. Realistically, it would take years to overtake an answer that already has a hundred upvotes, if that's even possible (I honestly doubt it). In those years, the readers who view the top answers (which is probably the vast majority) experience a worse quality of content. Commented Sep 1, 2023 at 21:38
  • 28
    There are still developers stuck with legacy C++11 code (on legacy hardware). Don't edit that out. It's only a few changes now, but what happens in 2 years? In 4 years? 2 more changes? And then again 2 more? It's the viewer's responsibility to take the answer they need. It's not your responsibility to rewrite existing (old) answers.
    – Cerbrus
    Commented Sep 1, 2023 at 21:42
  • 15
    Look, we can argue "minimum standard" all we want, but in the meantime, there's still old hardware chugging away in a basement somewhere, keeping companies afloat... Until it breaks. Then some poor intern is going to need those old answers to figure out what went wrong, or what the thing actually did.
    – Cerbrus
    Commented Sep 1, 2023 at 21:46
  • 6
    I'm merely suggesting an alternative non-destructive approach that everyone can benefit from. Suggestions like this have been discussed a lot, before, and people often forget about those that are stuck maintaining legacy hard/software.
    – Cerbrus
    Commented Sep 1, 2023 at 21:53
  • 7
    Those are very specific examples. My suggestion doesn't change. Add a new answer, and let the votes and viewers decide.
    – Cerbrus
    Commented Sep 1, 2023 at 21:57
  • 7
    How does my suggestion prevent readers from accessing the "clean, modern solution that guides them"?
    – Cerbrus
    Commented Sep 1, 2023 at 22:01
  • 4
    If the top answer uses some outdated construct that has been superseded by a standard library utility for a decade, then this is what the website is teaching the programming community. Sure, you can add your own answer, and maybe, given a few years, could even overtake an existing top answer (though I still doubt that). However, until you do overtake it, the website teaches people outdated practices. Commented Sep 1, 2023 at 22:04
  • 24
    I'm still maintaining C++98 projects and C89 projects I'd love to update, but in some cases the hardware's also solidly obsolete, so no one produces even as ancient as a C++11-supporting compiler for it. Such is life. Commented Sep 1, 2023 at 22:29
  • 7
    @JanSchultke changing old answers by removing working solutions for older versions is absolutely unacceptable. I would never approve such edit or would do a rollback. You can always add new answer if there is significant change for new versions. Nobody will downvote good new answer that contains information that does not exist in previous answers. at best you can do is editing answer to leave both versions clearly marked. I would also rather fully repeat some code (unless it is really long one) with appropriate adjustments, than changing just line or two in the middle.
    – Dalija Prasnikar Mod
    Commented Sep 3, 2023 at 9:36
  • 4
    @DalijaPrasnikar it's not removing a working solution. It's providing minor modernizing changes so that the solution is equivalent, but as if it was written in a more recent language standard. A breaking change would obviously not be acceptable, but that's not what the question is about. Of course, the upgrade to a more recent version does mean that a higher standard is required to run it, but this is a non-issue when we upgrade to say, Java 8 or C++11, which 97% of site users use anyway, and when porting to older versions is obvious & easy. (i.e. back-port by replacing let with var again) Commented Sep 3, 2023 at 9:46
  • 5
    @JanSchultke Because there are still people that use old versions. I am not against modernizing the practice, but your approach to doing so is not appropriate one. Yes, I understand your desire to bring more eyes to the new practice, and I gave you solution for that. Edit the answer by adding the complete new variant of the code separately, leaving the old one intact. And be prepared that someone will rollback that and then add completely new answer.
    – Dalija Prasnikar Mod
    Commented Sep 3, 2023 at 9:58
  • 4
    @JanSchultke really trivial changes like let to var are even more meaningless. Those who know the difference will know it, those that don't will not learn anything from your change alone.
    – Dalija Prasnikar Mod
    Commented Sep 3, 2023 at 10:08
  • 9
    @JanSchultke We are going in circles here. Which part of adding full separate code and explanation why it is separate you don't understand (even if you edit existing answer)? If you want to create teachable moment then having two visibly separate and marked code variants will teach people more than hidden change in code.
    – Dalija Prasnikar Mod
    Commented Sep 3, 2023 at 10:23
  • 3
    Yeah, "a good new answer" got a lone upvote in a year. Surely it works. Commented Sep 3, 2023 at 11:01
  • 6
    There will always be examples that illustrate whatever point you want to make.
    – Cerbrus
    Commented Sep 3, 2023 at 11:55
27

I think the main problem is that Stack Overflow provides answers to two different kinds of questions. And a competitive approach is only good for answering one-off questions.

But if, by some whim of Google's algorithm, it becomes a reference question, where Google sends people with a similar problem, the entire question's nature is drastically changed.

It's not anymore a private matter between the OP and those who answered to their initial question. The entire page becomes a community asset. And it could and should be reshaped to reflect the change. Question title and tags must be edited to match the answer provided. Question body must be edited as well, with all insignificant details and bad practices unrelated to the question removed.

And answers must become a target of constant maintenance as well. Naturally, for reference questions, there is just no such thing as "authors intent" anymore. All selfish motives should be put aside.

The noble purpose of this site of Stack Overflow is pretty clear:

to build a library of detailed, high-quality answers to every question about programming

Nowhere does it say "a place to show off" or "a museum of ancient practices". It's only quality that matters. And usefulness for people coming from search.

So a popular answer rather must be improved, no matter what was "initial intent". The opening poster is long gone. And whatever initial intent was, it's not applicable anymore. It's a reference question now, for people coming from Google. And it should answer to the current standards. Otherwise Stack Overflow will become rather a dispenser of outdated practices than a library of high-quality answers.

What and how should be improved is another matter. But I would say, providing the current best practice should be preferred. After all, those supporting legacy code will simply get an error, which they'd google for, and get their legacy solution all right.

25
  • 4
    You put it really well. Stack Overflow's goal was never to be a historical archive of old practices, it's a Q&A site for when you have a problem nowadays. Seen through that lens, the answer is obvious. I also find it a noble perspective to think that highly upvoted Q&As become "community property" in a sense, even if they aren't community wikis. The site would be a better place if people could put their egos aside and think of the website like that. Commented Sep 3, 2023 at 9:58
  • 16
    If we must edit posts to update them, then at least preserve the old version somehow. How about posting a new answer with the up-to-date version, then edit the accept answer with a disclaimer like "This answer targets version X, for newer versions see [link]"? Visibility should be given by this approach and it allows for separate voting on both approaches (I mean, who knows if the updated version is correct or a good idea?)
    – BDL
    Commented Sep 3, 2023 at 10:04
  • 5
    While answers are provided under the CC0 license, and therefore can be edited without regard for the author, in practice... it's rude to completely overhaul an answer without the original author's approval. It's more respectful to suggest a modern alternative to the author, or to write a brand new answer detailing all best (according to you) solutions based on circumstances. Commented Sep 3, 2023 at 11:24
  • 6
    @MatthieuM. Yes, it's rude. But why nobody cares for the bad answer shown to hundred visitors? For some reason Stack Overflow caters for anyone: the opening poster who is long gone; for the fastest gun in the west who scored the fist answer; but not for hundreds or thousands coming from search. That's beyond my understanding. Commented Sep 3, 2023 at 11:35
  • 20
    @JanSchultke "You put it really well. Stack Overflow's goal was never to be a historical archive of old practices, it's a Q&A site for when you have a problem nowadays." Wrong. SO is a repository of quality Q&A. It doesn't care about your current problem, its purpose is to provide answers for all viewers.
    – Cerbrus
    Commented Sep 3, 2023 at 11:57
  • 3
    I wholeheartedly agree that showing anything but the best/most relevant answer to viewers is sub-optimal. I disagree that this means you should be rude to others to "fix" the problem. Micro steps have already been taken -- the highest voted answer is finally displayed before the accepted answer -- and we "just" need to push so the platform evolves to be better. Commented Sep 3, 2023 at 11:58
  • 9
    @Cerbrus I disagree. SO should be catering to viewers of modern times, and not a hypothetical audience that is interested in obsolete historical practices. Minor modernizing edits don't take much/anything away from "all" viewers, especially if they provide an alternative modern solution instead of replacing an old one. The push for flagging answers as obsolete (see meta.stackoverflow.com/questions/405302/…) proves that modern, good practices are more important. Commented Sep 3, 2023 at 12:06
  • 7
    @JanSchultke: It does answer, indirectly. Recommending to write a new answer is just another way of saying "Yes, it does conflict with author intent". Also... more popular on meta does matter => community norms (especially informal, subjective) are queried by polling in which direction the community leans toward. Commented Sep 3, 2023 at 12:07
  • 11
    @Cerbrus you didn't explain anything about policy. You didn't cite the help center or any other authoritative resource. You didn't link to any other relevant meta question where this got addressed by moderators. You did nothing more than provide your opinon. Commented Sep 3, 2023 at 12:11
  • 5
    And on top of that, the more often you call SO's audience "hypothetical", the more you insist you're right and everyone else is wrong, the more reason we have to close this, because you're clearly not here for our opinion.
    – Cerbrus
    Commented Sep 3, 2023 at 12:12
  • 4
    @MisterMiyagi you're not sacrificing those people by providing an additional Python3 alternative on a question that provides a Python2 code sample. You're also not sacrificing those expert contributors by updating var to let. They already know what both mean. The point is that new users who don't know these nuances yet should get the good, modern, recommended answer first and foremost, not a historical curiosity. Commented Sep 3, 2023 at 19:25
  • 4
    That var vs let example is a very specific, and in this case, insignificant change. It's not a good example to use for all modernization edits. It's rather disingenuous to suggest that all of those edits are as straightforward as var vs let. By your logic, we should just be replacing python 2 code with python 3 code. It's not that simple.
    – Cerbrus
    Commented Sep 3, 2023 at 20:43
  • 10
    @Cerbrus I believe you are intentionally and maliciously ignoring all the nuance of my argument. I have said in one sentence that an alternative to a Python2 solution should be provided. In another sentence, I have said that var should be replaced with let. These are obviously two different solutions, and the choice is nuanced and depends on the situation. I wonder why you keep misrepresenting and ignoring me. Do you think that you're going to convince me with it, or are you just venting your anger? Commented Sep 3, 2023 at 21:14
  • 5
    @MisterMiyagi I am afraid, this question is sort of academic to you. You are arguing just for sake of it. This topic you are constantly pushing on is just a tiny part of the problem. There are many answers with outright bad code/phrasing. There are many edits that don't touch backward compatibility at all. There are many unrelated edits when a question is about one topic but a code example feature a bad practice from another area. In all these cases the code/phrasing must be fixed without further ado. Only the code related to the question must be scrutinized your way Commented Sep 4, 2023 at 6:05
  • 4
    @MisterMiyagi but again: the question is not "whether to introduce backward incompatible changes" but "whether the author is untouchable person and their contribution is forever sacred". For example, a question that became popular, is about splitting a string but the code example contains an outright SQL injection. And the answer just replicates it, while providing a correct method for splitting a string. In this case both question and answer must be edited, with SQL injection part either completely wed out or fixed. Not matter what "author's intent" was Commented Sep 4, 2023 at 6:06
8

I'm not against modernization of code snippets, but I don't think either of the examples represents modernization. For me, modernization would include:

  • Replacing officially deprecated bits with current equivalents (e.g., noexcept instead of throw().)

  • Replacing error-prone patterns (especially verbose ones) with best practices where the standard has evolved to provide better alternatives. Examples:

    • auto operator<=>(T const &rhs) = default; instead of hand-written implementations of the binary comparison operators that attempt to impose the default ordering.

    • Concepts and requires clauses instead of std::enable_if.

    • (I don't know Javascript, but I believe the let-for-var change would apply here.)

  • Adding annotations that improve safety and/or clarify intent without changing behavior, like [[fallthrough]], [[noreturn]].

Modernization would not include:

  • Arbitrary shifts in style, e.g., “east const” versus “const west”.

  • Style changes enabled by newer standards but that aren't really relevant to the matter at hand. Examples:

    • using MyType = Foo; instead of typedef Foo MyType;.

    • Changing a declaration that has an initializer to “almost always auto” with (nearly-) universal initialization.

  • Pulling in new paradigms or library classes that the code isn't already using. For example, don't rewrite somebody's snipped to use the chrono library if they're not already using it.

(I agree with some of these styles, but they are not modernization and I don't feel it's appropriate to change other people's code snippets because they didn't use my style.)

Definitions aside, in both of the examples given, I'd say it's better to leave the code alone, but my reasoning is different for each example.

  1. The ranges library introduces a new paradigm (algorithm composition) into a language that already supports many different paradigms (procedural, object-oriented, functional, generics/metaprogramming, data-oriented, etc.). Programmers (and style guides) pick and choose which paradigms to use.

    Rewriting the std::find example in terms of ranges introduces a library that opens the door to another paradigm that may or may not be relevant and may or may not be allowed by the reader's local style guide. Unless that paradigm is required for the solution, there's no value here.

    This doesn't “modernize” the code, it just shifts from one style to another. The STL-style algorithms are as much a part of C++20 and C++23 as ranges are. They're not deprecated. There's no "best practice" that says they should be excised from everyone's code.

    Offering the ranges style as an alternative adds clutter without adding value. Programmers who prefer to use the ranges style probably don't need guidance to make the simple transform from the STL-style algorithm to the ranges version.

  2. I see replacement of const with constexpr as a different kind of proposal because it's based on a much older language feature rather than a relatively new library feature. I'd approach this with more open-mindedness.

    In the particular example (initializing a variable with a literal) there's no effective difference. In other circumstances, there will be. If the code snippet has a literal there to generate a minimal example that demonstrates the problem or solution, then it's best to leave it alone. In real code, that value might not be a literal and thus, while it's good practice to assign it to a constant variable, it would be an error to use constexpr.

    If the code requires a constant expression, then—by all means—fix it. But that's a correction, not a modernization.

9
  • 1
    Why do you not think that the examples represent modernization? Making changes like replacing var with let is a form of modernization, and linters recommend it. C++ is a bit of a different beast because the language is very complex, and a linter can't really tell whether a use of const instead of constexpr is intentional, or a modernization opportunity. However, I believe it falls under the umbrella of modernization. I get your point that it's C++11, although I disagree that this is too recent of a standard (it's more than a decade old). Isn't upgrading to C++11 modernization though? Commented Sep 4, 2023 at 18:07
  • As for the ranges example, there's certainly the dilemma of clutter. On one side, it would be too drastic to replace std::find examples with std::ranges::find, and chances are, visitors of the website don't use C++20 yet, so they would benefit from the old version. On the other side, the readers who are using C++20 would be misled into thinking that there isn't a more concise way. I believe the utility of new readers being given the most recent idiomatic code outweighs the downside of "clutter". Ultimately it's subjective whether it is clutter. Commented Sep 4, 2023 at 18:13
  • 1
    @JanSchultke: Most of my answer is about why I don't think those examples represent modernization. I'm not sure what I could add in a comment to clarify further. Features should be used with intent, not just because they're new. Bringing them into the conversation when they're tangential to the Q&A can be confusing, distracting, and sometimes may not align with the intent of the original code snippet. Commented Sep 4, 2023 at 18:55
  • I don't think the semantics discussion about what is and isn't modernization would go anywhere, as it's ultimately opinion-based. The original question was about whether these edits are overall beneficial, and whether conflict with the intent of the author. I seem to have misread some of what you've said about const; that was my mistake. I don't think we'll find any agreement on std::ranges because it just boils down to "this alternative is helpful" vs "this alternative is clutter" which is also purely opinion-based. Commented Sep 4, 2023 at 19:11
  • 5
    @JanSchultke: let, as I understand it, fills a gap in the language that prevented people from applying of the best practice of limiting scope. You can almost always figure out when let is appropriate without looking at much context. constexpr enables up a bunch of new functionality. When you write constexpr you're communicating an intent distinct from const. In many cases, you cannot easily tell when constexpr is allowed without a lot of context. There's no consensus on whether it should be the "default" choice over const. Commented Sep 4, 2023 at 19:11
  • What I mean by modernizing const -> constexpr is cases like const int size = 10; int arr[size]; where through a peculiar special case in the language, this is actually allowed (and doesn't produce a VLA). To my knowledge, it's almost unanimous consensus that this should be written with constexpr, barring C compatibility. More complex cases where constexpr isn't obviously better wouldn't be a modernizing change, and might even be breaking. I totally agree. Commented Sep 4, 2023 at 19:13
  • 5
    @Jan Schultke: When people are asked what they think about "modernizing" code snippets, they're largely be in favor. If asked about making style changes, they're going to be more cautious. That's why I'm worried about the meaning of modernization. Commented Sep 4, 2023 at 19:25
  • 2
    @JanSchultke: Who would use a C-style array in modern C++ code? Surely you meant, constexpr auto size = std::size_t{10}; std::array<int, size> arr;, right? (Assuming, of course, that size is not a member variable.) ;-) Commented Sep 4, 2023 at 19:32
  • 1
    The problem with std::array is that it's quite opinion-based when to use it (always, single-dimensional only, global variables only, etc.), and there are some people who oppose it. It also adds an extra include and isn't available in freestanding C++. I like std::array, but the change is too substantial to simply replace old code, and I don't feel comfortable making it. I would rather replace const with constexpr which virtually no one would oppose (unless it broke C++98 compatibility, and this is needed in the answer). Commented Sep 4, 2023 at 19:39
8
  • If the edit is substantial and related to the reason why the question was asked, you should stay away from it since you might change the intent or meaning of the code. In particular you risk changing it radically so that the questions + posted answers no longer make sense.

  • If the edit is not substantial but about some completely unrelated thing in the code, then you probably shouldn't be making it since it is likely too minor an edit.

  • It is generally not advised to mess around with other people's posted code but instead poke them through comments and have them change the code instead.


Other things of note:

  • It is fine to ask questions involving legacy/obsolete code, libraries and tools. Not everything needs to be about the latest and greatest version. Those of you whom might be real-world programmers will surely recognize the need of maintaining old code bases. When doing so, there's an overwhelming consensus that changing as little as possible is a good thing.

    So when faced with some bug trouble-shooting in some C++03 code base, you should fix that bug, test the fix and then roll it out. You should not go crazy starting to port the whole code base to C++23. That will rain bugs all over the place for sure. And most of the time your clients/boss are only interested in a quick, non-disruptive fix, and not as interested in paying you for change things for the sake of changing things.

    A person asking a question on SO might be such a real-world programmer maintaining some old code base. Therefore changing their code isn't helpful but potentially harmful. Ideally they should tag their question with some legacy tag like but maybe they just forgot? Prompt them in comments. ("Is there a reason why you are still using auto_ptr?" etc)

  • Questions have a date. When reading something on the Internet, don't expect some post from 2008 to be up to date. You have to read it as-is and take the date in account. This has nothing to do with programming or SO as such, but with source evaluation and common sense.

    If there are better things to do the same nowadays, then perhaps a new answer or a new Q&A would be more suitable than an edit? Particularly since we do have an SO aspect that very old posts were written against different site standards than the present.

  • The change might not be as so good as you think. Whenever changing something there's always the chance of adding new bugs. Additionally, new language features could hide compatibility issues and introduce poorly-defined behavior. Tool support might be lacking. New features can be controversial without you knowing about it. And so on.

    In case of C++ specifically, then every single new language feature you can come up with can very likely check off some of the above listed problems.

  • You might not be as brilliant as you think. Pretty much all programmers tend to have big egos and quite a bit of hubris. In programmer surveys it is common that most programmers rate their personal skills higher than what their actual experience attests. Like when every single person in the dev dpt rates their own skills as above the average of the group, which evidently can't be true.

    And because of this, quite often edit proposals are just poorly considered. Quite possibly the person who posted the original post actually knows more of the topic than you do. Weird edit proposals also become more frequent the more attention the post is getting, just as popular questions tend to attract more poor, late answers by people who don't have nearly as much to contribute with as they think they have.

22
  • 1
    I'm confused when you think an edit is appropriate at all then? You provide reasoning for why substantial edits are a bad idea, and why minor edits are also a bad idea. This leaves the question ... are edits ever appropriate then? Simply poking the author is unlikely to do much when you're just adding the 20th comment to an old post and the author either no longer exists (deleted profile), or has been inactive for a long time. Commented Sep 7, 2023 at 12:13
  • Take this high quality answer: stackoverflow.com/a/47466406/5740428. The author has been notified in 2020 by a comment that their answer is obsolete after the proposal was radically changed. When you look at what made it into C++20, the answer is completely obsolete and only useful from a historical perspective when talking about a temporary state that the spaceship operator proposal was in. 99.99% of users don't visit the website for this kind of extremely niche historical information. It's Stack Overflow after all, not Stack Historians :) Commented Sep 7, 2023 at 12:15
  • I wouldn't edit that particular answer because the edit would be way too large and obviously conflict with the intent of the author, but it comes to show that leaving comments isn't guaranteed to do anything. I've seen countless comments that say "this is obsolete now", or even comments that point out significant issues such as misinformation with the answer, but the author never took action. Editing is the only reliable way to improve the site's quality. Commented Sep 7, 2023 at 12:17
  • 2
    @JanSchultke Indeed most edits of code are not appropriate. Among those edits that are we find fixing formatting, indention, obvious typos etc.
    – Lundin
    Commented Sep 7, 2023 at 12:39
  • 2
    Jan, In the example you linked, the author of the answer is the one applying the edits, so that's a different scenario altogether.
    – Cerbrus
    Commented Sep 7, 2023 at 12:42
  • 2
    @JanSchultke Regarding your example, it isn't very good because the question is actually in fact asking why cppreference.com listed not yet voted-in, non-standard feature as part of the language before it was. So basically they ask why some C++ site was showing strange information back in 2017 and that's the historical context. But we know that questions about exotic features and operators always get ridiculously up-voted on SO by people who are curious, not necessarily because the Q&A was of particularly good quality or useful to preserve. ->
    – Lundin
    Commented Sep 7, 2023 at 12:42
  • 2
    Take this crap as the perfect example of something that should never have been up-voted this much: stackoverflow.com/questions/1642028/what-is-the-operator-in-c-c
    – Lundin
    Commented Sep 7, 2023 at 12:43
  • 3
    @JanSchultke Regarding updating posts, I can relate to the OP who didn't take action. The other day someone poked me in comments out of the blue and asked me what I meant in some 12 year old post and if the code shouldn't rather be "x". Most times I really don't have the time and energy to dig into some old stuff I wrote ages ago, to remember what I was thinking back then, if it is correct by the standards then & now and then either rewrite it or defend it. I'd personally be fine with such old posts just getting deleted, but SO frowns at this.
    – Lundin
    Commented Sep 7, 2023 at 12:47
  • 2
    Maybe the reason the old post is interesting, is because it contains old code... Where else does that intern figure out how to fix the legacy software? Not every edit gets scrutinized. Especially on lower traffic older questions, you can't rely on these edits being peer-reviewed.
    – Cerbrus
    Commented Sep 7, 2023 at 12:51
  • 2
    @JanSchultke Regarding the <=> none of that post looks particularly good and it has questionable history. Someone added the C++20 tag to it back in 2017 which should already set off all manner of alarms regarding technical accuracy. Then someone else thought it was a good idea to add the c++-faq tag to it, again long before this feature had even settled. Ironically these tag edits seem to be perfect examples of updating a post to recent technology far too soon, before that technology was even available. Bad edits, questionable history and apparently not yet up to date still...
    – Lundin
    Commented Sep 7, 2023 at 12:53
  • 1
    @JanSchultke "You've also mentioned that editors can be wrong and may think of themselves as more knowledgeable than they are. Well, the same applies to the author." And that's why we have voting. Pedantically, the author have the copyright of their content as-is. If it is incorrect and they won't change it, then down-vote. This isn't wikipedia - unless a post is explicitly a community wiki then we can't treat it as such. In case an answer is particularly bad or harmful we may sometimes cast delete votes though. There's no problem with deleting crap, but polishing crap might be problematic.
    – Lundin
    Commented Sep 7, 2023 at 12:57
  • 2
    @JanSchultke It is a well-known problem with SO's "crap preserving" philosophy and using up-voting as a misguided way to mark quality content, while up-votes might as well signify popular topic/technology, post age etc. For example the most up-voted Ada question ever on SO has +35 score and the most up-voted Java question has +34779 score. That isn't reasonable.
    – Lundin
    Commented Sep 7, 2023 at 13:21
  • 2
    @Lundin I totally agree with you that upvoting is a not a reliable measure of question/answer quality and is distributed unevenly. If you hold that belief though, then why do you make the argument "And that's why we have voting."? What is voting supposed to accomplish in this scenario? Commented Sep 7, 2023 at 13:26
  • 2
    The voting patterns of SO users are also exactly why providing a completely new answer is often not viable. Answers that have few or no upvotes are something "nobody likes" in people's minds, so it feels intuitively wrong to upvote them no matter the quality. Downvoting highly upvoted answers also goes against "perceived consensus", which is why a highly upvoted answer may be eternalized at the top no matter how much worse it is than more recent, and better answers. If voting or providing new answers doesn't solve the issue, then only closing/editing remain. Commented Sep 7, 2023 at 13:26
  • 2
    @JanSchultke Down-voting is more efficient than up-voting when it comes to marking technical quality. But I agree that down-voting doesn't work well once a bad old post sits at 50+ score.
    – Lundin
    Commented Sep 7, 2023 at 13:27
2

I think this problem can be solved with markup. Stack Overflow can add a new markdown element - tabs - and the convention would be to use them for the specific part of an answer that demonstrates/discusses the solution in multiple specific languages/versions/frameworks/libraries/standards/whatever.

When one wants to modernize an existing answer, they should:

  1. Convert the relevant part of the original answer to a tabs Markdown element.
  2. Name the tab that contains the original answer "Orig" or something like that. Or maybe add more context and name it something like "Orig (old C++)"
  3. Add a new tab for the modernized version, and name it after the modernized standard - in the example's case "C++20"
  4. Mention themselves inside the tab, together with the date of the edit, to make it clear that this tab is not something the original answerer is responsible for. Maybe this metadata can be part of the tabs Markdown syntax?
  5. Make the new tab the default one (although it shouldn't be the first tab - so the tabs syntax will need to support setting the default tab)

Of course, tabs can also be used right away by the original answerer. For example, if they suggest multiple libraries that can solve the problem, and want to show a snippet of how it would be solved in each library.

6
  • 5
    Tabs are not something markdown has. Markdown isn't a thing SO invented. Now, it would be possible to expand SO's MD parser, but I have a hard time thinking of a workable syntax for tabs...
    – Cerbrus
    Commented Sep 3, 2023 at 15:56
  • 4
    This answer basically boils down to "hide part of the answer". Just use normal Markdown headings as your "Tab Titles" and let all the information be easily accessible.
    – dotancohen
    Commented Sep 3, 2023 at 16:25
  • 5
    One of the few good ideas in Stack Overflow Documentation was a markup to bracket paragraphs as applying to particular versions. It's a shame that never made it to Q&A. Commented Sep 4, 2023 at 5:34
  • 1
    On Codidact, something like this is accomplished by the fact that the HTML parser allows the details and summary tags. Commented Nov 1, 2023 at 23:47
  • @dotancohen organizing large amounts of information with judicious use of tabs or other "collapsible" HTML elements makes the information more easily accessible. Instead of having to scan through the post for the right heading, it's already on screen and you can click to expand the relevant portion. Commented Nov 1, 2023 at 23:48
  • @KarlKnechtel: Yes, I use org-mode too ))
    – dotancohen
    Commented Nov 6, 2023 at 19:15

You must log in to answer this question.

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