Example 12.8.1. A Bottom-Up Design.
A bottom-up design for our date program would start by trying to identify small bits of work that we think will need to get done. Maybe we donβt initially think of turning each date into a number of days since 0/0/0. But we do realize that we will need to break a string like
"3/4/2023"
into its component parts. So we might start with:-
β Get the month as an integer. Inputs: a string like
"3/4/2023"
; Output: an integer representing the month (3 in this case).int getMonth(string date)
-
β Get the day as an integer. Inputs: a string like
"3/4/2023"
; Output: an integer representing the day (4 in this case).int getDay(string date)
-
β Get the year as an integer. Inputs: a string like
"3/4/2023"
; Output: an integer representing the year (2023 in this case).int getYear(string date)
Once we have those functions, we maybe get a sense of what we could do with them. It feels pretty clear that we are going to need to figure out how many days are in a given month. So we might add a function to handle that:
-
Get the month as an integer. Inputs: a string like
"3/4/2023"
; Output: an integer representing the month (3 in this case).int getMonth(string date)
-
Get the day as an integer. Inputs: a string like
"3/4/2023"
; Output: an integer representing the day (4 in this case).int getDay(string date)
-
Get the year as an integer. Inputs: a string like
"3/4/2023"
; Output: an integer representing the year (2023 in this case).int getYear(string date)
-
β Get the number of days in the given month. Inputs: an integer month; Output: an integer representing the number of days in the given month.
int daysInMonth(int month)
Then, maybe we realize it would be good to combine the month/day into a single total number of days. So 1/3 would be 3 days, 2/1 would be 32 days (31 for January plus one in February), etc... That might lead us to adding a
totalDays
function that takes the month and day. It is seems like that function will rely on daysInMonth
, so we will consider that older function a building block for the new function:-
Get the month as an integer. Inputs: a string like
"3/4/2023"
; Output: an integer representing the month (3 in this case).int getMonth(string date)
-
Get the day as an integer. Inputs: a string like
"3/4/2023"
; Output: an integer representing the day (4 in this case).int getDay(string date)
-
Get the year as an integer. Inputs: a string like
"3/4/2023"
; Output: an integer representing the year (2023 in this case).int getYear(string date)
-
β Get the total number of days represented by a given month/day combo. Inputs: an integer month, an integer day; Output: an integer representing the total number of days.
int totalDays(int month, int day)
-
Get the number of days in the given month. Inputs: an integer month; Output: an integer representing the number of days in the given month.
int daysInMonth(int month)
-
At this point we will stop the process. Another step might mix the year value in with the total from the month/day. And eventually we would need to get the user input.