This is a less-badly-written version of the code from the bad example. Notes about what’s improved, as well as what’s still bad, are below. While this is better than the bad example, it’s nowhere near as good as the good example.

Code

 /*
 * This is a program for the FakeRobot. It displays "OK", waits for the 
 * user to press the OK button, displays "Driving", drives forwards one 
 * floor time, stops, displays "Turning", turns right by 90 degrees, 
 * stops, displays "Driving",drives forward two floor tiles, stops, 
 * displays "Turning", turns to the left 180 degrees, stops, displays 
 * "Driving", drives forward one floor tile, and stops. It then displays 
 * "OK" again and waits for the user to press the OK button, at which
 * point the process repeats.
 *
 * Author      : Greg Phillips
 * Version     : 30 September 2014
 */

#include "fakerobot.h"

int main(void) {
	while (1) {
		 // wait for user to press button
		clear_display();
		display("Press OK");
		wait_for_button_down(OK_BUTTON);
		wait_for_button_up(OK_BUTTON);

		// drive forward one floor tile
		clear_display();
		display("Driving");
		zero_encoders();
		set_speed(BOTH_WHEELS, 20); // medium-slow
		while (read_encoder(LEFT_WHEEL) < 1050 // 1050 ticks == one tile
				&& read_encoder(RIGHT_WHEEL) < 1050) { 
			pause_ms(50); 
		}
		set_speed(BOTH_WHEELS, 0); // stop

		// turn right by 90 degrees
		clear_display();
		display("Turning");
		set_speed(LEFT_WHEEL, 10); // slow
		set_speed(RIGHT_WHEEL, -10);
		// 22 in each direction pivots the robot by 90 degrees
		while (read_encoder(LEFT_WHEEL) < 22 &&
				read_encoder(RIGHT_WHEEL) > -22) {
			pause_ms(50);
		}
		set_speed(BOTH_WHEELS, 0);

		// drive forward two floor tiles
		clear_display();
		display("Driving");
		zero_encoders();
		set_speed(BOTH_WHEELS, 20);
		while (read_encoder(LEFT_WHEEL) < 2100 // 2100 ticks == two tiles
		&& read_encoder(RIGHT_WHEEL) < 2100) {
			pause_ms(50);
		}
		set_speed(BOTH_WHEELS, 0);

		// turn left by 180 degrees
		clear_display();
		display("Turning");
		set_speed(LEFT_WHEEL, -10);
		set_speed(RIGHT_WHEEL, 10);
		// 45 in each direction pivots the robot by 180 degrees
		while (read_encoder(LEFT_WHEEL) < -45 &&
				read_encoder(RIGHT_WHEEL) > 45) {
			pause_ms(50);
		}
		set_speed(BOTH_WHEELS, 0);

		// drive forward one tile
		clear_display();
		display("Driving");
		zero_encoders();
		set_speed(BOTH_WHEELS, 20);
		while (read_encoder(LEFT_WHEEL) < 1050
				&& read_encoder(RIGHT_WHEEL) < 1050) {
			pause_ms(50);
		}
	}
}

Clarity

This code has all of the same clarity problems as the bad example. The problems are slightly masked by the addition of in-line comments ➋ and blank lines; however, the code still has no hierarchical structure and is needlessly repetitive.

Coding style

➊ This code does include a file header comment to explain the purpose of the file and indicate authorship and date. However, it provides a lot of detail that would be better expressed in the code itself; this is hard to follow and likely to get out of sync with the code if either are changed.

The code is still riddled with “magic numbers”, e.g., ➌.

Single statement blocks ➍ are now enclosed in braces.

Lines have been reformatted to fit in the maximum 80-character line length ➌.