规范2

Jun 13, 2016 am 10:23 AM
else then different layout brackets style Format of programmer flower specification Pass

If Then Else 格式 布局 这由程序员决定。不同的花括号样式会产生些微不同的样观。一个通用方式是: if (条件1) // 注释 { } else if (条件2) // 注释 { } else // 注释 { } 如果你有用到else if 语句的话,通常最好有一个else块以用于处理未处理到的其他情况。可以的话 放一个记录信息注释在else处,即使在else没有任何的动作。 条件格式 总是将恒量放在等号/不等号的左边,例如: if ( 6 == $errorNum ) ... 一个原因是假如你在等式中漏了一个等号,语法检查器会为你报错。第二个原因是你能立刻找到数值 而不是在你的表达式的末端找到它。需要一点时间来习惯这个格式,但是它确实很有用。 -------------------------------------------------------------------------------- switch 格式 Falling through a case statement into the next case statement shall be permitted as long as a comment is included. default case总应该存在,它应该不被到达,然而如果到达了就会触发一个错误。 如果你要创立一个变量,那就把所有的代码放在块中。 例如 switch (...) { case 1: ... // FALL THROUGH case 2: { $v = get_week_number(); ... } break; default: } -------------------------------------------------------------------------------- continue,break 和 ? 的使用: Continue 和 Break Continue 和 break 其实是变相的隐蔽的 goto方法。 Continue 和 break 像 goto 一样,它们在代码中是有魔力的,所以要节俭(尽可能少)的使用它们。 使用了这一简单的魔法,由于一些未公开的原因,读者将会被定向到只有上帝才知道的地方去。 Continue有两个主要的问题: 它可以绕过测试条件。 它可以绕过等/不等表达式。 看看下面的例子,考虑一下问题都在哪儿发生: while (TRUE) { ... // A lot of code ... if (/* some condition */) { continue; } ... // A lot of code ... if ( $i++ > STOP_VALUE) break; } 注意:"A lot of code"是必须的,这是为了让程序员们不能那么容易的找出错误。 通过以上的例子,我们可以得出更进一步的规则:continue 和 break 混合使用是引起灾难的正确方法。 ?: 麻烦在于人民往往试着在 ? 和 : 之间塞满了许多的代码。以下的是一些清晰的连接规则: 把条件放在括号内以使它和其他的代码相分离。 如果可能的话,动作可以用简单的函数。 把所做的动作,“?”,“:”放在不同的行,除非他们可以清楚的放在同一行。 例如 (condition) ? funct1() : func2(); or (condition) ? long statement : another long statement; -------------------------------------------------------------------------------- 声明块的定位 声明代码块需要对齐。 理由Justification 清晰。 变量初始化的类似代码块应该列表。 The ??token should be adjacent to the type, not the name. 例如 var $mDate var& $mrDate var& $mrName var $mName $mDate = 0; $mrDate = NULL; $mrName = 0; $mName = NULL; -------------------------------------------------------------------------------- 每行一个语句 除非这些语句有很密切的联系,否则每行只写一个语句。 -------------------------------------------------------------------------------- 短方法 方法代码要限制在一页内。 理由 这个思想是,每一个方法代表着一个完成单独目的的技术。 从长远来说,过多的无效参数是错误的。 调用函数比不调用要慢,但是这需要详细考虑做出决定(见premature optimization 未完善的优化)。 -------------------------------------------------------------------------------- 记录所有的空语句 总是记录下for或者是while的空块语句,以便清楚的知道该段代码是漏掉了,还是故意不写的。 while ($dest++ = $src++) ; // VOID -------------------------------------------------------------------------------- 不要采用缺省方法测试非零值 不要采用缺省值测试非零值,也就是使用: if (FAIL != f()) 比下面的方法好: if (f()) 即使 FAIL 可以含有 0 值 ,也就是PHP认为false的表示。在某人决定用-1代替0作为失败返回值的时候, 一个显式的测试就可以帮助你了。就算是比较值不会变化也应该使用显式的比较;例如:if (!($bufsize % strlen($str))) 应该写成:if (($bufsize % strlen($str)) == 0)以表示测试的数值(不是布尔)型。一个经常出 问题的地方就是使用strcmp来测试一个字符等式,结果永远也不会等于缺省值。 非零测试采用基于缺省值的做法,那么其他函数或表达式就会受到以下的限制: 只能返回0表示失败,不能为/有其他的值。 命名以便让一个真(true)的返回值是绝对显然的,调用函数IsValid()而不是Checkvalid()。 -------------------------------------------------------------------------------- 布尔逻辑类型 大部分函数在FALSE的时候返回0,但是发挥非0值就代表TRUE,因而不要用1(TRUE,YES,诸如此类)等式检测一个布尔值,应该用0(FALSE,NO,诸如此类)的不等式来代替: if (TRUE == func()) { ... 应该写成: if (FALSE != func()) { ... -------------------------------------------------------------------------------- 通常避免嵌入式的赋值 有时候在某些地方我们可以看到嵌入式赋值的语句,那些结构不是一个比较好的少冗余,可读性强的方法。 while ($a != ($c = getchar())) { process the character } ++和--操作符类似于赋值语句。因此,出于许多的目的,在使用函数的时候会产生副作用。使用嵌入式赋值 提高运行时性能是可能的。无论怎样,程序员在使用嵌入式赋值语句时需要考虑在增长的速度和减少的可维 护性两者间加以权衡。例如: a = b + c; d = a + r; 不要写成: d = (a = b + c) + r; 虽然后者可以节省一个周期。但在长远来看,随着程序的维护费用渐渐增长,程序的编写者对代码渐渐遗忘, 就会减少在成熟期的最优化所得。 -------------------------------------------------------------------------------- 重用您和其他人的艰苦工作 跨工程的重用在没有一个通用结构的情况下几乎是不可能的。对象符合他们现有的服务需求,不同的过程有着 不同的服务需求环境,这使对象重用变得很困难。 开发一个通用结构需要预先花费许多的努力来设计。当努力不成功的时候,无论出于什么原因,有几种办法推 荐使用: 请教!给群组发Email求助 这个简单的方法很少被使用。因为有些程序员们觉得如果他向其他人求助,会显得自己水平低,这多傻啊!做新 的有趣的工作,不要一遍又一遍的做别人已经做过的东西。 如果你需要某些事项的源代码,如果已经有某人做过的话,就向群组发email求助。结果会很惊喜哦! 在许多大的群组中,个人往往不知道其他人在干什么。你甚至可以发现某人在找一些东西做,并且自愿为你写代 码,如果人们在一起工作,外面就总有一个金矿。 告诉!当你在做事的时候,把它告诉所有人 如果你做了什么可重用的东西的话,让其他人知道。别害羞,也不要为了保护自豪感而把你的工作成果藏起来。 一旦养成共享工作成果的习惯,每个人都会获得更多。 Dont be Afraid of Small Libraries 对于代码重用,一个常见的问题就是人们不从他们做过的代码中做库。一个可重用的类可能正隐蔽在一个程序目 录并且决不会有被分享的激动,因为程序员不会把类分拆出来加入库中。 这样的其中一个原因就是人们不喜欢做一个小库,对小库有一些不正确感觉。把这样的感觉克服掉吧,电脑才不 关心你有多少个库呢。 如果你有一些代码可以重用,而且不能放入一个已经存在的库中,那么就做一个新的库吧。如果人们真的考虑重 用的话,库不会在很长的一段时间里保持那么小的。 If you are afraid of having to update makefiles when libraries are recomposed or added then dont include libraries in your makefiles, include the idea of services. Base level makefiles define services that are each composed of a set of libraries. Higher level makefiles specify the services they want. When the libraries for a service change only the lower level makefiles will have to change. Keep a Repository Most companies have no idea what code they have. And most programmers still dont communicate what they have done or ask for what currently exists. The solution is to keep a repository of whats available. In an ideal world a programmer could go to a web page, browse or search a list of packaged libraries, taking what they need. If you can set up such a system where programmers voluntarily maintain such a system, great. If you have a librarian in charge of detecting reusability, even better. Another approach is to automatically generate a repository from the source code. This is done by using common class, method, library, and subsystem headers that can double as man pages and repository entries. -------------------------------------------------------------------------------- 评价注释 注释应该是讲述一个故事 Consider your comments a story describing the system. Expect your comments to be extracted by a robot and formed into a man page. Class comments are one part of the story, method signature comments are another part of the story, method arguments another part, and method implementation yet another part. All these parts should weave together and inform someone else at another point of time just exactly what you did and why. Document Decisions Comments should document decisions. At every point where you had a choice of what

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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How to set up the keyboard boot function on a GIGABYTE motherboard (enable keyboard boot mode on GIGABYTE motherboard) How to set up the keyboard boot function on a GIGABYTE motherboard (enable keyboard boot mode on GIGABYTE motherboard) Dec 31, 2023 pm 05:15 PM

How to set up keyboard startup on Gigabyte's motherboard. First, if it needs to support keyboard startup, it must be a PS2 keyboard! ! The setting steps are as follows: Step 1: Press Del or F2 to enter the BIOS after booting, and go to the Advanced (Advanced) mode of the BIOS. Ordinary motherboards enter the EZ (Easy) mode of the motherboard by default. You need to press F7 to switch to the Advanced mode. ROG series motherboards enter the BIOS by default. Advanced mode (we use Simplified Chinese to demonstrate) Step 2: Select to - [Advanced] - [Advanced Power Management (APM)] Step 3: Find the option [Wake up by PS2 keyboard] Step 4: This option The default is Disabled. After pulling down, you can see three different setting options, namely press [space bar] to turn on the computer, press group

The first choice for CS players: recommended computer configuration The first choice for CS players: recommended computer configuration Jan 02, 2024 pm 04:26 PM

1. Processor When choosing a computer configuration, the processor is one of the most important components. For playing games like CS, the performance of the processor directly affects the smoothness and response speed of the game. It is recommended to choose Intel Core i5 or i7 series processors because they have powerful multi-core processing capabilities and high frequencies, and can easily cope with the high requirements of CS. 2. Graphics card Graphics card is one of the important factors in game performance. For shooting games such as CS, the performance of the graphics card directly affects the clarity and smoothness of the game screen. It is recommended to choose NVIDIA GeForce GTX series or AMD Radeon RX series graphics cards. They have excellent graphics processing capabilities and high frame rate output, and can provide a better gaming experience. 3. Memory power

