Re: Constructive Criticism
Re: Constructive Criticism
- Subject: Re: Constructive Criticism
- From: Bill Bumgarner <email@hidden>
- Date: Tue, 06 Oct 2009 14:35:29 -0700
On Oct 6, 2009, at 2:26 PM, Mick Walker wrote:
> #import <Cocoa/Cocoa.h>
> @interface Easter : NSObject {
> int Year;
int year;
> }
> - (id) init;
>
> - (void) setYear: (int) year;
@property int year;
>
> - (void) CalculateYear;
- (void) calculateYear;
>
> @end
>
>
> --- Easter.m ---
> #import "Easter.h"
>
>
> @implementation Easter
@synthesize year;
>
> - (id) init {
> if(self == [super init]){
> Year = 0;
> orignalYear = 0;
> }
> return (self);
> }
>
-- delete --
> -(void) setYear: (int) year {
> Year = year;
> orignalYear = year;
> }
-- end delete --
>
> - (void) CalculateYear {
- (void) calculateYear {
> if(Year == 0){
if (self.year == 0) {
> NSLog(@"Error: No Year Specified");
@throw ... some kind of fatal exception ...
Or, if it is recoverable, switch this to using the (NSError **) pattern pervasive to Cocoa
> }
> int day;
> int month;
int localYear = self.year;
s/Year/localYear/ throughout the following chunk o' code.
>
> int g = Year % 19;
> int c = Year / 100;
> int h = h = (c - (int)(c / 4) - (int)((8 * c + 13) / 25) + 19 * g + 15) % 30;
> int i = h - (int)(h / 28) * (1 - (int)(h / 28) * (int)(29 / (h + 1)) * (int)((21 - g) / 11));
>
> day = i - ((Year + (int)(Year / 4) + i + 2 - c + (int)(c / 4)) % 7) + 28;
> month = 3;
>
> if (day > 31)
> {
> month++;
> day -= 31;
> }
> NSLog(@"The date of Easter sunday in the year %d is %d/%d/%d", Year, day, month,Year);
> }
>
> - (void) dealloc {
if there is nothing to release or do in -dealloc, skip the implementation entirely.
>
> [super dealloc];
> }
>
> @end
>
> --- Easter Calculator.m ---
> #import <Foundation/Foundation.h>
> #import "Easter.h"
>
>
> int main (int argc, const char * argv[]) {
> NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
>
> Easter *myEaster = [[Easter alloc]init];
>
> [myEaster setYear:2010];
> [myEaster CalculateYear];
[myEaster calculateYear];
> [myEaster release];
> [pool drain];
> return 0;
> }
I'd also consider renaming calculateYear to something like -easterSunday. Something like:
- (NSDate *) easterSunday;
Then calculate and return the date object, to be processed, managed, or output by the caller. It makes the Easter class slightly thinner and considerably more reusable.
b.bum
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden