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

Jimmy Thong
3 min readSep 16, 2016
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…

--

--

Jimmy Thong

Makerspace Teacher, Code Coach, Amateur Home Cook, Writer