亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home php教程 php手冊 編寫易讀的代碼

編寫易讀的代碼

Jun 21, 2016 am 09:14 AM
name pascal

成功的開發(fā)團(tuán)隊(duì)要求隊(duì)伍中的每一位成員遵守代碼重用規(guī)則,這些規(guī)定把代碼的重用性推到極至同時(shí)卻不會(huì)顯著降低開發(fā)人員的創(chuàng)造力和開發(fā)效率。如果編寫和使用代碼的開發(fā)人員遵守共同的程序命名規(guī)范代碼和代碼注釋要求,那么代碼的重用性就會(huì)得以大大提升。這些標(biāo)準(zhǔn)的起點(diǎn)是系統(tǒng)結(jié)構(gòu)級的。你的功能規(guī)范應(yīng)該在類、屬性的名字、函數(shù)返回值以及其他關(guān)鍵程序元素的定義中反映這些標(biāo)準(zhǔn)。本文將就基本的命名規(guī)則和注釋提出一些可行的建議,意圖幫助讀者開發(fā)自己的代碼重用標(biāo)準(zhǔn)。


大小寫標(biāo)準(zhǔn)


在我們開始討論各類程序要素命名的正確方式之前,先讓我們定義區(qū)分元素的字符大小寫的兩種最常用方式,它們是:

Pascal規(guī)范—第1個(gè)字符大寫,目標(biāo)名中的每個(gè)單詞的第1個(gè)字母也大寫,比如InvoiceNumber或者PrintInvoice。其他的所有字符都小寫。
Camel規(guī)范—第1個(gè)字符不大寫,但目標(biāo)名中的每個(gè)單詞的第1個(gè)字母大寫,比如,invoiceNumber。其他的所有字符都小寫。
可是,采用字符大小寫區(qū)分元素可能在對大小寫不敏感的編程語言中引發(fā)問題。比方說,由于C#語言區(qū)分大小寫,所以你可以調(diào)用私有變量employee,接著它所具有的公共屬性Employee則可以被調(diào)用者所用。這些操作是完全合法的。但是,對Visual Basic來說就會(huì)產(chǎn)生錯(cuò)誤,因?yàn)閂B是不區(qū)分字母大小寫的,以上兩種元素在VB看來都是一回事。假如你在混合語言環(huán)境下工作,你只能指定某些規(guī)則要求開發(fā)人員合理利用多種語言閱讀其他人開發(fā)的代碼。


命名標(biāo)準(zhǔn)
假設(shè)我們采用了以上的大小寫標(biāo)準(zhǔn),現(xiàn)在就讓我們了解一些通用程序元素的簡單命名建議。


某些類設(shè)計(jì)為模擬真實(shí)世界的對象,就這些類來說,所選用的名字就應(yīng)該反映真實(shí)世界的對象、具有單數(shù)名詞的格式,比方Employee、 Invoice或者Timecard等。對內(nèi)部類而言可以采用Pascal規(guī)范令結(jié)果類具有單數(shù)形式的名字,比如ThreadPool或者CustomColor等。類應(yīng)當(dāng)是單數(shù)的,這樣它們的復(fù)數(shù)形式就可以代表同類的集合名,比如Employees數(shù)組等。

類的成員
采用C#以及其他大小寫敏感編程語言的開發(fā)人員應(yīng)當(dāng)采用camel規(guī)范命名類成員的名字。這樣做可以讓開發(fā)者更易于區(qū)分內(nèi)部變量的名字(name)和公共屬性的名字(Name)。許多VB開發(fā)人員更喜歡采用匈牙利命名法為類成員起名,也就是在名字前面加上前綴表示變量的類型,比如sName就指的是string類型的Name變量。我認(rèn)為,在使用VS.NET這樣高級的開發(fā)環(huán)境下這樣做是不必要的,因?yàn)樵谶@種情況下系統(tǒng)鼠標(biāo)停留在變量之上即可可自動(dòng)顯示變量的類型。我個(gè)人喜歡在類成員名前加上前綴:小寫的字母m。這樣內(nèi)部變量就保存了足夠的內(nèi)部類信息:內(nèi)部變量mName就正好代表了公共屬性Name。

方法
方法應(yīng)該用Pascal規(guī)范命名,同時(shí)用合理的方式說明他們的實(shí)施行為。比方說,給數(shù)據(jù)庫添加雇員的方法可以命名為AddEmployee,而打印發(fā)票的方法則不妨命名為PrintInvoice。假如方法返回的是布爾值,那么方法名應(yīng)該以動(dòng)詞開頭以便用在if語句的時(shí)候其含義更明顯。比如說,假如你有一個(gè)方法的功能是確定某位雇員是否符合公司401k計(jì)劃的要求,那么你可以在If語句中調(diào)用IsEligible401k方法:If IsEligible401k then…

