1

The following examples are from the book Programming in the Key of C#.

The first iteration of the program is the typical C way to do it, and the next reincarnation is more object oriented. The program is a simple example of calculating which day of the year a certain event occurred (december 31 is 365 or 366 if it's a leap year).

using System;

class StructureAndMethodsTwo
{
    static void Main()
    {
        Date dateMoonWalk = new Date();

        dateMoonWalk.iYear = 1969;
        dateMoonWalk.iMonth = 7;
        dateMoonWalk.iDay = 20;

        Console.WriteLine("Moon walk: {0}/{1}/{2} Day of Year: {3}", 
            dateMoonWalk.iMonth, dateMoonWalk.iDay, dateMoonWalk.iYear,
            Date.DayOfYear(dateMoonWalk));
    }
}

struct Date
{
    public int iYear;
    public int iMonth;
    public int iDay;

    public static bool IsLeapYear(int iYear)
    {
        return iYear % 4 == 0 && (iYear % 100 != 0 || iYear % 400 == 0);
    }

    static int[] aiCumulativeDays = { 0, 31, 59, 90, 120, 151,
                                        181, 212, 243, 273, 304, 334 };

    public static int DayOfYear(Date dateParam)
    {
        return aiCumulativeDays[dateParam.iMonth - 1] + dateParam.iDay +
            (dateParam.iMonth > 2 && IsLeapYear(dateParam.iYear) ? 1 : 0);
    }
}

The next version of the program is identical except for the DayOfYear method which turns into

public int DayOfYear()
{
return aiCumulativeDays[iMonth -1] + iDay+ (iMonth > 2 && IsLeapYear(iYear) ? 1:0);
}

What's exactly going on in the second version that makes it more OOP friendly than the first? Is an object of type Date being created by the method DayOfYear in the first iteration? I know that the instance version of the method has direct access to the fields of the structure, but I'm not aware of the distinct advantages of it.

3 Answers 3

1

In example two you are using the internal variables iYear, iMonth and iDay of the Date struct. The first example uses another copy of a Date object which you are passing to the DayOfYearFunction which is not needed.

Edit:

In the first example you use an instance of your Date struct and pass it as a parameter to the DayOfYear function, the instance of Date (dateParam) is allocated on the stack which would use more memory and require an additional garbage collection call from the CLR when it needs to be cleaned up.

Performance issues: IMO, although slightly more memory is used, hardly any performance issues will occur due to the nature of the program. It is just a redundant parameter.

Sign up to request clarification or add additional context in comments.

3 Comments

could you explain (if it's within the realm of reasonable understanding) what's going on when in the first example the method is using another copy of the Date object? Would this involve using more memory, performance issues, or more likely both?
@wootscootinboogie edited my answer to clarify. Hope this helps
thanks. i've been learning c# for about two weeks now and instead of glossing over the inner workings and just getting things to work as quickly as possible as i have done in the past, i'm trying to do things right and get all my ducks in a row before i precede. thanks :)
1

In the second version the object itself is supplying all the information, not the date parameter in a static context. If the method is using the object's own data instead of being 'told' by the parameters about what it is then it's more object oriented.

Comments

1

Day of year is not creating a Date in the first version. It is simply evaluating the parameter. The second version is more OO because it operates on an instance of your class rather than a static method call. This typically keeps things more organized and is more OO.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.