Skip to contentSkip to conversation

CODING_STYLE and if blocks

Qemu-devel Aurelien Jarno 5 participants 6 messages Oct 22, 2009 – Oct 23, 2009

lore
Share & tools
lore
#0 lore raw

Hi all,

I am currently reviewing the S390 patches which extensively use of code like:

    if (a == 5) printf("a was 5.\n");
    else if (a == 6) printf("a was 6.\n");
    else printf("a was something else entirely.\n");

It is something currently allowed by the CODING_STYLE document (there is no "indented statement"), but I am not fully comfortable with it. Should we accept such code? Should we fix CODING_STYLE?

Cheers,
Aurelien

Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
#1 lore raw
Quoted 5 lines · Aurelien Jarno

Hi all,

I am currently reviewing the S390 patches which extensively use of code like:

Quoted 8 lines
    if (a == 5) printf("a was 5.\n");
    else if (a == 6) printf("a was 6.\n");
    else printf("a was something else entirely.\n");

It is something currently allowed by the CODING_STYLE document (there is no "indented statement"), but I am not fully comfortable with it. Should we accept such code? Should we fix CODING_STYLE?

I'd vote for fixing CODING_STYLE as that syntax makes my eyes hurt.

While CODING_STYLE is there as a guideline, good taste should still always prevail :-)

Regards,

Anthony Liguori

Quoted 4 lines

Cheers,
Aurelien

#4 lore raw
In reply to Aurelien Jarno
Quoted 5 lines · Aurelien Jarno

Hi all,

I am currently reviewing the S390 patches which extensively use of code like:

Quoted 7 lines
    if (a == 5) printf("a was 5.\n");
    else if (a == 6) printf("a was 6.\n");
    else printf("a was something else entirely.\n");

It is something currently allowed by the CODING_STYLE document (there is no "indented statement"), but I am not fully comfortable with it. Should we accept such code? Should we fix CODING_STYLE?

In my opinion a line break and indent after the if () should be used except in the rare cases where keeping it all on a single line improves readability.

Signature · 2 lines
Måns Rullgård
[email protected]
#5 lore raw
In reply to Aurelien Jarno
  if (a == 5)
  {
      printf("a was 5.\n");
  }
  else if (a == 6)
  {
      printf("a was 6.\n");
  }
  else
  {
      printf("a was something else entirely.\n");
  }

Is in my opinion LOT CLEANER to debug.

This is my personal preference (comments included):
if (a == 5) // Check if it is 5

  {
      printf("a was 5.\n");
  }
  else // It was not 5
  {
      if (a == 6)
      {
           printf("a was 6.\n");
      }
      else // Neither was 6
      {
           printf("a was something else entirely.\n");
      }

}

Quoted 21 lines · Aurelien Jarno

Hi all,

I am currently reviewing the S390 patches which extensively use of code like:

  if (a == 5) printf("a was 5.\n");
  else if (a == 6) printf("a was 6.\n");
  else printf("a was something else entirely.\n");

It is something currently allowed by the CODING_STYLE document (there is no "indented statement"), but I am not fully comfortable with it. Should we accept such code? Should we fix CODING_STYLE?

Cheers,
Aurelien

Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
#2 lore raw
In reply to Anthony Liguori
Quoted 10 lines · Anthony Liguori
Quoted 9 lines · Aurelien Jarno

Hi all,

I am currently reviewing the S390 patches which extensively use of code like:

   if (a == 5) printf("a was 5.\n");
   else if (a == 6) printf("a was 6.\n");
   else printf("a was something else entirely.\n");
Quoted 8 lines
Quoted 4 lines

It is something currently allowed by the CODING_STYLE document (there is no "indented statement"), but I am not fully comfortable with it. Should we accept such code? Should we fix CODING_STYLE?

I'd vote for fixing CODING_STYLE as that syntax makes my eyes hurt.

While CODING_STYLE is there as a guideline, good taste should still always prevail :-)

I think Uli only wrote the code as is because CODING_STYLE told him to always use braces around one-liner statements. I don't see how

if (a == 5)

    printf("a was 5.\n");
else  if (a == 6)
    printf("a was 6.\n");
else
    printf("a was something else entirely.\n");

would be not readable. In fact I tend to use that code style myself a lot in places where it makes sense like:

if (r < 0)

    return r;

It would really hurt my eyes to have braces on these simple ifs every single time.

Alex

#3 lore raw
Quoted 35 lines · Alexander Graf
Quoted 17 lines · Anthony Liguori
Quoted 9 lines · Aurelien Jarno

Hi all,

I am currently reviewing the S390 patches which extensively use of code like:

   if (a == 5) printf("a was 5.\n");
   else if (a == 6) printf("a was 6.\n");
   else printf("a was something else entirely.\n");
Quoted 6 lines

It is something currently allowed by the CODING_STYLE document (there is
no "indented statement"), but I am not fully comfortable with it. Should
we accept such code? Should we fix CODING_STYLE?

Quoted 4 lines

I'd vote for fixing CODING_STYLE as that syntax makes my eyes hurt.

While CODING_STYLE is there as a guideline, good taste should still always prevail :-)

I think Uli only wrote the code as is because CODING_STYLE told him to always use braces around one-liner statements. I don't see how

if (a == 5)

    printf("a was 5.\n");
else  if (a == 6)
    printf("a was 6.\n");
else
    printf("a was something else entirely.\n");
Quoted 9 lines

would be not readable. In fact I tend to use that code style myself a lot in places where it makes sense like:

if (r < 0)

    return r;

It would really hurt my eyes to have braces on these simple ifs every single time.

It has been debated already a few times already, the argument against this it that it make patches more difficulty readable.

Signature · 2 lines
Aurelien Jarno	                        GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net