With a growing number of more or less inexperienced programmers on Stack Overflow, it is frequent to see the questions containing code not only badly formatted, but also badly written. While editing a question to fix the formatting is okay, I'm wondering if it is correct, when editing a question, to bring small changes to the code to make it more readable. In other words, until when is it acceptable to do a modification of a piece of code in a question?
Example
Let's say there is a question with following crappy C# source code:
void DoStuff( string filename ) {
String MsgToDisplay = "";
Stream stFile = new FileStream(filename);
if (stFile.Length > 0)
{
Boolean result=this.validateFile( stFile );
if (result==true)
MsgToDisplay="your file was submitted";
else
MsgToDisplay="the file is invalid!";
}
return MsgToDisplay;
}
State 1: The first thing to do is a general cleanup just to make it readable:
void DoStuff(string filename)
{
String MsgToDisplay = "";
Stream stFile = new FileStream(filename);
if (stFile.Length > 0)
{
Boolean result = this.validateFile(stFile);
if (result == true)
{
MsgToDisplay = "your file was submitted";
}
else
{
MsgToDisplay = "the file is invalid!";
}
}
return MsgToDisplay;
}
State 2: The next thing may be to make some changes according to the style guidelines for C# and other minor changes which does not affect the code: proper case, bool
instead of Boolean
, etc. The original author can still recognize her own code and if she understands well the code she's written, she will understand the new one.
private string DoStuff(string fileName)
{
string messageToDisplay = string.Empty;
Stream stream = new FileStream(fileName);
if (stream.Length > 0)
{
bool result = this.ValidateFile(stFile);
if (result)
{
messageToDisplay = "Your file was submitted.";
}
else
{
messageToDisplay = "The file is invalid.";
}
}
return messageToDisplay;
}
State 3: The last thing to do: make the more substantial changes by bringing things the original author may not know if she's a beginner:
private string DoStuff(string fileName)
{
string messageToDisplay = string.Empty;
bool? isFileValid = null;
using (Stream stream = new FileStream(fileName))
{
if (stream.Length > 0)
{
isFileValid = this.ValidateFile(stFile);
}
}
if (isFileValid.HasValue)
{
messageToDisplay = isFileValid.Value ? "Your file was submitted." : "The file is invalid.";
}
return messageToDisplay;
}
My thoughts
Until now, I thought it may be a good idea to go until the state 2, but never do the state 3. If we do the first two states:
- It leverages the quality of the content of Stack Overflow in general and shows that this is not a sort of Q&A website where you can post questions without bothering to format them correctly
- When other beginners are reading well-formatted code, they will probably try to write well-formatted code outside Stack Overflow too
- The original author will see that her code was reformatted and maybe make an effort later to format the code by her own. She can also wonder herself why
String
was transformed intostring
, why there is aif (result)
instead ofif (result == true)
, etc
I had a lot of pain to go to the state 3, because:
- It changes code so much that the original author may not understand why her own code was replaced by some stuff she doesn't even understand.
- The modified code may be quoted in the answers. When the original author will try to link the answers to her own, original piece of code, she may not understand an answer, and will blame the answerer, instead of me (or the person who modified the question).
- There is a risk of changing the original algorithm, just by mistake.
On the other hand,
- I see a lot of answers which start by enumerating the incorrect parts of the source code: forgotten
using
s onIDisposable
objects, possible SQL Injection, etc. I don't think those things have their place in the answers. - Again, keeping questions containing code with SQL Injection and without appropriate
using
s reduce the quality of Stack Overflow content. It is especially important since many beginners copy-paste code from Stack Overflow questions/answers without understanding it, nor reading the whole page with all the answers and comments.