开发者

Correct way to call a method inside a method

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 usag开发者_Python百科e of calling another method inside a method for calculation purposes.


Yes, this is perfectly fine.

However, you might have a bug here:

public int Age() {
    DateTime Now = DateTime.Today;
    return (Now.Year - DateOfBirth);
}

It's not clear what DateOfBirth is, but if it represents the year of birth as it appears to, what happens if I'm born December 31, 2011, and DateTime.Now is January 1, 2012 12:00:00.000 AM?

Also, one point of comment, you'll often see people write your method as

public int MaxHeartRate() {
    return (220 - this.Age());
}

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.


straight answer is yes, you can.


Yes, your usage is correct. This is pretty standard actually, as you can only call methods from within other methods. The only thing I would double check on is:

return (Now.Year - DateOfBirth); 

I don't know if this will return an int or not due to the DateofBirth being a datetime. You might want to just check over that.


Yes, you can (and should!) use the Age method in places where you're using that calculated value to calculate others.

Your logic for calculating age is not correct, though. DateTime.Today returns a DateTime object representing (as of this writing) September 16, 2011 at 12:00:00 AM. DateTime.Today.Year would therefore return 2011. If I was born in February of 1984, and assuming DateOfBirth is the year I was born, this method will always return 27 when you run it in 2011, until you hit January 1, 2012 at which point it will start returning 28.

Don't just compare years--compare the actual dates and then pull out the Year part of the TimeSpan difference between them.


Yes, what you are doing is fine, assuming that DateOfBirth returns what you want. The function you have defined will always return an int since that is the return type you have defined. This means you can use the function Age() in any place that expects an integer.

A couple of pointers to help you start out:

  • You mentioned this so I think you get it, but in general this might be a candidate for storing in a variable. The way you have it now every time the Age() function is called a calculation will be performed. Sometimes this is what you want. It is reasonable here since your age could change, but that is only once a year. You can imagine a really complicated method that does a lot of heavy lifting. You would want to cache the output of that function in a variable.
  • You have defined a variable Now that just gets DateTime.Today. Then you use that to find the year. You can save a step by just putting DateTime.Today.Year instead of making a new variable. A minor point.


EDIT: My bad! it's been a long week! As mentioned below in the comments, you'd only way this as static if it takes a parameter.

The problem you will have here is that the Age() function is not static, as a result you'd need to create and instance, which for that type of method is not what you want.

If you define Age() as :

public static int Age() { }

Then you can use it like you have in the MaxHearRate() method

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