Skip to contentSkip to conversation

Staging: comedi: Replace if-else with ternary operator

PATCH In review Vatika Harlalka 3 participants 3 messages Mar 2, 2015 Staging

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Staging: comedi: Replace if-else with ternary operator

This patch uses Coccinelle script to replace the if-else block with ternary operator to make it more compact.

---
 drivers/staging/comedi/drivers.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/staging/comedi/drivers.c +3 -6
index f32e714..8fda5f3 100644
@@ -363,8 +363,7 @@ unsigned int comedi_nscans_left(struct comedi_subdevice *s,
 		if (async->scans_done < cmd->stop_arg)
 			scans_left = cmd->stop_arg - async->scans_done;
 
-		if (nscans > scans_left)
-			nscans = scans_left;
+		nscans = (nscans <= scans_left) ? nscans : scans_left;
 	}
 	return nscans;
 }
@@ -397,8 +396,7 @@ unsigned int comedi_nsamples_left(struct comedi_subdevice *s,
 					cmd->scan_end_arg) - scan_pos;
 		}
 
-		if (samples_left < nsamples)
-			nsamples = samples_left;
+		nsamples = (nsamples <= samples_left) ? nsamples : samples_left;
 	}
 	return nsamples;
 }
@@ -540,8 +538,7 @@ static int __comedi_device_postconfig_async(struct comedi_device *dev,
 
 	async->max_bufsize = comedi_default_buf_maxsize_kb * 1024;
 	buf_size = comedi_default_buf_size_kb * 1024;
-	if (buf_size > async->max_bufsize)
-		buf_size = async->max_bufsize;
+	buf_size = (buf_size <= async->max_bufsize) ? buf_size : async->max_bufsize;
 
 	if (comedi_buf_alloc(dev, s, buf_size) < 0) {
 		dev_warn(dev->class_dev, "Buffer allocation failed\n");
Signature · 1 line
1.9.1
#1 lore raw
Quoted 13 lines · Vatika Harlalka

This patch uses Coccinelle script to replace the if-else block with ternary operator to make it more compact.

---
 drivers/staging/comedi/drivers.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/staging/comedi/drivers.c +0 -0
index f32e714..8fda5f3 100644

drivers/staging/comedi/drivers.c unsigned int comedi_nscans_left(struct comedi_subdevice *s, · line 366

Quoted 6 lines
drivers/staging/comedi/drivers.c +1 -2
@@ -363,8 +363,7 @@ unsigned int comedi_nscans_left(struct comedi_subdevice *s,
                if (async->scans_done < cmd->stop_arg)
                        scans_left = cmd->stop_arg - async->scans_done;
 
-               if (nscans > scans_left)
-                       nscans = scans_left;
+               nscans = (nscans <= scans_left) ? nscans : scans_left;

This doesn't look as an improvement to me. It even makes code harder to read.

#2 lore raw
Quoted 18 lines · Daniel Baluta
Quoted 16 lines · Vatika Harlalka

This patch uses Coccinelle script to replace the if-else block with ternary operator to make it more compact.

---
 drivers/staging/comedi/drivers.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/staging/comedi/drivers.c +0 -0
index f32e714..8fda5f3 100644
@@ -363,8 +363,7 @@ unsigned int comedi_nscans_left(struct comedi_subdevice *s,
                if (async->scans_done < cmd->stop_arg)
                        scans_left = cmd->stop_arg - async->scans_done;
 
Quoted 5 lines

drivers/staging/comedi/drivers.c unsigned int comedi_nscans_left(struct comedi_subdevice *s, · line 366

Quoted 3 lines
drivers/staging/comedi/drivers.c +1 -2
-               if (nscans > scans_left)
-                       nscans = scans_left;
+               nscans = (nscans <= scans_left) ? nscans : scans_left;

This doesn't look as an improvement to me. It even makes code harder to read.

I don't think that kernel developers are so used to using ?. In any case, I don't remember seeing it much. Perhaps it could be useful if there are two branches and both branches assign the same variable to a constant.

julia