Testing and Debugging
Status Codes
Named status values make success and failure paths easier to read.
Status Codes
status_codes.c
#include <stdio.h>
int main(void) {
const int STATUS_OK = 0;
const int STATUS_EMPTY = 1;
int count = ;
int status = STATUS_OK;
int average = 0;
if (count == 0) {
status = STATUS_EMPTY;
} else {
average = 30 / count;
}
printf("status=%d average=%d\n", status, average);
return 0;
}
#include <stdio.h>
int main(void) {
const int STATUS_OK = 0;
const int STATUS_EMPTY = 1;
int count = ;
int status = STATUS_OK;
int average = 0;
if (count == 0) {
status = STATUS_EMPTY;
} else {
average = 30 / count;
}
printf("status=%d average=%d\n", status, average);
return 0;
}
#include <stdio.h>
int main(void) {
const int STATUS_OK = 0;
const int STATUS_EMPTY = 1;
int count = ;
int status = STATUS_OK;
int average = 0;
if (count == 0) {
status = STATUS_EMPTY;
} else {
average = 30 / count;
}
printf("status=%d average=%d\n", status, average);
return 0;
}
named codes
Constants give status numbers readable names.
branch result
The final branch can translate the status into the value the caller needs.