下面为实现同一个功能(通过手机号码获取验证码),用两种编码思路分别进行编码。
第一种:递进化的思考方式编码举例:
//获取验证码 -(IBAction)btnGetVerificationCodeOnClicked:(id)sender { //判断网络 if(![self getNetConnectState]){ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"没有连接到网络!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil]; [alert show]; [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(dismissAlert:) userInfo:[NSDictionary dictionaryWithObjectsAndKeys:alert,@"alert", nil] repeats:NO]; [alert release]; } else { if ([txtPhone.text isEqualToString:@""]) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入手机号码!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil]; [alert show]; [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(dismissAlert:) userInfo:[NSDictionary dictionaryWithObjectsAndKeys:alert,@"alert", nil] repeats:NO]; [alert release]; } else { //通过手机号获取验证码的核心业务代码 NSString *soapMessage = [NSString stringWithFormat: @"<v:Envelope xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\" xmlns:c=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:v=\"http://schemas.xmlsoap.org/soap/envelope/\"><v:Header /><v:Body><getSmsVerificationCode xmlns=\"http://10.72.32.119:9081/MobileInfo/services/Info/\" id=\"o0\" c:root=\"1\"><mobile i:type=\"d:string\">%@</mobile></getSmsVerificationCode></v:Body></v:Envelope>",txtPhone.text ]; //请求发送到的路径 NSURL *url = [NSURL URLWithString:@"http://moa.sdunicom.com:8008/MobileInfo/services/Info?wsdl"]; ASIHTTPRequest *theRequest = [ASIHTTPRequest requestWithURL:url]; NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]]; [theRequest addRequestHeader:@"Content-Type" value:@"text/xml; charset=utf-8"]; [theRequest addRequestHeader: @"SOAPAction" value:@"http://moa.sdunicom.com:8008/MobileInfo/services/Info?wsdl/getSmsVerificationCode"]; [theRequest addRequestHeader:@"Content-Length" value:msgLength]; [theRequest setRequestMethod:@"POST"]; [theRequest appendPostData: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; [theRequest setDefaultResponseEncoding:NSUTF8StringEncoding]; [theRequest startSynchronous]; //所谓获取验证码,就是组建一个SOAP消息啊 NSError *error = [theRequest error]; if (!error) { NSData *data = [theRequest responseData]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"验证码发送成功!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil]; [alert show]; [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(dismissAlert:) userInfo:[NSDictionary dictionaryWithObjectsAndKeys:alert,@"alert", nil] repeats:NO]; [alert release]; NSString *theXML = [[NSString alloc] initWithBytes: [data mutableBytes] length:[data length] encoding:NSUTF8StringEncoding]; NSLog(@"%@",theXML); NSXMLParser *parser = [[NSXMLParser alloc] initWithData: data]; [parser setShouldProcessNamespaces:NO]; [parser setShouldReportNamespacePrefixes:NO]; [parser setShouldResolveExternalEntities:NO]; parser.delegate = self; [parser parse]; [parser release]; [theXML release]; } else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"验证码获取失败,请与管理员联系!" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:nil]; [alert show]; [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(dismissAlert:) userInfo:[NSDictionary dictionaryWithObjectsAndKeys:alert,@"alert", nil] repeats:NO]; [alert release]; } } } }
第二种:结构化的思考方式来编码举例:
//获取验证码 -(IBAction)btnGetVerificationCodeOnClicked:(id)sender { // 下列条件均具备,则通过指定手机号获得注册码 if( [self getNetConnectState] && ![txtPhone.text isEqualToString:@""] ){ //soap消息内容 NSString *soapMessage = [NSString stringWithFormat: @"<v:Envelope xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\" xmlns:c=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:v=\"http://schemas.xmlsoap.org/soap/envelope/\"><v:Header /><v:Body><getSmsVerificationCode xmlns=\"http://10.72.32.119:9081/MobileInfo/services/Info/\" id=\"o0\" c:root=\"1\"><mobile i:type=\"d:string\">%@</mobile></getSmsVerificationCode></v:Body></v:Envelope>",txtPhone.text ]; //请求发送到的路径 NSURL *url = [NSURL URLWithString:@"http://moa.sdunicom.com:8008/MobileInfo/services/Info?wsdl"]; ASIHTTPRequest *theRequest = [ASIHTTPRequest requestWithURL:url]; NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]]; [theRequest addRequestHeader:@"Content-Type" value:@"text/xml; charset=utf-8"]; [theRequest addRequestHeader: @"SOAPAction" value:@"http://moa.sdunicom.com:8008/MobileInfo/services/Info?wsdl/getSmsVerificationCode"]; [theRequest addRequestHeader:@"Content-Length" value:msgLength]; [theRequest setRequestMethod:@"POST"]; [theRequest appendPostData: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; [theRequest setDefaultResponseEncoding:NSUTF8StringEncoding]; [theRequest startSynchronous]; //所谓获取验证码,就是组建一个SOAP消息啊 NSError *error = [theRequest error]; if (!error) { NSData *data = [theRequest responseData]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"验证码发送成功!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil]; [alert show]; [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(dismissAlert:) userInfo:[NSDictionary dictionaryWithObjectsAndKeys:alert,@"alert", nil] repeats:NO]; [alert release]; NSString *theXML = [[NSString alloc] initWithBytes: [data mutableBytes] length:[data length] encoding:NSUTF8StringEncoding]; NSLog(@"%@",theXML); NSXMLParser *parser = [[NSXMLParser alloc] initWithData: data]; [parser setShouldProcessNamespaces:NO]; [parser setShouldReportNamespacePrefixes:NO]; [parser setShouldResolveExternalEntities:NO]; parser.delegate = self; [parser parse]; [parser release]; [theXML release]; } else{ // 此处相当于做了异常处理 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"验证码获取失败,请与管理员联系!" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:nil]; [alert show]; [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(dismissAlert:) userInfo:[NSDictionary dictionaryWithObjectsAndKeys:alert,@"alert", nil] repeats:NO]; [alert release]; } } else{ //对用户进行各种提示 if(![self getNetConnectState]){ // 网络不正常 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"没有连接到网络!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil]; [alert show]; [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(dismissAlert:) userInfo:[NSDictionary dictionaryWithObjectsAndKeys:alert,@"alert", nil] repeats:NO]; [alert release]; } if ([txtPhone.text isEqualToString:@""]) { //用户没有输入手机号 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入手机号码!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil]; [alert show]; [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(dismissAlert:) userInfo:[NSDictionary dictionaryWithObjectsAndKeys:alert,@"alert", nil] repeats:NO]; [alert release]; } } }
第一种编码思路是符合“流程图”的思想,让整个业务形成一个“流”的概念。
但是,人类正常的思维方式是“结构化”的,而不是单一的“流”。所以,第二种编码思想更加接近人类“真实”的思维方式。它将使代码更加易于理解和维护,尤其利于扩展。因为这个思想将使业务的核心代码集中在一起,而不是分散在“流”的各个节点上。
“结构化的编码方式”,将使代码模块化称为可能。
之前,我们常常要求软件工程师编码要有明确的“流程“,但同时还要求获得“模块化”的代码块,现在想想,这就是一个悖论。”流程化“的思想如何创造”模块化“的代码块?除非我们在编码的时候就使采用“结构化的编码思想”,才会获得“模块化”的功能代码。