本篇內(nèi)容主要講解“C++怎么壓縮作用域”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“C++怎么壓縮作用域”吧!
武強ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!
ES.5: 盡量壓縮作用域
可讀性。最小化資源的保持時間。避免變量的誤用。
換個說法:不要沒有必要擴大名稱的作用域。
Example(示例)
void use()
{
int i; // bad: i is needlessly accessible after loop
for (i = 0; i < 20; ++i) { /* ... */ }
// no intended use of i here
for (int i = 0; i < 20; ++i) { /* ... */ } // good: i is local to for-loop
if (auto pc = dynamic_cast<Circle*>(ps)) { // good: pc is local to if-statement
// ... deal with Circle ...
}
else {
// ... handle error ...
}
}
void use(const string& name)
{
string fn = name + ".txt";
ifstream is {fn};
Record r;
is >> r;
// ... 200 lines of code without intended use of fn or is ...
}
這個函數(shù)用任何標準衡量都太長了,但是要點在于fn使用的資源和is管理的文件被維持的時間遠遠超過需要,有可能在函數(shù)接下來的部分is和fn會被意外使用。這種情況下,分解出一個read函數(shù)可能是一個好主意。
Record load_record(const string& name)
{
string fn = name + ".txt";
ifstream is {fn};
Record r;
is >> r;
return r;
}
void use(const string& name)
{
Record r = load_record(name);
// ... 200 lines of code ...
}
Flag loop variable declared outside a loop and not used after the loop
標記在循環(huán)外定義循環(huán)變量并且循環(huán)之后不再使用的情況。
Flag when expensive resources, such as file handles and locks are not used for N-lines (for some suitable N)
標記高價值資源(例如文件句柄和鎖)在N行(適當(dāng)值)之內(nèi)沒有使用的情況。
到此,相信大家對“C++怎么壓縮作用域”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
網(wǎng)站欄目:C++怎么壓縮作用域
本文來源:http://aaarwkj.com/article8/igdjip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、動態(tài)網(wǎng)站、、云服務(wù)器、網(wǎng)站設(shè)計、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)