Skip to contentSkip to conversation

staging: iio: ad7280a: Group similar macros into enums

PATCH In review sayli karnik 4 participants 5 messages Mar 7, 2017 – Mar 13, 2017

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 staging: iio: ad7280a: Group similar macros into enums

The patch groups similar macros into enum data types.

---
Changes in v2:
Corrected the order of both the enums.
Used enum data type names instead of unsigned int inside struct ad7280_platform_data.

 drivers/staging/iio/adc/ad7280a.h | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
drivers/staging/iio/adc/ad7280a.h +14 -10
index ccfb90d..e22694f 100644
@@ -13,15 +13,19 @@
  * TODO: struct ad7280_platform_data needs to go into include/linux/iio
  */
 
-#define AD7280A_ACQ_TIME_400ns			0
-#define AD7280A_ACQ_TIME_800ns			1
-#define AD7280A_ACQ_TIME_1200ns			2
-#define AD7280A_ACQ_TIME_1600ns			3
+enum ad7280a_acq_time {
+	AD7280A_ACQ_TIME_400ns = 0,
+	AD7280A_ACQ_TIME_800ns,
+	AD7280A_ACQ_TIME_1200ns,
+	AD7280A_ACQ_TIME_1600ns,
+};
 
-#define AD7280A_CONV_AVG_DIS			0
-#define AD7280A_CONV_AVG_2			1
-#define AD7280A_CONV_AVG_4			2
-#define AD7280A_CONV_AVG_8			3
+enum ad7280a_conv_avg {
+	AD7280A_CONV_AVG_DIS = 0,
+	AD7280A_CONV_AVG_2,
+	AD7280A_CONV_AVG_4,
+	AD7280A_CONV_AVG_8,
+};
 
 #define AD7280A_ALERT_REMOVE_VIN5		BIT(2)
 #define AD7280A_ALERT_REMOVE_VIN4_VIN5		BIT(3)
@@ -29,8 +33,8 @@
 #define AD7280A_ALERT_REMOVE_AUX4_AUX5		BIT(1)
 
 struct ad7280_platform_data {
-	unsigned int		acquisition_time;
-	unsigned int		conversion_averaging;
+	enum ad7280a_acq_time		acquisition_time;
+	enum ad7280a_conv_avg		conversion_averaging;
 	unsigned int		chain_last_alert_ignore;
 	bool			thermistor_term_en;
 };
Signature · 1 line
2.7.4
#1 lore raw
Quoted 1 line · sayli karnik

The patch groups similar macros into enum data types.

Why?

What benefit does this provide?

Quoted 18 lines

---
Changes in v2:
Corrected the order of both the enums.
Used enum data type names instead of unsigned int inside struct ad7280_platform_data.

 drivers/staging/iio/adc/ad7280a.h | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
drivers/staging/iio/adc/ad7280a.h +0 -0
index ccfb90d..e22694f 100644
@@ -13,15 +13,19 @@
  * TODO: struct ad7280_platform_data needs to go into include/linux/iio
  */

drivers/staging/iio/adc/ad7280a.h line 20

Quoted 10 lines
drivers/staging/iio/adc/ad7280a.h +5 -4
 
