Explaining variables

I recently came across an excellent book: The Art of Readable Code by Dustin Boswell and Trevor Foucher. As soon as I heard about the book, I knew that it would interest me and ordered a copy without delay. For years, I have pushed the message that the #1 priority, when writing code, is readability; the authors and I are on the same wavelength. I am likely to be referring to this book again in this blog, as, on initial reading, although many things are already clear and familiar to me, I still have more to learn and to share …

A common argument for writing complex, hard-to-read code is “efficiency”. Although this is an important matter for many embedded designs, where resources are limited, modern development tools really help to avoid unnecessary obfuscation. Let’s look at an example:

Imagine their is a device, with a control register located at address 0x80000004 and bit 2 indicates that the device has an error. In our software, we reach a point where we wish to execute some code if the device is showing an error and we might do that thus:

if ((*((unsigned *)0x80000004)) & 0x4) != 0)
   ...

This is far from readable and can be improved in obvious ways:

#define MYDEVICE (*((unsigned *)0x80000004))

if ((MYDEVICE & 0x4) != 0) // test error bit
   ...

A further improvement would be to use an intermediate variable to hold the result of the test – an “explaining variable”:

#define MYDEVICE (*((unsigned *)0x80000004))

unsigned DeviceError;

DeviceError = MYDEVICE & 0x4;   // test error bit

if (DeviceError != 0)
   ...

Is that clearer? I believe that it is. However, I can hear a “non-believer” complaining: “That intermediate variable is an unnecessary overhead!” This would be true, except that a modern, optimizing compiler would certainly eliminate the variable if it were used just once. If It were to be used again, the variable would probably be instantiated, but the test would only be performed once, so the overhead is well worthwhile. In any case, it may be allocated to a machine register, which minimizes the impact.

Post Author

Posted May 22nd, 2017, by

Post Tags

, , ,

Post Comments

6 Comments

About The Colin Walls Blog

This blog is a discussion of embedded software matters - news, comment, technical issues and ideas, along with other passing thoughts about anything that happens to be on my mind. The Colin Walls Blog

@colin_walls tweets

Follow colin_walls

Comments

6 comments on this post | ↓ Add Your Own

Commented on 1 June 2017 at 12:50
By David

This is generally good advice. However, it can be improved with better types:

#define MYDEVICE (*((volatile uint32_t *) 0x80000004))
bool DeviceError = MYDEVICE & 0x4; // test error bit
if (DeviceError) …

It is also debatable whether it is a good idea to make #define’s for such register access. Structs are often better, and minimise the namespace pollution of macros. Another option is:

static inline uint32_t mydevice_read_status(void) {
return *((volatile uint32_t *) 0x80000004);
}

static inline bool mydevice_isError(void) {
return mydevice_read_status() & 0x04;
}

Names are good, but macros are not necessarily the best way to get them.

Commented on 1 June 2017 at 14:18
By David

Let me try that again, and see if some blank lines make the post legible.

This is generally good advice. However, it can be improved with better types:

#define MYDEVICE (*((volatile uint32_t *) 0x80000003))

bool DeviceError = MYDEVICE & 0x04; // Test error bit

if (DeviceError) …

It is also debatable whether it is a good idea to make #define’s for such register access. Structs are often better, and minimise the namespace pollution of macros. Another option is:

static inline int32_t mydevice_read_status(void) {

return *((volatile uint32_t *) 0x8000004);

}

static inline bool mydevice_isError(void) {

return mydevice_read_status() & 0x04;

}

Names are good, but macros are not necessarily the best way to get them.

Commented on 1 June 2017 at 16:49
By Colin Walls

No arguments with any of the @David.

Commented on 2 June 2017 at 12:30
By David

Colin, is there a way to make comments here that allow sensible formatting? I would like to comment on a number of your blog articles, but it would be a waste of time if the result is as illegible as the posts I have made here.

Commented on 8 June 2017 at 06:19
By Zeeshan

Very good read. One can make the code slightly more readable by defining bit #2 like #define CR_DEVICEERRORBIT 0x04 thus using CR_DEVICEERRORBIT instead of 0x04.

Commented on 12 June 2017 at 13:41
By David

You certainly can add definitions for bit numbers like that. But be wary of doing that too much. If the bit number is only used in one case, are you really adding anything by having a macro definition? A macro definition with a name does not give the reader (or the compiler) any more information than using the bit number directly along with a comment – and it avoids adding more macros to the global namespace. I would also recommend adding a reference to the appropriate datasheet or reference manual chapter to the comments – it makes it far easier to see find out what the particular bit actually does.

Add Your Comment

Archives