29

Recently I noticed a question where a long time user edit the void main() in the OP's code to int main(). I left a comment asking how did he know that is not using void main()? I got a reply back from the editor

[...]It would be really bad teaching to let it pass by in SO example code (except where it is the point of the code), so please do fix that wherever you see it.[...]

Now I get that void main() is not standard in C++, it never has been and I do not think it ever will be. I personally point that out to the OP in a comment and normally I link to What should main() return in C and C++?. The editor did have that in their comment

You might also admonish the OP about it. When you have the time.

Which I agree with but if the code is changed to int main() then the comment looks out of place as there is no void main() in the code anymore.

So do we really want to change the OP's code in this case or should we leave it alone and politely inform the user that they should not be using void main()?

I disagree with the dupe closure. The dupe states that the edit should not change the intent of the OP. That is what I am trying to figure out in this question. Does changing void main() to int main() which has no effect on the code except to make it compile on compliant compilers changing the intent or not and if it does not then should we still do it or not.

22
  • 14
    I don't like to edit someone's code if it's not a (very) clear typo (even if my edit improves or fix the "bad" code).
    – Maroun
    Commented Mar 30, 2016 at 13:02
  • 6
    On a "suggested edit review", that would get rejected as an "attempt to reply" comment. And for good reason. Code shouldn't be changed. Commented Mar 30, 2016 at 13:17
  • 1
    Simply flag obsolete comments as "Obsolete". Commented Mar 30, 2016 at 14:30
  • 1
    @HansPassant But the comment isn't obsolete. Most likely the OP is still using void main() but the question now shows int main() and it was not edited by the OP. Commented Mar 30, 2016 at 14:32
  • 4
    What the OP is actually using in his program is completely irrelevant, the question was not about getting the wrong process exit code. The tedious comments are obsolete so flag them that way. Commented Mar 30, 2016 at 14:47
  • int main is a problem because it's not accepted by many compilers. This means that it (1) teaches readers an Evil(tm) practice, and (2) makes it less straightforward for those who answer to try out code: with void main it has to be modified to try it, unless one happens to use a compiler that accepts it. Commented Mar 30, 2016 at 16:05
  • It's just a few days since last I pointed out an error of someone, and the someone started a silly edit war, postulated an absurd interpretation of my question, and posted about it on meta. Likewise, just after correcting one of Nathan's statements, he construes an issue that doesn't exist, and posts about it on meta. I can't help but notice a pattern. Commented Mar 30, 2016 at 16:08
  • @Cheersandhth.-Alf What statement of mine did you correct? If anything your int main is a problem because it's not accepted by many compilers. is completely false. I brought this issue to meta as I do not think it is correct to change the OP's code as it changes its behavior. Commented Mar 30, 2016 at 16:11
  • If you are talking about the static free functions I admitted I was wrong and removed the comment. Commented Mar 30, 2016 at 16:12
  • 1
    "it changes its behavior" is correct: it makes the code compile on C++ compilers in general. That is not a reason to not do it. It's a reason to do it. Except for questions where that is precisely the (or one) issue. Commented Mar 30, 2016 at 16:16
  • 1
    @Cheersandhth.-Alf Since when do we do that? Does that also mean we should add #include <string> when someone does not but it compiles on their platform as one of their other includes does include <string>? Commented Mar 30, 2016 at 16:21
  • 8
    is your argument seriously that we should edit questions to constitute good examples? really? you've seen how trivial most of them are, right? downvote or comment and move on. SO should be judged by its answers, their votes, and critical thought above both. trying to bend all questions to be useful examples in and of themselves is a real waste of time, not only for the other reasons given, but because most of them are not good examples, by definition... otherwise there'd be no question to ask. Commented Mar 30, 2016 at 19:34
  • 1
    By the way, I looked at the edit in question. I find it ironic that the person who made the edit, who is apparently so bothered by the use of the non-standard void return type (supported in VC++), did not bother to include a return statement in this method that is now supposed to return an int. Yet another reason for not editing code is that it's a lot easier to make a harmful mistake when one does, as so aptly demonstrated by that editor. Commented Mar 30, 2016 at 21:07
  • 1
    @NathanOliver : It doesn't, and I am with you. I think it is clear that the OP was using an MSVC++ compiler (void main compiles) and system ("pause") suggests Windows as well. But even then, I think any reasonable developer would know how to resolve the problem with void main. It's not like the OP even provided a compilable program. Anyone would have realized it was missing a using namespace and a couple of includes. I've seen other users bring the issue with void main up, but it is generally done as a comment, or if someone answers the question they bring it up there as a side note. Commented Mar 30, 2016 at 21:13
  • 2
    @PeterDuniho Whether the return statement is required or not depends on compiler settings No it does not. The standard guarantees that not having a return statement in main is okay. That is not a setting but an aspect of being standard compliant. In any case, an int main() method without a return statement is way worse than a void main() No again. void main is not standard but not having the return is. Commented Mar 30, 2016 at 21:23

