开发者

How To Optimize This Objective-C Code

I am teaching myself Objective-C and wrote this short little code to practice.

It works fine, but I don't think this is the right way to code with object-oriented programming.

How can I change the code so that I have a separate className.h file. The interface, implementation, and main should all be separate instead of jammed 开发者_如何学Goin one code. Any suggestions? Thanks

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

 int courses, x;
 float grade, y;

 NSLog(@"Welcome to GPA Calculator \n How many courses are you taking?");
    scanf("%i",&courses);
 NSLog(@"You are taking %i courses", courses);

 while (x<courses) {
  NSLog(@"Please enter your grade for that course using number scale");
  scanf("%f",&grade);
  y=y+grade;
  x++;
    }
 NSLog(@"Your GPA is %f",y/courses);

 [pool drain];
    return 0;
    }


The main purpose of refactoring code into classes and modules is to improve readability and maintainability. A trivial program like this would be made substantially less readable by such a refactoring, so it is difficult to offer meaningful advice on how it should be split up. The best answer I can offer is that it shouldn't.

Tackle some bigger projects, and then you can learn the use of modularisation techniques in their proper context.

"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction." — Albert Einstein

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