方法參數(shù)、返回值和變量
所有的方法參數(shù)、返回值和變量都應(yīng)該采用Pascal規(guī)范命名,同方法名一樣也應(yīng)該能反映參數(shù)或者變量所代表的含義。這一點(diǎn)對參數(shù)方法而言特別重要,因?yàn)槟阍谡{(diào)用方法的時(shí)候智能感知(Intellisense)會(huì)返回參數(shù)名和參數(shù)類型。所有采用方法的開發(fā)人員都應(yīng)該使用描述性的名字和類型,便于相互理解其含義。

控件
控件命名是開發(fā)領(lǐng)域一個(gè)經(jīng)常引發(fā)爭議的問題。雖然大多數(shù)人贊同不應(yīng)該使用控件的默認(rèn)名稱,比如TextBox1或者Label1等等,但是,他們還反對按照變量的方式命名控件或者采用前綴表示控件的類型。我比較喜歡采用標(biāo)準(zhǔn)的三字母前綴命名窗體中控件的名字。比如說,保存姓氏和名字的文本框控件就不妨分別命名為txtLastName和txtFirstName。處理窗體數(shù)據(jù)的命令按鈕則可以命名為cmdSubmit或者cmdCancel。其實(shí),只要你能保證控件命名的一致性而且標(biāo)準(zhǔn)易于理解即可。

注釋
注釋代碼對所有開發(fā)人員來說都是必要的。為了教授正確的注釋技術(shù),我就經(jīng)常在自己的演示程序中添加注釋代碼。同時(shí),為了簡化注釋過程,我建議開發(fā)人員首先編寫注釋說明他們想編寫的程序。我首先會(huì)寫注釋說明程序中的過程、類或者其他程序要素,但對其具體工作原理不做闡述。然后我會(huì)編寫一系列的注釋代碼描述過程的每一主要步驟或者類的元素。在編寫了定義類或者說明過程的代碼之后,我對各個(gè)外部變量、控件、打開的文件乃至其他過程所訪問的元素文檔化,對輸入?yún)?shù)和返回值做簡要說明。

如果你在使用C#開發(fā)程序,那么VS.NET環(huán)境已具有內(nèi)置的工具幫助你把內(nèi)部C# 注釋轉(zhuǎn)換為外部HTML文檔。你可以在自己的文檔中加上特殊的處理指示符而改變外部文檔的表示方式。有關(guān)這方面的更多信息可以參考VS.NET內(nèi)部幫助文件: ms-help://MS.VSCC/MS.MSDNVS/csref/html/vcoriXMLDocumentation.htm.



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
Introduction to Pascal language Introduction to Pascal language Jun 14, 2023 pm 05:38 PM

The Pascal language was proposed by Professor N. Wirth of Switzerland in 1971. It is named in honor of the French mathematician Pascal. It is the world's first structured high-level programming language. Structured programming mainly consists of three basic structures (sequential structure, selection structure, and loop structure). Later, some people regarded functions and procedures as the fourth basic structure. These four structures have an entrance and an exit from the outside. The structure is clear and avoids the confusion caused by GOTO statements.

php提交表單通過后,彈出的對話框怎樣在當(dāng)前頁彈出,該如何解決 php提交表單通過后,彈出的對話框怎樣在當(dāng)前頁彈出,該如何解決 Jun 13, 2016 am 10:23 AM

php提交表單通過后,彈出的對話框怎樣在當(dāng)前頁彈出php提交表單通過后,彈出的對話框怎樣在當(dāng)前頁彈出而不是在空白頁彈出?想實(shí)現(xiàn)這樣的效果:而不是空白頁彈出:------解決方案--------------------如果你的驗(yàn)證用PHP在后端,那么就用Ajax;僅供參考:HTML code

Introduction to PASCAL language basics Introduction to PASCAL language basics Jun 14, 2023 pm 05:46 PM

The PASCAL language is also an algorithmic language. It was developed by N. It was designed by Professor Niklaus Wirth in 1968 and officially published in 1971. In 1975, the PASCAL language was modified as the "Standard PASCAL Language".

What should I do if php cannot get the name? What should I do if php cannot get the name? Nov 24, 2022 am 09:56 AM

PHP cannot get the name because when the name and id values ??of the form element are different, the browser cannot recognize it. The solution: 1. Check whether some form elements and frame elements use name; 2. Check only Elements that can be assigned ID but not name; 3. For multi-select box checkbox, you can use "join(',', $__POST['name'])" to form data.

How to add name to setup in Vue3 How to add name to setup in Vue3 May 13, 2023 am 09:40 AM

What is the use of name in Vue3? 1. Name needs to be defined when making recursive components. 2. The component can be cached with keep-aliveincludeexclude. 3. When Vue reports an error or is debugging, you can see the name of the component. Vue3 defines name1. It is automatically generated as long as the setup syntax sugar mode single file component is turned on in the script. The corresponding name option will be automatically generated based on the file name. For example, Tree.vue, then its name will be automatically generated by Tree. This has a drawback. If you want to modify the name, you need to modify the component name. If there is a place to import the component, you need to modify it together. 2. Open a script to define name

How to install Pascal How to install Pascal Jun 14, 2023 pm 05:41 PM

Editing, saving and debugging of PASCAL programming: 1. Please confirm the disk directory where the Turbo.exe file is located. If it is placed in the TP7 folder (also known as a subdirectory) on the F: drive. 2. Set the disk where you want to save the program file as the current disk, and save the file in the current directory of the current disk. If your program files are to be stored in the current directory of the H: drive, enter the following command after the DOS prompt ">".

不用數(shù)據(jù)庫來實(shí)現(xiàn)用戶的簡單的下載,代碼如下,但是卻不能下載,請高手找下原因,文件路勁什么的沒有關(guān)問題 不用數(shù)據(jù)庫來實(shí)現(xiàn)用戶的簡單的下載,代碼如下,但是卻不能下載,請高手找下原因,文件路勁什么的沒有關(guān)問題 Jun 13, 2016 am 10:15 AM

不用數(shù)據(jù)庫來實(shí)現(xiàn)用戶的簡單的下載,代碼如下,但是卻不能下載,請高手找下原因,文件路勁什么的沒問題。

From Coin Flips to Cityscapes: How Names, Parks, and Planting Shaped St. Albert From Coin Flips to Cityscapes: How Names, Parks, and Planting Shaped St. Albert Jul 08, 2025 am 10:04 AM

Explore the fascinating story of André Gate and his lasting impact on St. Albert's parks and greenery, plus the latest news in play-to-earn gaming and crypto.

See all articles