6 Answers 6

7

Focusing strictly on code edits, I think the following are unequivocally OK:

  • fixing formatting / indentation / brace usage (especially for new users that end posting triple-spaced quadruple-indented code, or for people that have bizarre brace styles)
  • fixing obvious typos that in no way affect the question (i.e. the typo is not the problem, just prevents it from being a true MCVE)

I'm not sure anybody would really argue against those. Where there seems to be some issue is regarding edits of the kind that make the question better by making it more of an MCVE. This category includes anything from:

  • adding missing #includes (for questions unrelated to undefined symbols)
  • removing dozens to hundreds of lines of code to reproduce the problem more minimally
  • making obvious fixes of ill-formed/undefined behavior code that aren't related to the actual problem OP is asking about (e.g. void main(), or OP not having allocated a pointer before dereferencing it - where said pointer dereference is not related to the issue, etc.)

These all fall under the same umbrella and I think they're OK if you're sure you maintained the invariant of OP's original problem... and you're sure because you've verified by compiling/running the code and getting the identical compile error/runtime behavior. But I think they're only actually worth doing if they improve the quality of the question.

Turning a 400 line not-quite compiling, clearly-not-minimal example into a 15 line MCVE with all the right #includes and everything? Hell yea, edit! That significantly increases the upvote-worthiness of a question and makes it more likely that (a) the OP understands OP's own question better and (b) OP gets better, more focused answers. I love these edits and wish I could upvote them.

Turning a 25 line non-compiling example with void main() into a 24 line still-non-compiling example with int main()? I think the edit's fine and non-objectionable (the problem here has absolutely nothing to do with the return type of main() - invariant maintained), but it just isn't particularly significant. I still wouldn't upvote this question. May as well spend the time to actually make the code compile, or leave a comment about the invalidity of void main(), or pass. Probably worth it to have left a comment in the edit as to why that edit was made for OP's sake.

4
  • "Turning a 25 line non-compiling example with void main() into a 24 line still-non-compiling example" -- except there's no evidence the example failed to compile before, and the edit changed the main() function into a function declared as returning a value, but without a return statement. A missing return statement may prevent successful compilation altogether (depending on compiler settings), and at the very least is very poor practice. Much worse than using void main() with a compiler that supports that syntax. Commented Mar 30, 2016 at 21:14
  • @PeterDuniho Example uses cout without including <iostream> (and without std:: or using namespace std;). So yeah, it doesn't compile.
    – Barry
    Commented Mar 30, 2016 at 21:27
  • @PeterDuniho : log10 would also require #include <cmath> Commented Mar 30, 2016 at 21:30
  • It's common on SO to omit well-understood and commonly-used boilerplate, like standard includes, namespaces, etc. That's a lot different from writing code that from the first provided line to the last would compile fine, given reasonably assumed context (i.e. the boilerplate). That said, I've been reminded that the main() function is special and has a default return value. So there's that. I still maintain it's poor practice, but then in spite of my years using C++, I have decided I much prefer C#. So there are a lot of people using C++ with whom I simply disagree about good conventions. Commented Mar 30, 2016 at 21:30
16

Never change the code provided in a question, under any circumstances whatsoever, if you are not already an expert in the topic that the question is about, simply because your edit could deface the meaning of the question if you just edit code without actually and fully understanding it.

Inline-Comments are an exception, but please do not change the actual code in questions. This only leads to confusion ("Your code is working fine!" [Post-Edit]) and anger ("Why did you fix the issue by editing instead of answering?").

Instead you should use an answer to point out typos or bad practices along with the solution. If you cannot provide a solution, point out typos or bad practices in a comment instead.