How to write the qq mailbox format? What is the qq mailbox format? How to write the qq mailbox format? What is the qq mailbox format? Feb 22, 2024 pm 03:40 PM

QQ email: QQ number@qq.com, English QQ email: English or numbers@qq.com, foxmail email account: set up your own account@foxmail.com, mobile phone email account: mobile phone number@qq.com. Tutorial Applicable Model: iPhone13 System: IOS15.3 Version: QQ Mailbox 6.3.3 Analysis 1QQ mailbox has four formats, commonly used QQ mailbox: QQ number@qq.com, English QQ mailbox: English or numbers@qq.com, foxmail Email account: set up your own account@foxmail.com, mobile phone email account: mobile phone number@qq.com. Supplement: What is qq mailbox? 1 The earliest QQ mailbox was only between QQ users

Digital audio output interface on the motherboard-SPDIF OUT Digital audio output interface on the motherboard-SPDIF OUT Jan 14, 2024 pm 04:42 PM

SPDIFOUT connection line sequence on the motherboard. Recently, I encountered a problem regarding the wiring sequence of the wires. I checked online. Some information says that 1, 2, and 4 correspond to out, +5V, and ground; while other information says that 1, 2, and 4 correspond to out, ground, and +5V. The best way is to check your motherboard manual. If you can't find the manual, you can use a multimeter to measure it. Find the ground first, then you can determine the order of the rest of the wiring. How to connect motherboard VDG wiring When connecting the VDG wiring of the motherboard, you need to plug one end of the VGA cable into the VGA interface of the monitor and the other end into the VGA interface of the computer's graphics card. Please be careful not to plug it into the motherboard's VGA port. Once connected, you can

Which AI programmer is the best? Explore the potential of Devin, Tongyi Lingma and SWE-agent Which AI programmer is the best? Explore the potential of Devin, Tongyi Lingma and SWE-agent Apr 07, 2024 am 09:10 AM

On March 3, 2022, less than a month after the birth of the world's first AI programmer Devin, the NLP team of Princeton University developed an open source AI programmer SWE-agent. It leverages the GPT-4 model to automatically resolve issues in GitHub repositories. SWE-agent's performance on the SWE-bench test set is similar to Devin, taking an average of 93 seconds and solving 12.29% of the problems. By interacting with a dedicated terminal, SWE-agent can open and search file contents, use automatic syntax checking, edit specific lines, and write and execute tests. (Note: The above content is a slight adjustment of the original content, but the key information in the original text is retained and does not exceed the specified word limit.) SWE-A

Glodon Software's computer configuration recommendations; Glodon Software's computer configuration requirements Glodon Software's computer configuration recommendations; Glodon Software's computer configuration requirements Jan 01, 2024 pm 12:52 PM

Glodon Software is a software company focusing on the field of building informatization. Its products are widely used in all aspects of architectural design, construction, and operation. Due to the complex functions and large data volume of Glodon software, it requires high computer configuration. This article will elaborate on the computer configuration recommendations of Glodon Software from many aspects to help readers choose a suitable computer configuration processor. Glodon Software requires a large amount of data calculation and processing when performing architectural design, simulation and other operations. Therefore, the requirements for the processor are higher. It is recommended to choose a multi-core, high-frequency processor, such as Intel i7 series or AMD Ryzen series. These processors have strong computing power and multi-thread processing capabilities, and can better meet the needs of Glodon software. Memory Memory is affecting computing

Revealing the appeal of C language: Uncovering the potential of programmers Revealing the appeal of C language: Uncovering the potential of programmers Feb 24, 2024 pm 11:21 PM

The Charm of Learning C Language: Unlocking the Potential of Programmers With the continuous development of technology, computer programming has become a field that has attracted much attention. Among many programming languages, C language has always been loved by programmers. Its simplicity, efficiency and wide application make learning C language the first step for many people to enter the field of programming. This article will discuss the charm of learning C language and how to unlock the potential of programmers by learning C language. First of all, the charm of learning C language lies in its simplicity. Compared with other programming languages, C language

Guide to solving misalignment of WordPress web pages Guide to solving misalignment of WordPress web pages Mar 05, 2024 pm 01:12 PM

Guide to solving misaligned WordPress web pages In WordPress website development, sometimes we encounter web page elements that are misaligned. This may be due to screen sizes on different devices, browser compatibility, or improper CSS style settings. To solve this misalignment, we need to carefully analyze the problem, find possible causes, and debug and repair it step by step. This article will share some common WordPress web page misalignment problems and corresponding solutions, and provide specific code examples to help develop

See all articles