新聞中心

創(chuàng)新互聯(lián)建站服務(wù)項(xiàng)目包括岢嵐網(wǎng)站建設(shè)、岢嵐網(wǎng)站制作、岢嵐網(wǎng)頁制作以及岢嵐網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,岢嵐網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到岢嵐省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
也許在Python編碼風(fēng)格指導(dǎo)(PEP8)中最有爭議的一部分要數(shù)每行代碼不超過80個(gè)字符的限制。沒錯(cuò),實(shí)際上是79個(gè)字符,但我使用80個(gè)字符,這個(gè)大概數(shù),它是給程序員的一個(gè)參考值。
[[69723]]
古老的VT100終端
現(xiàn)在很多軟件公司采用的編碼規(guī)范基本是PEP8,但每行80個(gè)字符的限制除外。GitHub上的項(xiàng)目,大多數(shù)都遵循PEP8規(guī)范(這一點(diǎn)似乎達(dá)到了高度的統(tǒng)一),但遵守80個(gè)字符限制的很少。在一些有明確規(guī)定的規(guī)范標(biāo)準(zhǔn)中,這個(gè)限制可能會增加(100或120),甚至完全刪除。這樣做常長見的理由是:我們已經(jīng)不是使用VT100終端編程的年代了,我們有了更大,更高分辨率的屏幕。這是事實(shí),但我發(fā)現(xiàn),在Python編碼中采用這個(gè)80個(gè)字符的規(guī)范,配合空格的使用,這會讓我們的代碼更急湊,更可讀。
有一點(diǎn)你可以看出,在自然情況下,Python語句的長度一般會占大概35-60個(gè)字符(不包括縮進(jìn))。更長的語句很少見。如果突然有一個(gè)句子比其它的要長很多,會顯得很突兀,不好看。同樣,使用強(qiáng)制性的空格來增加行寬能夠從視覺上幫助你優(yōu)化減少嵌套循環(huán)的層數(shù),一般的建議是重構(gòu)代碼不要讓縮進(jìn)多于4層。
例如,把下面這個(gè):
- def search(directory, file_pattern, path_match,
- follow_symlinks=True, output=True, colored=True):
- ''' Search the files matching the pattern. The files will be returned, and can be optionally printed '''
- pattern = re.compile(file_pattern)
- results = []
- for root, sub_folders, files in os.walk(directory, followlinks=follow_symlinks):
- # Ignore hidden directories
- if '/.' in root:
- continue
- # Search in files and subfolders
- for filename in files + sub_folders:
- full_filename = os.path.join(root, filename)
- to_match = full_filename if path_match else filename
- match = re.search(pattern, to_match)
- if match:
- # Split the match to be able to colorize it
- # prefix, matched_pattern, sufix
- smatch = [to_match[:match.start()], to_match[match.start(): match.end()], to_match[match.end():]]
- if not path_match:
- # Add the fullpath to the prefix
- smatch[0] = os.path.join(root, smatch[0])
- if output:
- print_match(smatch, colored)
- results.append(full_filename)
- return results
和這個(gè)比較:
- def search(directory, file_pattern, path_match,
- follow_symlinks=True, output=True, colored=True):
- ''' Search the files matching the pattern.
- The files will be returned, and can be optionally printed '''
- pattern = re.compile(file_pattern)
- results = []
- for root, sub_folders, files in os.walk(directory,
- followlinks=follow_symlinks):
- # Ignore hidden directories
- if '/.' in root:
- continue
- # Search in files and subfolders
- for filename in files + sub_folders:
- full_filename = os.path.join(root, filename)
- to_match = full_filename if path_match else filename
- match = re.search(pattern, to_match)
- if match:
- # Split the match to be able to colorize it
- # prefix, matched_pattern, sufix
- smatch = [to_match[:match.start()],
- to_match[match.start(): match.end()],
- to_match[match.end():]]
- if not path_match:
- # Add the fullpath to the prefix
- smatch[0] = os.path.join(root, smatch[0])
- if output:
- print_match(smatch, colored)
- results.append(full_filename)
- return results
在第一段代碼里會出現(xiàn)滾動條,但即使是沒有出現(xiàn)滾動條,這代碼表現(xiàn)的也不美觀,視覺上不平衡。第二段代碼看起來更好,更容易閱讀。
另外重要的一點(diǎn)是,我可以在屏幕上顯示更多的東西。很多時(shí)候我們都需要屏幕上同時(shí)看一個(gè)文件的多個(gè)地方,或多個(gè)文件的內(nèi)容。我喜歡的實(shí)現(xiàn)這個(gè)目的的方法是讓它們按列排列。如果整個(gè)文件有80個(gè)寬度的限制,代碼會有一個(gè)很好的呈現(xiàn),我不用擔(dān)心代碼在編輯器里會否自動折行,不用去麻煩配置編輯器。如果我需要使用 vim在命令行里快速編輯一個(gè)文件,就不用擔(dān)心文件的寬度。能專注于代碼。
豎行排列顯示
唯一有問題的是使用Django的時(shí)候。當(dāng)使用Django框架,你需要使用很多像這樣的調(diào)用:
- ThisIsMyModel.objects.find(field1=value1, field2=value2).count()
在有縮進(jìn)的代碼里,一個(gè)‘最小’的model函數(shù)調(diào)用都會讓你沒有多少剩余空間…但我仍然堅(jiān)持相同的原則,盡量讓代碼表現(xiàn)的清晰可讀,但這比起其它Python代碼來要難的多。
所以,即使這個(gè)限制最初的愿望已經(jīng)和現(xiàn)在完全不符合,我仍然覺得這個(gè)限制能幫助我寫出更可讀緊湊的代碼。我是一個(gè)要求“可讀性”的狂熱分子,我甚至認(rèn)為代碼的可讀性是一個(gè)最重要的需要考慮的方面,程序員應(yīng)該在任何時(shí)候都銘記這一點(diǎn)。
英文原文:80 chars per line is great
譯文連接:http://www.aqee.net/80-chars-per-line-is-great/
本文名稱:為何編碼規(guī)范每行代碼不超過80個(gè)字符是合理的
文章位置:http://www.dlmjj.cn/article/cdsicsc.html


咨詢
建站咨詢
