Re: NSObject Exercise comments?
Re: NSObject Exercise comments?
- Subject: Re: NSObject Exercise comments?
- From: Andreas Mayer <email@hidden>
- Date: Sat, 10 Dec 2005 08:37:19 +0100
Am 10.12.2005 um 07:30 Uhr schrieb Lee Cullens:
I would greatly appreciate any comments you might offer so that I
might nip any misconceptions and bad habits in the bud.
Read Apple's Cocoa Naming Conventions:
http://developer.apple.com/documentation/Cocoa/Conceptual/
CodingGuidelines/CodingGuidelines.html
@interface Fraction: NSObject
Since Objective-C has no namespaces you should add a prefix to your
class names.
+(Fraction *) newFractionWith: (int) n: (int) d;
Don't abbreviate variable names. Use named parameters.
+ (Fraction *)fractionWithNumerator:(int)numerator denominator:(int)
denominator;
-(Fraction *) initFractionWith: (int) n: (int) d;
Initializers should return an id.
- (id)initWithNumerator:(int)numerator denominator:(int)denominator;
+(void) incrRejectCnt;
Don't abbreviate.
+ (void)incrementRejectCount;
// getters
+(int) getRejectCnt;
Don't prefix getters.
+ (int)rejectCount;
-(int) getNumerator;
- (int)numerator;
-(int) getDenominator;
- (int)demoninator;
// utility
-(NSMutableString *) formatFraction;
'description' is a method that most objects understand; it seems
reasonable to use this method as a replacement.
- (NSString *)description;
-(double) convertToDec;
Seems to be an accessor. The method name should make clear what the
result is.
- (double)decimalFraction;
-(Fraction *) reduceFraction;
Not sure about this one. Maybe:
- (Fraction *)reducedFraction;
-(Fraction *) addFraction: (Fraction *) f;
- (Fraction *)fractionByAdding:(Fraction *)fraction;
(see NSDecimalNumber as an example)
-(Fraction *) subtractFraction: (Fraction *) f;
-(Fraction *) multiplyFraction: (Fraction *) f;
-(Fraction *) divideByFraction: (Fraction *) f;
Enough with naming conventions. :-)
// ------------------------------------------------------
// initialize a new fraction instance for newFractionWith::
-(Fraction *) initFractionWith: (int) n: (int) d
{
if (numerator = n)
You need to call [super init] in your initializer:
- (id)initSomething
{
if (self = [super init]) {
// do some setup
}
return self;
}
+(Fraction *) newFractionWith: (int) n: (int) d
{
Fraction *newFinit;
Fraction *newF = [[Fraction alloc] init];
if (newF)
{
if (newFinit = [newF initFractionWith: n : d])
return [newFinit autorelease];
+ (XXFraction *)fractionWithNumerator:(int)numerator denominator:(int)
denominator
{
XXFraction *result = [[[XXFraction alloc]
initWithNumerator:numerator denominator:denominator] autorelease];
if (!result) {
[XXFraction increaserejectCount];
}
return result;
}
-(Fraction *) reduceFraction
{
if (numerator)
{
[...]
{
[self release];
return nil;
}
This seems wrong. You are supposed to return a *new* instance of
Fraction here. Releasing the object will lead to memory management
problems.
HTH,
Andreas
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden