I need to create an application to act as a BMI calculator, that collects a number of attributes, has matching properties to get and set these attribute values and requests a number of methods to calculate specific data such as patient age, max heart rate, target rates and more.
My question is essentially regarding the usage of methods and to see if this usage is correct or improper.
My method to calculate age looks like the following:
public int Age()
{
DateTime Now = DateTime.Today;
return (Now.Year - DateOfBirth);
}
What I am not 100% sure of is if I can now use that method in another method’s calculation, such as the following:
public int MaxHeartRate()
{
return (220 - Age());
}
I could have used a property to do this as well however, the assignment called for the use of methods to perform the calculation.
Any assistance would be greatly appreciated. I understand the usage between both methods and properties, however I am just unclear on the usage of calling another method inside a method for calculation purposes.
Yes, this is perfectly fine.
However, you might have a bug here:
It’s not clear what
DateOfBirthis, but if it represents the year of birth as it appears to, what happens if I’m born December 31, 2011, andDateTime.Nowis January 1, 2012 12:00:00.000 AM?Also, one point of comment, you’ll often see people write your method as
making it crystal clear that we are invoking the instance method
Age. Readability is one of the single most important features of writing good code.