10
  • 11
    −1 "because you cannot know if it's the cause of the specified problem" is incorrect (false, not true, no match with reality, etc.) for the case at hand. Thus the argument is based on a false assumption, and is invalid. That said, sweeping generalizations are ungood of themselves and should not be posted in meta, and it is IMHO no coincidence that this one ended up as a fallacy; that's where sweeping generalizations generally go. Commented Mar 30, 2016 at 16:02
  • 10
    While I agree what this too much of a generalization @Cheersandhth.-Alf, I do agree with the general idea that code in questions should not be edited beyond simple formatting, for the simple reason that I can now no longer point out this error in an answer or comment. Even if this is not the source of the error, I typically point out these things if I notice them. Silently fixing them is not particularly helpful to the OP. Commented Mar 30, 2016 at 16:26
  • 1
    @Carpetsmoker: I agree that silent int main fix is not as helpful as commented fix. Indeed I advised Nathan to generally comment such a fix. And I agree it would be ungood to fix it if it was already mentioned in an answer. Both because there would be reduced reason to do it, and because it's all wrong to change that which an answer is based on. Apart from teaching the OP and other readers the Right Way to do things, and making the code portable, such a fix helps avoid giving an impression of the OP as one who writes void main. I.e. it can help yield more informative serious answers. Commented Mar 30, 2016 at 16:30
  • However, I think one should not have negative feelings about not being able to point out errors beyond what's asked about. Commented Mar 30, 2016 at 16:36
  • 14
    "Make use of the answer-function to point out typos [...]" - No. Comment, downvote and flag/vote to close in that order. We don't want typo questions on this site. So posting an answer makes it harder to get rid of it quickly.
    – Artjom B.
    Commented Mar 30, 2016 at 19:28
  • 6
    "simply because you cannot know if it's the cause of the specified problem" Horse puckey. If you know enough to post an answer to the problem, then you know enough to know which of the 300 lines of code dump are extraneous. Editing out superfluous material makes the question better, whether it's prose or code. "or the cause of a different problem" Questions here are expected to be about one thing, not fixing all the problems in someone's posted code.
    – jscs
    Commented Mar 30, 2016 at 19:45
  • 3
    This is wrong too: "Instead you should use an answer to point out typos or bad practices." Answers are for solving the problem; if an answer only addresses tangential issues, without providing a way to resolve the thing that caused the question, it isn't the kind of answer that we're here to collect. It's a reply, like in a forum. Then when you search and find the thread later, you have to read though 84 irrelevant "By the way, don't flibber the gibbet until..." "The Razzmatazz option was deprecated in 2.4..." tidbits before you find the piece of information you're actually looking for.
    – jscs
    Commented Mar 30, 2016 at 22:08
  • Woops, did not expect that much feedback. For clarification: My answer represents my point of view of this topic, which is arguably good for a "newbie". Of course people with sufficient knowledge of the topic can separate between the actual issue and a simple typo, but I - personally - prefer playing it safe, given my limited knowledge. Offending anyone was never my intention.
    – Seth
    Commented Mar 31, 2016 at 15:10
  • 2
    No one's offended, we just disagree. (A lot, in my case.) As CodeCaster said, "answers given to discussion questions on Meta are used as reference material to steer and defend future behavior". You've made some strong, absolute statements here, that appear to be directed to all editing users. If you intended them to be qualified in some way, you should edit to make that clear.
    – jscs
    Commented Mar 31, 2016 at 18:23
  • 1
    @JoshCaswell Done. I hope that makes it a bit more clear (and less absolute). Thanks for the feedback!
    – Seth
    Commented Apr 1, 2016 at 6:51
9

Only change void main to int main if you know that it isn't the cause of the OP's problem.

This usually requires that the problem be solved (either by you or someone else) before making the edit.

Removing "noise" from a question is not a bad thing, but only after you know it is noise.

3

In most cases, coding issues (including the trivial one indicated) should not be fixed without first checking with the original author. Usually a comment like "Is there a reason you are using void main() instead of the correct int main()?" is good enough. It lets later readers know that the code is not standard while giving an opportunity to the owner of the code to explain or approve the edit.

Questions are never good sources of code, as they are most often the problem that the original poster is trying to fix. As such, there is not enough value (after the comment) to update the code in a question, even if the original poster is not available to approve the edit.

