D.R.Y. using two “if” statements in C

Source
“Don’t repeat yourself” or D.R.Y.

Formulated by Andy Hunt and Dave Thomas, the D.R.Y. is “aimed at reducing repetition of information of all kinds.”

As I am learning C, I fortunately came across one example of D.R.Y. in learning how to write if statements.

/* This program asks the user for their birth year and calculates how old they
will be in the current year. (it also checks to make sure a future year has not been entered.)
It then tells the user if they were born in a leap year. */
#include <stdio.h>
#define CURRENTYEAR 2016
int main()
{
int yearBorn, age;
printf("What year were you born?\n");
scanf(" %d", &yearBorn);
// This if statement can do some data validation, making sure
// the year makes sense
// The statements will only execute if the year is after the
// current year
if (yearBorn > CURRENTYEAR)
{
printf("Really? You haven't been born yet?\n");
printf("Want to try again with a different year?\n");
printf("What year were you born?\n");
scanf(" %d", &yearBorn);
}
age = CURRENTYEAR - yearBorn;
printf("\nSo this year you will turn %d on your birthday!\n", age);
// The second if statement uses the modulus operator to test if
// the year of birth was a Leap Year. Again, only if it is will
// the code execute
if ((yearBorn % 4) == 0)
{
printf("\nYou were born in a Leap Year---cool!\n");
}
return 0;
}

Instead of defining the variable CURRENTYEAR globally (with the definition 2016), I could have instead of written 2016 in all instances of the current variable CURRENTYEAR locally (within the main function). It would have looked like this:

// Example program #1 from Chapter 11 of Absolute Beginner's Guide
// to C, 3rd Edition
// File Chapter11ex1.c
/* This program asks the user for their birth year and calculates how old they
will be in the current year. (it also checks to make sure a future year has not been entered.)
It then tells the user if they were born in a leap year. */
#include <stdio.h>
int main()
{
int yearBorn, age;
printf("What year were you born?\n");
scanf(" %d", &yearBorn);
// This if statement can do some data validation, making sure
// the year makes sense
// The statements will only execute if the year is after the
// current year
if (yearBorn > 2016)
{
printf("Really? You haven't been born yet?\n");
printf("Want to try again with a different year?\n");
printf("What year were you born?\n");
scanf(" %d", &yearBorn);
}
age = 2016 - yearBorn;
printf("\nSo this year you will turn %d on your birthday!\n", age);
// The second if statement uses the modulus operator to test if
// the year of birth was a Leap Year. Again, only if it is will
// the code execute
if ((yearBorn % 4) == 0)
{
printf("\nYou were born in a Leap Year---cool!\n");
}
return 0;
}

What are the two problems of doing it this way (and not abiding by the D.R.Y. principle)?

  • If someone uses this program next year (in this case: 2017), rather than changing just the CURRENTYEAR (variable) definition (2016) globally, I would have to change every instance of the variable (2016) locally, two in this case. I would have to do double the work for the same result.
  • When another programmer (or my future self) works on maintaining this software in the future, they are going to have to figure out what 2016 means, which would be easy enough for this small program. But in larger programs, figuring out what the (numerical) values are, and how they fit into the functioning of the program without proper contextual syntax, would be like finding a needle in a haystack.

Work smarter, not harder.