1. 程式人生 > >1.5 引入解釋性變量

1.5 引入解釋性變量

ini amp sin dex void true 代碼 return 程序

【1】源代碼

 1 bool wasInitialized()
 2 {
 3     // do something...
 4     return true;
 5 }
 6 
 7 void Func(QString platform)
 8 {
 9     if (platform.toUpper().indexOf("MAC") > -1 &&
10             platform.toUpper().indexOf("IE") > -1 &&
11             wasInitialized() && resize > 0
) 12 { 13 // do something... 14 } 15 }

【2】引入解釋性變量

 1 bool wasInitialized()
 2 {
 3     // do something...
 4     return true;
 5 }
 6 
 7 void Func(QString platform)
 8 {
 9     bool bIsMaxOS = platform.toUpper().indexOf("MAC") > -1;
10     bool bIsIEBrowser = platform.toUpper().indexOf("
IE") > -1; 11 bool bMorethanResize = resize > 0; 12 if (bIsMaxOS && bIsIEBrowser && wasInitialized() && bMorethanResize) 13 { 14 // do something... 15 } 16 }

【3】總結

程序中有一個復雜表達式。將該復雜表達式(或其中一部分)的結果放進一個臨時變量,以此變量名稱來解釋表達式用途。

Good Good Study, Day Day Up.

順序 選擇 循環 總結

1.5 引入解釋性變量