ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] ## 减少嵌套层次的技术总结 ### 重复判断一部分条件 bad ``` if(inputStatus == InputStatus_Success){ // lots of code ... if( printerRoutine !=NULL){ // lots of code ... if(SetupPage()){ // lots of code ... if(AllocMen(&printData)){ // lots of code ... } } } } ``` good ``` if(inputStatus == InputStatus_Success){ // lots of code ... if(printRouine !=NULL){ // lots of code ... } } if(inputStatus == InputStatus_Success && (printRoutine !=NULL) && SetupPage() ){ // lots of code ... if(AllocMen(& printData)){ // lots of code ... } } ``` ### 转换成if-then-else bad ``` if(10 <quantity){ if(100 <quantity){ if(100 <quantity){ discount = 0.10; }else{ discount = 0.05; } }else{ discount = 0.025; } }else{ discount = 0.0; } ``` good ``` if(1000 <quantity){ discount = 0.10; }else if( 100 <quantity ){ discount = 0.05; }else if( 10 <quantity ){ discount = 0.025; }else{ discount = 0; } ``` ### 转换成 switch 语句 bad ``` while( !Transactionscomplete()){ // read transaction record transaction= ReadTransaction(); // process transaction depending on type of transaction if( transaction.Type == TransactionType_Deposit ){ ProcessDeposit( transaction. AccountType, transaction Account SubType, transaction. AccountNum, transaction. Amount ) }else if( transaction.Type == TransactionType_withdrawal){ Processwitharawal( transaction.Account Type transaction.Account Num transaction.Amount ) }else if( transaction.Type == TransactionType_Transfer ){ MakePundsTransfer( transaction.SourceAccountType, transaction.TargetAccountType, transaction.AccountNum, transaction.Amount ) }else{ // process unknown transaction type LogTransactionError("Unknown Transaction Type", transaction); } } ``` good ``` while (!Transactionscomplete()){ transaction= ReadTransaction(); switch(transaction.Type){ case (TransactionType_Deposit); ProcessDeposit( transaction. AccountType, transaction Account SubType, transaction. AccountNum, transaction. Amount ) break; case (TransactionType_withdrawal); Processwitharawal( transaction.Account Type transaction.Account Num transaction.Amount ) break; case (TransactionType_Transfer); MakePundsTransfer( transaction.SourceAccountType, transaction.TargetAccountType, transaction.AccountNum, transaction.Amount ) break; default: LogTransactionError("Unknown Transaction Type", transaction); break; } } ``` ### 使用对象和多态派分(polymorphic dispatch) ``` TransactionData transactionData Transaction *transaction while( !Transactionscomplete())( // read transaction record transactionData = ReaaTransaction(); // create transaction object, depending on type of transaction switch transactionDataType ){ case( TransactionType_Deposit ): transaction= new Deposit( transactionData ); break case( TransactionType_withdrawal ): transaction= new withdrawal( transactionData); break; case( TransactionType_Transfer ): transaction = new Transfer( transactionData); break default: // process unknown transaction type LogTransactionError('Unknown Transaction Type", transactionData) return; } transaction->Complete(); delete transaction; ``` ### 把深层嵌套的代码提取成单独的子程序 对于各种规模的系统,都可以用 Factory Method 模式来替换其中的 switch语句 该模式可以重用于系统中任何需要创建 Transaction 类型对象的场合。如果在这个系统里使用下面的代码,那么这一部分还会变得更加简单 ``` TransactionData transactionData: Transaction *transaction: while( !Transactionscomplete())( // read transaction record and complete transaction transactionData = ReadTransaction(); transaction = TransactionFactory.Create( transactionData ); transaction->Complete(); delete transaction(); } ```