Code in answers should be correct. If the original poster has not been seen for a while (at least a couple of months) and you are 100% sure that your change is correct and necessary, go ahead and make the change with a comment indicating what you changed and why "I changed void main() to int main() so it is in line with C++ standards." (the link is an example, in your comment you would link to the appropriate documentation).

If the original poster has been around recently, a comment asking why they are not following standards should be enough. Usually the original poster will make the correction for you, but if not, the comment is enough to warn future visitors.

-8

Code in an Answer or Question is intended first for compilation by one very special compiler: the human brain. All other compilers are a secondary concern.

This fantastic brain compiler can easily compile all kinds of code, and can even accept returning a value from a void.

Whether or not the code is Standard or otherwise acceptable by some unknown, dumb, and inflexible machine compiler is only relevant if the Question or Answer hinges on this specific Standard issue. In which case it should be called out in an Answer or Comment as such, with an example or diff provided that shows the difference. A simple "I had to change your code thusly to get it to compile..." will suffice.

Otherwise, the human brain treats it as noise to be filtered out. It does not matter if a potential machine compiler would or would not choke on the code; the primary intended compiler for SE is always the human reading the question.

Yes, folks writing Questions should strive to write complete, stand-alone, compilable examples. They should be encouraged to correct the code themselves, or ask for help to do so.

However, folks writing Answers, or wanting to assist future Answers with edits should not risk changing the intent unless everyone agrees it is a good idea, as this muddies the waters for everyone, and breaks history. It should not be something done unilaterally.

3
  • "Code in an Answer or Question is intended first for compilation by one very special compiler: the human brain" -- I disagree. Even ignoring that SO is used worldwide, by many people whose native language is not English, having valid code is critical for comprehension. That the human brain might be able to interpret invalid code is irrelevant; the point of providing real code (as opposed to pseudocode, which should be identified as such) is to provide something that will work when given to an actual compiler. Not the human brain. Commented Mar 30, 2016 at 21:16
  • I've made my case why I think you are wrong. After decades of experience I maintain that the compiler in our heads is what source code is for first. Even so, changing code in a question to satisfy standard pedantry isn't the sort of help helping is all about.
    – user1531971
    Commented Mar 30, 2016 at 21:40
  • 1
    Oh, don't get me wrong. I disagree with the edit in question, just as you do. It's just not for the reason you put forth here. Commented Mar 30, 2016 at 21:42
-13

If I see a question with void main(), I edit this out as fast as possible for the following reasons:

  • main must return an int. Not knowing it means the asker doesn't know much about very basic concepts of C/C++. The program must return an exit code to the OS.
  • this error is very easy to fix and may not be the cause of the problems the OP faces
8
  • 9
    Editing the question is not how you should provide an answer.
    – Blorgbeard
    Commented Mar 30, 2016 at 19:41
  • 5
    @Blorgbeard, if void main() is the source of the problem, then you're right. But it never is.
    – ForceBru
    Commented Mar 30, 2016 at 19:43
  • I disagree. I don't think code should ever be edited beyond simple formatting. Just because void main is not best practices, doesn't mean it should be edited out. Should we edit out sloppy code while we're at it? Bad code that is unrelated to the issue should be pointed out in comments.
    – DJMcMayhem
    Commented Mar 30, 2016 at 19:46
  • If you think void main() is not the source of the problem, then you should post a comment.
    – Blorgbeard
    Commented Mar 30, 2016 at 19:46
  • 1
    Okay so you edit it out. Do you inform the OP that void main() should not be used? If you do don't you think it looks silly when void main() doesn't show up in the code anymore? Commented Mar 30, 2016 at 19:50
  • @Blorgbeard, if it is a problem, the compiler complains about it pretty loudly so it can be fixed easily.
    – ForceBru
    Commented Mar 30, 2016 at 19:50
  • 2
    @NathanOliver, sure, I do. No, I don't. If the OP still experiences problems, then he may roll back.
    – ForceBru
    Commented Mar 30, 2016 at 19:53
  • 8
    Let's forget the OP for a moment. Leaving a comment is still useful for other people just getting started who are making the same void main() mistake. Rather than just silently covering it up, they can see a comment to not do it. Remembering the OP, it adds more of a friendly way to combat the problem rather than a mystery edit with no rational obvious to them
    – DeadChex
    Commented Mar 30, 2016 at 19:56

You must log in to answer this question.

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