This is an example of well-written code that does the same thing as both the bad example and the less-bad example. Notes about why it’s good are below the code.

Code

 /*
 * A FakeRobot program that drives a simple pattern when the user presses 
 * the OK button, then stops. Subsequent presses of OK repeat the pattern.
 * See the main() function for pattern details.
 *
 * Author      : Greg Phillips
 * Version     : 30 September 2014
 */

#include "fakerobot.h"
 #include <stdbool.h>  
#include <stdlib.h>

 #define SLOW 10
#define MEDIUM_SLOW 20
#define TILE_WIDTH 30 			// width of a tile in cm
#define TICKS_PER_CM 35			// when driving straight
#define TICKS_PER_DEGREE 0.25	// assumes wheels counter-rotating
#define LEFT 1
#define RIGHT -1
#define SENSOR_REFRESH 50
#define ALLOW_FINGER_REMOVAL 250

 void wait_for_user();
void drive(int speed, int distance);
void turn(int direction, int angle, int speed);

int main(void) { 
	while (true) {
		wait_for_user();
		drive(MEDIUM_SLOW, TILE_WIDTH);
		turn(RIGHT, 90, SLOW);
		drive(MEDIUM_SLOW, 2 * TILE_WIDTH);
		turn(LEFT, 180, SLOW);
		drive(MEDIUM_SLOW, TILE_WIDTH);
	}
}

 /*
 * Wait for press of OK button. After release, brief pause.
 */
 void wait_for_user() {
	clear_display();
	display("Press OK");
	wait_for_button_down(OK_BUTTON);
	wait_for_button_up(OK_BUTTON);
	pause_ms(ALLOW_FINGER_REMOVAL);
	clear_display();
}

 /*
 * Drive straight, while displaying "Driving".
 * Uses absolute values of distance and encoder readings to account
 * for forward and backward driving.
 *
 * speed: in native motor-control units
 * distance: to drive, in centimeters
 */
 void drive(int speed, int distance) {
	int distance_ticks = abs(distance * TICKS_PER_CM);
	clear_display();
	display("Driving");
	zero_encoders();
	set_speed(BOTH_WHEELS, speed);
	while (abs(read_encoder(LEFT_WHEEL)) < distance_ticks
			&& abs(read_encoder(RIGHT_WHEEL)) < distance_ticks) {
		pause_ms(SENSOR_REFRESH);
	}
	set_speed(BOTH_WHEELS, 0);
	clear_display();
}

 /*
 * Turn while displaying "Turning", with the wheels counter-rotating.
 * Computes absolute values of turn limit and encoder readings to account
 * for all combinations of left and right turns.
 *
 * direction: LEFT (1) or RIGHT (-1)
 * angle: in degrees
 * speed: in native motor-control units
 */
 void turn(int direction, int angle, int speed) {
	int turn_limit = abs(angle * TICKS_PER_DEGREE);
	clear_display();
	display("Turning");
	set_speed(LEFT_WHEEL, -1 * direction * speed);
	set_speed(RIGHT_WHEEL, direction * speed);
	while (abs(read_encoder(LEFT_WHEEL)) < turn_limit
			&& abs(read_encoder(RIGHT_WHEEL)) < turn_limit) {
		pause_ms(SENSOR_REFRESH);
	}
	set_speed(BOTH_WHEELS, 0);
	clear_display();
}

Improvements explained

➊ The file header comment is concise, stating only the high-level purpose of the code. Details have been left to the code itself.

stdbool.h and stdlib.h have been included so that we can use the named constant true and the function abs, which make our code clearer and more robust.

➌ All the numeric constants in the code have been given symbolic names. These names are used everywhere in the code in place of the numbers, which makes the code read more clearly and also means that changes can be made in a single place. There are still some numeric values in the main body of the code, e.g., 90 and 180, but these have well-understood meanings and are unlikely to change. Some of the numeric constants require explanation, which has been provided as in-line comments.

The three key actions of the program (waiting for the user, driving, and turning) have been modularized as individual functions. These are declared before the main ➍ and defined after the main ➏. This removes the repetition that was present in the previous versions. Each function can now be tested independently, and any required change can be made in a single place rather than repeatedly.

The main function has been re-written to use the three defined functions ➎. Note that the main reads like an outline. There is no need to explain what it does: function is obvious because of the clear structure and well-chosen names.

Each of the defined functions has a function comment that clearly explains what the function does, any assumptions it makes, what it requires as input parameters, and what value (if any) it returns ➐.