13

Here's a situation I ran into recently, which other meta questions don't seem to cover. The OP wrote:

if( foo == bar )
do_stuff( fizz );
do_stuff( buzz );

So, formatting it for readability, the "correct" way:

if( foo == bar )
    do_stuff( fizz );
do_stuff( buzz );

However, the lack of brackets was part of the OP's problem. So, I did this:

if( foo == bar )
    do_stuff( fizz );
    do_stuff( buzz );

And pointed out the issue in the comments.

Does that make sense, or should I have done it the "correct" way?

1
  • 1
    Great answers--wish I could award the "answer" to all of you! I rolled back my edit and will keep this in mind going forward.
    – BryanH
    Feb 5, 2013 at 18:57

3 Answers 3

22

Since the indentation of the code is, in and of itself, a part of the problem, I wouldn't edit the indentation at all. I don't know if I feel strongly enough to roll back an edit of someone else who changed the indentation, but I would make a point of keeping my own hands off and address the problem(s) in the answer (or comments, depending on whether or not this issue is sufficient to answer the question).

1
  • 3
    +1 for I wouldn't edit the indentation at all.
    – juergen d
    Feb 5, 2013 at 17:05
9

Why is "tipping them off to the problem" something to avoid? Isn't that what we're here for? I would answer, assuming the lack of brackets is their problem, something like this:


Your problem is that you don't have brackets after your if statement. You seem to believe your code will behave like this:

if( foo == bar )
    do_stuff( fizz );
    do_stuff( buzz );

But since you have no brackets, it will actually behave like this:

if( foo == bar )
    do_stuff( fizz );
do_stuff( buzz );

(The compiler doesn't read indents.)

To get the behaviour you want, write the code like this:

if( foo == bar )
{
    do_stuff( fizz );
    do_stuff( buzz );
}

It's a good habit to always use the brackets to make your intensions clear, even when there is only one line to be executed when the if is true.


And I would leave the question completely unedited.

2
  • 4
    +1 for pointing out that "tipping them off to the problem" is kind of the point. I wouldn't re-format either, since it's essential to understanding (a) the problem, and (b) why indentation is more important than some people seem to understand. Feb 5, 2013 at 18:11
  • 2
    "Why is "tipping them off to the problem" something to avoid? Isn't that what we're here for?" Tipping them off to the problem by editing their question and not answering is a problem. You should tip them off to the problem by answering. I realize that's what you advocate, but I just want to emphasize that it really can be a significant problem when you answer a question through editing the answer into the question itself.
    – Servy
    Feb 5, 2013 at 18:27
7

I would not edit the code at all. Instead, I'd ask the OP to format his code first:

  1. If no reply. I leave. (Which is how I usually treat uninteresting ask-and-forget questions.)
  2. If he does and he notices - problem solved. Either answer in comment and/or close as too localized.
  3. If he does and doesn't notice - either answer or answer in comment.

Alternatively, I might just a leave a single comment and leave:

Hint: Format your code

You must log in to answer this question.

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