Subject: Re: [PATCH] bonding: use pre-defined macro in bond_mode_name instead of magic number 0

From: Wang Sheng-Hui <[email-masked]>
Date: Wed, 24 Jul 2013 20:38:02 +0800

On 2013年07月24日 15:10, Joe Perches wrote:
> On Wed, 2013-07-24 at 14:53 +0800, Wang Sheng-Hui wrote:
>> We have BOND_MODE_ROUNDROBIN pre-defined as 0, and it's the lowest mode number.
>> Use it to check the arg lower bound instead of magic number 0 in bond_mode_name.
> []
>> diff --git a/drivers/net/bonding/bond_main.c b/drivers/net/bonding/bond_main.c
> []
>> @@ -273,7 +273,7 @@ const char *bond_mode_name(int mode)
>>    		[BOND_MODE_ALB] = "adaptive load balancing",
>>    	};
>>
>> -	if (mode < 0 || mode > BOND_MODE_ALB)
>> +	if (mode < BOND_MODE_ROUNDROBIN || mode > BOND_MODE_ALB)
>>    		return "unknown";
>>
>>    	return names[mode];
>
> Probably be simpler, less confusing, and more normal style
> to use a switch case.
>
> 	switch (mode) {
> 	case BOND_MODE_ROUNDROBIN:
> 		return "load balancing (round-robin)";
> 	case BOND_MODE_ACTIVEBACKUP:
> 		return "fault-tolerance (active-backup)";
> 	case BOND_MODE_XOR:
> 		return "load balancing (xor)";
> 	case BOND_MODE_BROADCAST;
> 		return "fault-tolerance (broadcast)";
> 	case BOND_MODE_8023AD:
> 		return "IEEE 802.3ad Dynamic link aggregation";
> 	case BOND_MODE_TLB:
> 		return "transmit load balancing";
> 	case BOND_MODE_ALB:
> 		return "adaptive load balancing";
> 	default:
> 		return "unknown";
> 	}
>
>

Regenerated the patch on Joe's advice. Please check it.  Thanks,

[PATCH] bonding: use switch case to make bond_mode_name more clear

We have predefined the mode macros, and should avoid the
magic number 0 in the old version. Also, use a switch case
would be simpler, less confusing, and more normal style.

Signed-off-by: Wang Sheng-Hui <[email-masked]>
---
  drivers/net/bonding/bond_main.c |   30 +++++++++++++++++-------------
  1 file changed, 17 insertions(+), 13 deletions(-)

diff --git a/drivers/net/bonding/bond_main.c b/drivers/net/bonding/bond_main.c
index 07f257d4..69bfb4c 100644
--- a/drivers/net/bonding/bond_main.c
+++ b/drivers/net/bonding/bond_main.c
@@ -263,20 +263,24 @@ static void bond_uninit(struct net_device *bond_dev);

  const char *bond_mode_name(int mode)
  {
-	static const char *names[] = {
-		[BOND_MODE_ROUNDROBIN] = "load balancing (round-robin)",
-		[BOND_MODE_ACTIVEBACKUP] = "fault-tolerance (active-backup)",
-		[BOND_MODE_XOR] = "load balancing (xor)",
-		[BOND_MODE_BROADCAST] = "fault-tolerance (broadcast)",
-		[BOND_MODE_8023AD] = "IEEE 802.3ad Dynamic link aggregation",
-		[BOND_MODE_TLB] = "transmit load balancing",
-		[BOND_MODE_ALB] = "adaptive load balancing",
-	};
-
-	if (mode < 0 || mode > BOND_MODE_ALB)
+	switch (mode) {
+	case BOND_MODE_ROUNDROBIN:
+		return "load balancing (round-robin)";
+	case BOND_MODE_ACTIVEBACKUP:
+		return "fault-tolerance (active-backup)";
+	case BOND_MODE_XOR:
+		return "load balancing (xor)";
+	case BOND_MODE_BROADCAST:
+		return "fault-tolerance (broadcast)";
+	case BOND_MODE_8023AD:
+		return "IEEE 802.3ad Dynamic link aggregation";
+	case BOND_MODE_TLB:
+		return "transmit load balancing";
+	case BOND_MODE_ALB:
+		return "adaptive load balancing";
+	default:
  		return "unknown";
-
-	return names[mode];
+	}
  }

  /*---------------------------------- VLAN -----------------------------------*/
-- 
1.7.10.4