Skip to contentSkip to conversation

Comma at end of enum lists

DISCUSSION Jacek Luczak 6 participants 15 messages Mar 29, 2008

lore
Share & tools
lore
#0 lore raw

Hi All,

I've found that in many enum lists, there's a comma at the end, e.g. (arch/x86/kernel/early_printk.c):

enum {
        MAGIC1 = 0xBACCD00A,
        MAGIC2 = 0xCA110000,
        XOPEN = 5,
        XWRITE = 4,
};

Just out of curiosity, is there any particular reason here (no word in CodingStyle about that).

-Jacek

#1 lore raw
Quoted 12 lines · Jacek Luczak

Hi All,

I've found that in many enum lists, there's a comma at the end, e.g. (arch/x86/kernel/early_printk.c):

enum {
         MAGIC1 = 0xBACCD00A,
         MAGIC2 = 0xCA110000,
         XOPEN = 5,
         XWRITE = 4,
};
Quoted 2 lines

Just out of curiosity, is there any particular reason here (no word in CodingStyle about that).

Yes. This idiom allows you to add or remove items without changing adjacent lines.

In a language with strict a comma-as-separator rule you can get this benefit by placing the comma before new items rather than after existing items:

enum { FOO
,FIE
,FUM
};

but luckily C doesn't need this perversion.

#6 lore raw
In reply to Jacek Luczak
Quoted 4 lines · Jacek Luczak

Hi All,

I've found that in many enum lists, there's a comma at the end, e.g. (arch/x86/kernel/early_printk.c):

Quoted 9 lines
enum {
        MAGIC1 = 0xBACCD00A,
        MAGIC2 = 0xCA110000,
        XOPEN = 5,
        XWRITE = 4,
};

Just out of curiosity, is there any particular reason here (no word in CodingStyle about that).

Yes, it's so you can add a line without affecting the line before it, making a one-line patch into a two-line patch that's more likely to conflict.

-hpa

#2 lore raw
In reply to Mikael Pettersson

Mikael Pettersson pisze:

Quoted 13 lines
Quoted 12 lines · Jacek Luczak

Hi All,

I've found that in many enum lists, there's a comma at the end, e.g. (arch/x86/kernel/early_printk.c):

enum {
         MAGIC1 = 0xBACCD00A,
         MAGIC2 = 0xCA110000,
         XOPEN = 5,
         XWRITE = 4,
};
Quoted 5 lines
Quoted 2 lines

Just out of curiosity, is there any particular reason here (no word in CodingStyle about that).

Yes. This idiom allows you to add or remove items without changing adjacent lines.

Yep, that's obvious, one line less in diff after every enum change :)

Quoted 11 lines

In a language with strict a comma-as-separator rule you can get this benefit by placing the comma before new items rather than after existing items:

enum { FOO
,FIE
,FUM
};

but luckily C doesn't need this perversion.

I was just curious, because it's not common schema (some miss extra comma).

Thanks,
-Jacek

#7 lore raw
In reply to H. Peter Anvin
Quoted 13 lines · H. Peter Anvin
Quoted 12 lines · Jacek Luczak

Hi All,

I've found that in many enum lists, there's a comma at the end, e.g. (arch/x86/kernel/early_printk.c):

enum {
       MAGIC1 = 0xBACCD00A,
       MAGIC2 = 0xCA110000,
       XOPEN = 5,
       XWRITE = 4,
};
Quoted 7 lines
Quoted 3 lines

Just out of curiosity, is there any particular reason here (no word in CodingStyle about that).

Yes, it's so you can add a line without affecting the line before it, making a one-line patch into a two-line patch that's more likely to conflict.

Note that doing that makes sense only when you can expect additions to the end and even then it's a matter of taste.

#8 lore raw
Quoted 13 lines · Al Viro
Quoted 12 lines · H. Peter Anvin
Quoted 11 lines · Jacek Luczak

Hi All,

I've found that in many enum lists, there's a comma at the end, e.g. (arch/x86/kernel/early_printk.c):

enum {
       MAGIC1 = 0xBACCD00A,
       MAGIC2 = 0xCA110000,
       XOPEN = 5,
       XWRITE = 4,
};
Quoted 6 lines
Quoted 3 lines
Quoted 2 lines

Just out of curiosity, is there any particular reason here (no word in CodingStyle about that).

Yes, it's so you can add a line without affecting the line before it, making a one-line patch into a two-line patch that's more likely to conflict.

Note that doing that makes sense only when you can expect additions to the end and even then it's a matter of taste.

Yes, it is.

I personally prefer it this way (strongly) for exactly the same reason C requires a semicolon at the end of each statement, as opposed to Pascal which doesn't require a semicolon immediately before an "end".

-hpa

#12 lore raw
In reply to Al Viro

Al Viro pisze:

Quoted 13 lines · H. Peter Anvin
Quoted 12 lines
Quoted 11 lines · Jacek Luczak

Hi All,

I've found that in many enum lists, there's a comma at the end, e.g. (arch/x86/kernel/early_printk.c):

enum {
       MAGIC1 = 0xBACCD00A,
       MAGIC2 = 0xCA110000,
       XOPEN = 5,
       XWRITE = 4,
};
Quoted 7 lines
Quoted 3 lines
Quoted 2 lines

Just out of curiosity, is there any particular reason here (no word in CodingStyle about that).

Yes, it's so you can add a line without affecting the line before it, making a one-line patch into a two-line patch that's more likely to conflict.

Note that doing that makes sense only when you can expect additions to the end and even then it's a matter of taste.

I think it's hard to ,,expect additions'' or just predict them. But smaller patch (diff) is one of things that makes sens of adding extra commas. I'm just pedantic here.

-Jacek

#9 lore raw
In reply to H. Peter Anvin
Quoted 10 lines · H. Peter Anvin
Quoted 3 lines

Note that doing that makes sense only when you can expect additions to the end and even then it's a matter of taste.

Yes, it is.

I personally prefer it this way (strongly) for exactly the same reason C requires a semicolon at the end of each statement, as opposed to Pascal which doesn't require a semicolon immediately before an "end".

...as opposed to Perl which does not strictly require a ; at the end of a block. :>

# while (1) { print 1; print 2 }
#10 lore raw
In reply to H. Peter Anvin
Quoted 5 lines · H. Peter Anvin

Yes, it is.

I personally prefer it this way (strongly) for exactly the same reason C requires a semicolon at the end of each statement, as opposed to Pascal which doesn't require a semicolon immediately before an "end".

enum foo {
Foo,
Bar,
Baz,
Max_foo = Baz
};

when you know there won't any additions past the last line (for that matter, ending it with
Baz,
Sentry_foo,
Max_foo = Sentry_foo - 1
}; is also an option, in which case the end of enum will never be touched at all).

enum {Some_constant = ...};

when you do not want to mix it with other constants and use a separate enum for each natural group.

Sometimes it makes sense, sometimes it doesn't. It is an expressive element, not something to be cast in stone by Documentation/CodingStyle.

#11 lore raw
Quoted 9 lines · Al Viro

when you know there won't any additions past the last line (for that matter, ending it with
Baz,
Sentry_foo,
Max_foo = Sentry_foo - 1
}; is also an option, in which case the end of enum will never be touched at all).

Yes, that's a very important exception. Don't put the comma in if you want the set to be actively closed.

-hpa

#13 lore raw
In reply to Jacek Luczak
Quoted 1 line · Jacek Luczak

I think it's hard to ,,expect additions'' or just predict them.

Depends on the situation.

Quoted 3 lines

But
smaller patch (diff) is one of things that makes sens of adding extra commas. I'm just pedantic here.

Fine, but please let's not turn that into yet another brainless crusade; we already have enough of those going...

#14 lore raw
Quoted 2 lines · Al Viro

Fine, but please let's not turn that into yet another brainless crusade; we already have enough of those going...

Amen!

-hpa

#3 lore raw
In reply to Mikael Pettersson
Quoted 10 lines · Mikael Pettersson

In a language with strict a comma-as-separator rule you can get this benefit by placing the comma before new items rather than after existing items:

enum { FOO
,FIE
,FUM
};

but luckily C doesn't need this perversion.

Only since C99 (but GNU C never needed it either).

Andreas.

Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected]
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#4 lore raw
Quoted 4 lines · Andreas Schwab
Quoted 4 lines

In a language with strict a comma-as-separator rule you can get this benefit by placing the comma before new items rather than after existing items:

Quoted 8 lines
Quoted 6 lines

enum { FOO
,FIE
,FUM
};

but luckily C doesn't need this perversion.

Only since C99 (but GNU C never needed it either).

C had this for much longer than 99. Borland Turbo C from around 1990 (which you can expect to be C89 if you have luck) also allows , at the end.

#5 lore raw
Quoted 10 lines · Jan Engelhardt
Quoted 9 lines · Andreas Schwab
Quoted 9 lines

In a language with strict a comma-as-separator rule you can get this benefit by placing the comma before new items rather than after existing items:

enum { FOO
,FIE
,FUM
};

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

but luckily C doesn't need this perversion.

Only since C99 (but GNU C never needed it either).

C had this for much longer than 99. Borland Turbo C from around 1990 (which you can expect to be C89 if you have luck) also allows , at the end.

C89 definitely does not allow a comma at the end of the enumerator list (only at the end of the initializer list). If Borland C allows it in its strict C89 mode (if it has such a thing) then it is buggy.

Andreas.

Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected]
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."