Is there a cleaner way to write this objective-c code?

I am new to objective-c and programming in general. I’m a paramedic, and I decided to learn how to program objectively. I have some experience with c, so this program is encoded in this way. I was wondering if there was a more efficient way to code this with objective-c? Thanks. (The program compiles without errors, so if there is a syntax error, it is probably because I'm new to escaping characters on boards inside blocks of code)

#import &ltFoundation/Foundation.h> void calcDiagnosis (float pHInput, int paCO2Input, int hCO3Input); int main (int argc, const char * argv[]){ int i; int repeat; i = 0; for(i = 0; i &lt 3; i++){ //Initialize lab value variables float pH; int paCO2; int hCO3; //Introduction NSLog(@"Welcome to the ABG Lab Value Interpreter v1.0\n"); NSLog(@"Please enter the necessary values.\n"); //Gather the necessary values NSLog(@"Enter the pH value:"); scanf("%f", &pH); NSLog(@"Enter the PaCO2 value:"); scanf("%i", &paCO2); NSLog(@"Enter the HCO3 value:"); scanf("%i", &hCO3); calcDiagnosis (pH, paCO2, hCO3); //Control Loop NSLog(@"Again?\n 1: Yes\n 2: No"); scanf("%i", &repeat); switch (repeat){ case 1: i = 0; break; case 2: i = 3; break; } } return 0; } void calcDiagnosis (float pHInput, int paCO2Input, int hCO3Input) { //Transfer the arguments to new variables float pH = pHInput; int paCO2 = paCO2Input; int hCO3 = hCO3Input; ////////////////////////////////// //Diagnose Respiratory Acidosis// //////////////////////////////// //Acute if ((pH &lt 7.35) && (paCO2 > 45) && (hCO3 >=22 && hCO3 &lt=26)) { NSLog(@"Acute Respiratory Acidosis"); } //Partially Compensated if ((pH &lt 7.35) && (paCO2 > 45) && (hCO3 >26)) { NSLog(@"Partially Compensated Respiratory Acidosis"); } //Compensated if ((pH >= 7.35 && pH &lt= 7.45) && (paCO2 > 45) && (hCO3 >26)) { NSLog(@"Compensated Respiratory Acidosis"); } /////////////////////////////////// //Diagnose Respiratory Alkalosis// ///////////////////////////////// //Acute if ((pH > 7.45) && (paCO2 &lt 35) && (hCO3 >=22 && hCO3 &lt=26)) { NSLog(@"Acute Respiratory Alkalosis"); } //Partially Compensated if ((pH > 7.45) && (paCO2 &lt 35) && (hCO3 &lt22)) { NSLog(@"Partially Compensated Respiratory Alkalosis"); } //Compensated if ((pH >= 7.35 && pH &lt= 7.45) && (paCO2 &lt 35) && (hCO3 &lt22)) { NSLog(@"Compensated Respiratory Alkalosis"); } ////////////////////////////////// //Diagnose Metabolic Acidosis//// //////////////////////////////// //Acute if ((pH &lt 7.35) && (paCO2 >= 35 && paCO2 &lt= 45) && (hCO3 &lt22)) { NSLog(@"Acute Metabolic Acidosis"); } //Partially Compensated if ((pH &lt 7.35) && (paCO2 &lt 35) && (hCO3 >22)) { NSLog(@"Partially Compensated Metabolic Acidosis"); } //Compensated if ((pH >= 7.35 && pH &lt= 7.45) && (paCO2 &lt 35) && (hCO3 &lt22)) { NSLog(@"Compensated Metabolic Acidosis"); } ////////////////////////////////// //Diagnose Metabolic Alkalosis/// //////////////////////////////// //Acute if ((pH > 7.45) && (paCO2 >= 35 && paCO2 &lt= 45) && (hCO3 >26)) { NSLog(@"Acute Metabolic Alkalosis"); } //Partially Compensated if ((pH > 7.45) && (paCO2 > 45) && (hCO3 >26)) { NSLog(@"Partially Compensated Metabolic Alkalosis"); } //Compensated if ((pH >= 7.35 && pH &lt= 7.45) && (paCO2 > 45) && (hCO3 >26)) { NSLog(@"Compensated Metabolic Alkalosis"); } ////////////////////// //Diagnosis Normal/// //////////////////// if ((pH >= 7.35 && pH &lt= 7.45) && (paCO2 >= 35 && paCO2 &lt= 45) && (hCO3 >= 22 && hCO3 &lt= 26)) { NSLog(@"Normal Values"); } return; } 
+4
source share
3 answers

This can be a tough question. As you become more experienced, you will become more comfortable with more advanced concepts. The problem you are working with is actually quite complex and makes a great learning tool.

The biggest problem is that your current solution does not use object orientation, which makes it difficult to support and / or expand in the future.

Ultimately, the question of the optimal structure of the code can have many answers, and you may not know what is better than the next, until you add more functionality to your program.

I reprogrammed your program, in which I feel that this is a solid final game structure (as opposed to shooting with a shorter intermediate step). Unfortunately, this can be a little leap when you are just starting.

There are two advanced concepts in this solution: object-oriented programming and a selector. Selectors are an incredibly powerful tool that lets you pass the actual instructions on your program using variables. In your case, you can store if statements in your diagnostic objects.

In any case, please feel free to ask any questions about the following:

Vitals.h

 #import <Foundation/Foundation.h> @interface Vitals : NSObject { float _pH; int _paCO2; int _hCO3; } - (id) initWithPH:(float)pH paCO2:(int)paCO2 hCO3:(int)hCO3; - (float) pH; - (int) paCO2; - (int) hCO3; @end 

Vitals.m

 #import "Vitals.h" @implementation Vitals - (id) initWithPH:(float)pH paCO2:(int)paCO2 hCO3:(int)hCO3 { if (self = [super init]) { _pH = pH; _paCO2 = paCO2; _hCO3 = hCO3; } return self; } - (float) pH {return _pH;} - (int) paCO2 {return _paCO2;} - (int) hCO3 {return _hCO3;} @end 

Diagnosis.h

 #import <Foundation/Foundation.h> @class Vitals; @interface Diagnosis : NSObject { NSString* _name; id _delegate; SEL _test; } - (id) initWithName:(NSString*)name delegate:(id)delegate test:(SEL)test; - (NSString*) name; - (BOOL) test:(Vitals*)vitals; @end 

Diagnosis.m

 #import "Diagnosis.h" @implementation Diagnosis - (id) initWithName:(NSString*)name delegate:(id)delegate test:(SEL)test { if (self = [super init]) { _name = [name retain]; _delegate = delegate; _test = test; } return self; } - (void) dealloc { [_name release]; [super dealloc]; } - (NSString*) name {return _name;} - (BOOL) test:(Vitals*)vitals { return [(NSNumber*)[_delegate performSelector:_test withObject:vitals] boolValue]; } @end 

Doctor.h

 #import <Foundation/Foundation.h> @class Vitals; @class Diagnosis; @interface Doctor : NSObject { NSMutableArray* _diagnoses; } - (void) learnDiagnosis:(Diagnosis*)diagnosis; - (Diagnosis*) diagnose:(Vitals*)vitals; @end 

Doctor.m

 #import "Diagnosis.h" #import "Doctor.h" @implementation Doctor - (id) init { if (self = [super init]) { _diagnoses = [[NSMutableArray alloc] init]; } return self; } - (void) dealloc { [_diagnoses release]; [super dealloc]; } - (void) learnDiagnosis:(Diagnosis*)diagnosis { [_diagnoses addObject:diagnosis]; } - (Diagnosis*) diagnose:(Vitals*)vitals { for (Diagnosis* diagnosis in _diagnoses) { if ([diagnosis test:vitals]) return diagnosis; } return 0; } @end 

Differential.h

 #import <Foundation/Foundation.h> @interface Differential : NSObject {} - (void) teach:(Doctor*)doctor; @end 

Differential.m

 #import "Vitals.h" #import "Diagnosis.h" #import "Doctor.h" #import "Differential.h" @implementation Differential - (NSNumber*) acuteRespiratoryAcidosis:(Vitals*)vitals { return [NSNumber numberWithBool:(([vitals pH] < 7.35) && ([vitals paCO2] > 45) && ([vitals hCO3] >=22 && [vitals hCO3] <=26))]; } - (NSNumber*) partiallyCompensatedResporatoryAcidosis:(Vitals*)vitals { return [NSNumber numberWithBool:(([vitals pH] < 7.35) && ([vitals paCO2] > 45) && ([vitals hCO3] >26))]; } - (void) teach:(Doctor*)doctor { Diagnosis* diagnosis; diagnosis = [[Diagnosis alloc] initWithName:@"Acute Respiratory Acidosis" delegate:self test:@selector(acuteRespiratoryAcidosis:)]; [doctor learnDiagnosis:diagnosis]; [diagnosis release]; diagnosis = [[Diagnosis alloc] initWithName:@"Partially Compensated Respiratory Acidosis" delegate:self test:@selector(partiallyCompensatedResporatoryAcidosis:)]; [doctor learnDiagnosis:diagnosis]; [diagnosis release]; } @end 

Sandbox.h

 #import <Foundation/Foundation.h> #import "Vitals.h" #import "Diagnosis.h" #import "Doctor.h" #import "Differential.h" void run () { float pH=7.2; int paCO2=47; int hCO3=25; Doctor* doctor = [[Doctor alloc] init]; Differential* differential = [[Differential alloc] init]; [differential teach:doctor]; Vitals* vitals = [[Vitals alloc] initWithPH:pH paCO2:paCO2 hCO3:hCO3]; Diagnosis* diagnosis = [doctor diagnose:vitals]; NSLog(@"%@",[diagnosis name]); [vitals release]; [differential release]; [doctor release]; } 
+6
source

While there are a few problems with the code you posted, the biggest problem is to use a for loop, where it would be more natural to use a while . for usually used for iteration (e.g. reading or writing for each element in an array). while , as a rule, is repeated multiple (but indefinite) number of times. You can perform several different methods, but a simple modification will be as follows:

 int main (int argc, const char * argv[]){ int menu_input = 0; while(menu_input != 2){ //Initialize lab value variables float pH; int paCO2; int hCO3; //Introduction NSLog(@"Welcome to the ABG Lab Value Interpreter v1.0\n"); NSLog(@"Please enter the necessary values.\n"); //Gather the necessary values NSLog(@"Enter the pH value:"); scanf("%f", &pH); NSLog(@"Enter the PaCO2 value:"); scanf("%i", &paCO2); NSLog(@"Enter the HCO3 value:"); scanf("%i", &hCO3); calcDiagnosis (pH, paCO2, hCO3); //Control Loop NSLog(@"Again?\n 1: Yes\n 2: No"); scanf("%i", &menu_input); } return 0; 

}

As mentioned in your post, it will be useful for you to do some basic input validation, if used in a real environment.

0
source

Are you learning Objective-C for writing programs for Mac or iPhone? I will take it because it is perhaps the main reason why people learn this. If you haven’t already done so, you should check out the Apple Developer website, where they have many useful guides, etc. I think you should try to do this in a graphical application, because you will use more Objective-C and Cocoa in this way. You really wrote a C program other than NSLog () (and NSStrings inside them). Here is a good tutorial.

0
source

Source: https://habr.com/ru/post/1332580/


All Articles