3

I usually read the tag here on Stack Overflow and sometimes in the questions there are long queries with very little code on every single line, for example this question.

The particular point is the second code block:

...
SELECT   C.KeyCompany ,
         CASE WHEN ( GROUPING(O.O1) = 1 ) THEN ''
              ELSE O.O1
         END AS O1 ,
         NULL AS O2 ,
         NULL AS O3 ,
         NULL AS O4 ,
         NULL AS O5 ,
         NULL AS O6 ,
         NULL AS O7 ,
         NULL AS O8 ,
         NULL AS O9 ,
         NULL AS O10 ,
         NULL AS O11 ,
         NULL AS O12 ,
         1 AS viewDepth
FROM     dbo.COMPANY C
         INNER JOIN dbo.SUBCOMPANY SC ON SC.KeyCompany = C.KeyCompany
                                          AND ( C.viewdepth + 1 ) = 1
         INNER JOIN dbo.Organistation O ON O.KeySubCompany = SC.KeySubCompany
GROUP BY C.KeyCompany ,
         O.O1
         WITH ROLLUP
...

I thought about edit it to something like

...
SELECT   C.KeyCompany ,
         CASE WHEN ( GROUPING(O.O1) = 1 ) THEN ''
              ELSE O.O1
         END AS O1 ,
         NULL AS O2, NULL AS O3, NULL AS O4, NULL AS O5, NULL AS O6, NULL AS O7, 
         NULL AS O8, NULL AS O9, NULL AS O10, NULL AS O11, NULL AS O12,
         1 AS viewDepth
FROM     dbo.COMPANY C
         INNER JOIN dbo.SUBCOMPANY SC ON SC.KeyCompany = C.KeyCompany
                                          AND ( C.viewdepth + 1 ) = 1
         INNER JOIN dbo.Organistation O ON O.KeySubCompany = SC.KeySubCompany
GROUP BY C.KeyCompany, O.O1 WITH ROLLUP
...

to increase the signal/noise ratio (the NULL columns are noise IMO), and reduce the length of the vertical scroll, but I refrained as not sure if this will be useful for SO.

The same thing can happen in other language if the write use extra blank line (3 or 5 instead of 1) to separate code blocks.

Is there a guideline or a common way to handle case like this?

5
  • 2
    I try not to push my stylistic preferences on others, 1 column per line is a pretty widely accepted standard. I do think there is benefit to making scrollable code blocks non-scrolling if it can be done with very minor changes, but in the linked question, that ain't gonna happen.
    – OGHaza
    May 13, 2014 at 12:09
  • @OGHaza my stylistic preference will go for the first, if I wanted to push my preference I would have edited the comma at the beginning of the lines, but you can see I haven't change it, and you're right no edit can make that wall of code non-scrolling
    – Serpiton
    May 13, 2014 at 12:17
  • Don't get me wrong, I don't personally think it's a bad edit and I'm sure it wouldn't be rolled back - after all there is no problem with the code to accidentally obscure, it's working code. If I was the OP I'd certainly have done something to improve readability - for starters given the fragment of SQL he's chosen to include, he could have excluded the columns 04 and onwards entirely.
    – OGHaza
    May 13, 2014 at 12:38
  • @Serpiton: Wouldn't you want to increase the signal/noise ratio? Jul 1, 2016 at 19:06
  • @NisseEngström You're right
    – Serpiton
    Jul 3, 2016 at 8:45

2 Answers 2

7

Don't do this.

Editing the code in questions is very dangerous as there's a good chance that you'll edit out the problem.

This will make the question useless.

The only "safe" edits to make are those that fix formatting issues - i.e. add enough leading spaces on each line to actually make the code look like code.

2
  • Would the same apply to answers? Should we give the same guidance to users who are editing answers to update code or add error checking, etc.?
    – Mogsdad
    Mar 24, 2016 at 20:01
  • 2
    @Mogsdad editing code in answers is less problematic, but should be done with care. You can fix errors but it would be better to leave a comment. If you find yourself making extensive changes you might be better off creating a new answer.
    – ChrisF Mod
    Mar 24, 2016 at 20:21
2

In at least some cases, the confusion in the question can actually be caused by poor formatting. I would be very very cautious about changing the formatting of code in a question. Also, preferences differ. Personally, I prefer single-column per line.

You must log in to answer this question.

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