Constructive Criticism
Constructive Criticism
- Subject: Constructive Criticism
- From: Mick Walker <email@hidden>
- Date: Tue, 06 Oct 2009 22:26:52 +0100
Hi everyone,
I am currently reading through Learn Objective-C on the Mac (M
Dalrymple & S Knaster). While working through the provided examples, I
want to back up what I am learning by attempting to put into practice
what is being demonstrated to me.
To this end, I would like to post some of my own code, to get
feedback, basically to find out if I am doing things correctly, and if
not, why not; especially in the realms of memory management (I think I
understand the concept, but I want to reaffirm it).
The first program I tried to write, is a simple console program which
uses math to calculate the date of easter for a given year, my code is
below:
--- Easter.h ---
#import <Cocoa/Cocoa.h>
@interface Easter : NSObject {
int Year;
}
- (id) init;
- (void) setYear: (int) year;
- (void) CalculateYear;
@end
--- Easter.m ---
#import "Easter.h"
@implementation Easter
- (id) init {
if(self == [super init]){
Year = 0;
orignalYear = 0;
}
return (self);
}
-(void) setYear: (int) year {
Year = year;
orignalYear = year;
}
- (void) CalculateYear {
if(Year == 0){
NSLog(@"Error: No Year Specified");
}
int day;
int month;
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 {
[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 release];
[pool drain];
return 0;
}
As I mentioned before, all criticism is welcome. If what I have wrote
is dire, feel free to flame me.
Regards
Mick
_______________________________________________
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