-#define AD7280A_ACQ_TIME_400ns			0
-#define AD7280A_ACQ_TIME_800ns			1
-#define AD7280A_ACQ_TIME_1200ns			2
-#define AD7280A_ACQ_TIME_1600ns			3
+enum ad7280a_acq_time {
+	AD7280A_ACQ_TIME_400ns = 0,
+	AD7280A_ACQ_TIME_800ns,
+	AD7280A_ACQ_TIME_1200ns,
+	AD7280A_ACQ_TIME_1600ns,

Before your change, you instantly knew that the 1600ns value needed to be 3. Now I need to count an enum. Does this seem like a better change? Are you _sure_ the compiler sets the values properly?

thanks,

greg k-h

#2 lore raw
Quoted 6 lines · Greg Kroah-Hartman
Quoted 1 line · sayli karnik

The patch groups similar macros into enum data types.

Why?

What benefit does this provide?

As suggested by Lars, I converted the macros to an enum data type, so that it is clear what their function is-
https://groups.google.com/d/msg/outreachy-kernel/VM6Wu-fJVfQ/VngpohNBAwAJ

Quoted 9 lines
Quoted 9 lines

---
Changes in v2:
Corrected the order of both the enums.
Used enum data type names instead of unsigned int inside struct ad7280_platform_data.

Quoted 4 lines

Review

Quoted 4 lines
 drivers/staging/iio/adc/ad7280a.h | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
(quoted diff) +0 -0

b/drivers/staging/iio/adc/ad7280a.h

Quoted 7 lines
Quoted 7 lines

index ccfb90d..e22694f 100644

drivers/staging/iio/adc/ad7280a.h +0 -0
@@ -13,15 +13,19 @@
  * TODO: struct ad7280_platform_data needs to go into include/linux/iio
  */
 
Quoted 13 lines

drivers/staging/iio/adc/ad7280a.h line 20

Quoted 9 lines
drivers/staging/iio/adc/ad7280a.h +5 -4
-#define AD7280A_ACQ_TIME_400ns                       0
-#define AD7280A_ACQ_TIME_800ns                       1
-#define AD7280A_ACQ_TIME_1200ns                      2
-#define AD7280A_ACQ_TIME_1600ns                      3
+enum ad7280a_acq_time {
+     AD7280A_ACQ_TIME_400ns = 0,
+     AD7280A_ACQ_TIME_800ns,
+     AD7280A_ACQ_TIME_1200ns,
+     AD7280A_ACQ_TIME_1600ns,

Before your change, you instantly knew that the 1600ns value needed to be 3. Now I need to count an enum. Does this seem like a better change?

Hmm we do need to count.
But,
-I feel grouping into an enum organizes similar #defines in a better way.

-You get free compile time checking of valid values using enums. For example

public static int VALUE_ONE = 0;public static int VALUE_TWO = 1;

does not ensure,

void selectOption(int value) {...}

will only accept 0 or 1 as a parameter value. Using an enum, that is guaranteed.

-Also it avoids the repetitive #defines.

Quoted 5 lines

Are you _sure_ the compiler sets the values properly?

thanks,

greg k-h

#3 lore raw
Quoted 58 lines · sayli karnik
Quoted 6 lines · Greg Kroah-Hartman
Quoted 1 line · sayli karnik

The patch groups similar macros into enum data types.

Why?

What benefit does this provide?

As suggested by Lars, I converted the macros to an enum data type, so that it is clear what their function is-
https://groups.google.com/d/msg/outreachy-kernel/VM6Wu-fJVfQ/VngpohNBAwAJ

Quoted 9 lines
Quoted 9 lines

---
Changes in v2:
Corrected the order of both the enums.
Used enum data type names instead of unsigned int inside struct ad7280_platform_data.

Quoted 4 lines

Review

Quoted 4 lines

 drivers/staging/iio/adc/ad7280a.h | 24 ++++++++++++++----------

 1 file changed, 14 insertions(+), 10 deletions(-)
(quoted diff) +0 -0

b/drivers/staging/iio/adc/ad7280a.h

Quoted 7 lines
Quoted 7 lines

index ccfb90d..e22694f 100644

drivers/staging/iio/adc/ad7280a.h +0 -0
@@ -13,15 +13,19 @@
   * TODO: struct ad7280_platform_data needs to go into include/linux/iio
   */
 
Quoted 13 lines

drivers/staging/iio/adc/ad7280a.h line 20

Quoted 9 lines
drivers/staging/iio/adc/ad7280a.h +5 -4
-#define AD7280A_ACQ_TIME_400ns                       0
-#define AD7280A_ACQ_TIME_800ns                       1
-#define AD7280A_ACQ_TIME_1200ns                      2
-#define AD7280A_ACQ_TIME_1600ns                      3
+enum ad7280a_acq_time {
+     AD7280A_ACQ_TIME_400ns = 0,
+     AD7280A_ACQ_TIME_800ns,
+     AD7280A_ACQ_TIME_1200ns,
+     AD7280A_ACQ_TIME_1600ns,

Before your change, you instantly knew that the 1600ns value needed to be 3.  Now I need to count an enum.  Does this seem like a better change?

Hmm we do need to count.
But,

(quoted diff) +0 -2
-I feel grouping into an enum organizes similar #defines in a better way.
 
-You get free compile time checking of valid values using enums. For example

public static int VALUE_ONE = 0;
public static int VALUE_TWO = 1;

Quoted 7 lines

does not ensure,

void selectOption(int value) {
...
}
will only accept 0 or 1 as a parameter value. Using an enum, that is guaranteed.

Regrettably in C it is not.

Still, using enums does make it possible to design tools that do such checking. With a random bunch of defines it is less clear what goes with what.

Quoted 4 lines

-Also it avoids the repetitive #defines.

Quoted 1 line

Are you _sure_ the compiler sets the values properly?

Page 105 of this document

WG14/N1256 Committee Draft — Septermber 7, 2007 ISO/IEC 9899:TC3
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf

says:

An enumerator with = defines its enumeration constant as the value of the constant expression. If the first enumerator has no =, the value of its enumeration constant is 0. Each subsequent enumerator with no = defines its enumeration constant as the value of the constant expression obtained by adding 1 to the value of the previous enumeration constant.

One could also check whether the actual value is important, eg the constant is passed to a device, or if it is just used as an internal flag.

julia

Quoted 16 lines
Quoted 4 lines

thanks,

greg k-h

--
You received this message because you are subscribed to the Google Groups
"outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visithttps://groups.google.com/d/msgid/outreachy-kernel/CAKG5xWiuKm2VboRAR2ALz%2 BSnHPbcBTQvH2Hw%2BRpkfm2P%2BGK2Qg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

maintainer
#4 lore raw
Quoted 95 lines · Julia Lawall
Quoted 58 lines · sayli karnik
Quoted 6 lines · Greg Kroah-Hartman
Quoted 1 line · sayli karnik

The patch groups similar macros into enum data types.

Why?

What benefit does this provide?

As suggested by Lars, I converted the macros to an enum data type, so that it is clear what their function is-
https://groups.google.com/d/msg/outreachy-kernel/VM6Wu-fJVfQ/VngpohNBAwAJ

Quoted 9 lines
Quoted 9 lines

---
Changes in v2:
Corrected the order of both the enums.
Used enum data type names instead of unsigned int inside struct ad7280_platform_data.

Quoted 4 lines

Review

Quoted 4 lines
 drivers/staging/iio/adc/ad7280a.h | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
(quoted diff) +0 -0

b/drivers/staging/iio/adc/ad7280a.h

Quoted 7 lines
Quoted 7 lines

index ccfb90d..e22694f 100644

drivers/staging/iio/adc/ad7280a.h +0 -0
@@ -13,15 +13,19 @@
  * TODO: struct ad7280_platform_data needs to go into include/linux/iio
  */
 
Quoted 13 lines

drivers/staging/iio/adc/ad7280a.h line 20

Quoted 9 lines
drivers/staging/iio/adc/ad7280a.h +5 -4
-#define AD7280A_ACQ_TIME_400ns                       0
-#define AD7280A_ACQ_TIME_800ns                       1
-#define AD7280A_ACQ_TIME_1200ns                      2
-#define AD7280A_ACQ_TIME_1600ns                      3
+enum ad7280a_acq_time {
+     AD7280A_ACQ_TIME_400ns = 0,
+     AD7280A_ACQ_TIME_800ns,
+     AD7280A_ACQ_TIME_1200ns,
+     AD7280A_ACQ_TIME_1600ns,

Before your change, you instantly knew that the 1600ns value needed to be 3. Now I need to count an enum. Does this seem like a better change?

Hmm we do need to count.
But,

(quoted diff) +0 -2
-I feel grouping into an enum organizes similar #defines in a better way.
 
-You get free compile time checking of valid values using enums. For example

public static int VALUE_ONE = 0;
public static int VALUE_TWO = 1;

Quoted 7 lines

does not ensure,

void selectOption(int value) {
...
}
will only accept 0 or 1 as a parameter value. Using an enum, that is guaranteed.

Regrettably in C it is not.

Still, using enums does make it possible to design tools that do such checking. With a random bunch of defines it is less clear what goes with what.

Quoted 4 lines

-Also it avoids the repetitive #defines.

Quoted 1 line

Are you _sure_ the compiler sets the values properly?

Page 105 of this document

WG14/N1256 Committee Draft — Septermber 7, 2007 ISO/IEC 9899:TC3
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf

says:

An enumerator with = defines its enumeration constant as the value of the constant expression. If the first enumerator has no =, the value of its enumeration constant is 0. Each subsequent enumerator with no = defines its enumeration constant as the value of the constant expression obtained by adding 1 to the value of the previous enumeration constant.

Quoted 2 lines

One could also check whether the actual value is important, eg the constant is passed to a device, or if it is just used as an internal flag.

This is the key element to my mind. Scan indexes are just a software
construction. We want them to be consistent, but don't really care exactly what they are as long as the are monotonic.

For register values like these ones it gets less obvious. Personally I prefer the consistency of just having them as defines, even though you loose the checking of type, which can be useful.

Jonathan

Quoted 20 lines

julia

Quoted 16 lines
Quoted 4 lines

thanks,

greg k-h

--
You received this message because you are subscribed to the Google Groups
"outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visithttps://groups.google.com/d/msgid/outreachy-kernel/CAKG5xWiuKm2VboRAR2ALz%2 BSnHPbcBTQvH2Hw%2BRpkfm2P%2BGK2Qg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.