Standard 2_PHP tutorial

Source: Internet
Author: User
Tags case statement
Specification 2. The layout of IfThenElse format is determined by the programmer. Different curly braces may have slightly different styles. A common method is: if (condition 1) comment {} elseif (condition 2) If Then Else format layout, which is determined by the programmer. Different curly braces may have slightly different styles. A common method is: if (condition 1) // note {} else if (condition 2) // annotation {} else // annotation {} if you use the else if statement, it is usually better to have an else block to handle other unprocessed situations. If possible, place a record information comment in else, even if there is no action in else. The condition format always places the constant value on the left side of the equal sign or non-equal sign, for example, if (6 = $ errorNum )... one reason is that if you miss an equal sign in the equation, the syntax examiner will report an error for you. The second reason is that you can immediately find a value instead of finding it at the end of your expression. It takes a bit of time to get used to this format, but it is really useful. Using switch format Falling through a case statement into the next case statement shall be permitted as long as a comment is wrongly ded. the default case always exists, and it should not be reached. However, if it is reached, an error will be triggered. If you want to create a variable, put all the code in the block. For example, switch (...) {case 1 :... // fall through case 2: {$ v = get_week_number ();...} break; default:} ------------------------------------------------------------------------------------ continue, break, and? Use: Continue, Break Continue, and break are actually concealed goto methods in disguise. Like goto, Continue and break are magical in code, so they should be frugal (as few as possible) to use them. With this simple magic, readers will be directed to places only known to God for some undisclosed reasons. Continue has two major problems: it can bypass the test conditions. It can bypass equal/unequal expressions. Let's take a look at the example below and consider where the problem occurs: while (TRUE ){... // A lot of code... if (/* some condition */) {continue ;}... // A lot of code... if ($ I ++> STOP_VALUE) break;} note: "A lot of code" is required, so that programmers cannot easily find errors. Through the above example, we can draw a further rule: the hybrid use of continue and break is the correct method to cause disasters.?: The trouble is that people often try? And: it is full of code. The following are clear connection rules: place conditions in parentheses to separate them from other codes. If possible, you can use simple functions for the action. Take the action, "?", ":" Is placed on different rows unless they can be clearly placed on the same row. For example (condition )? Funct1 (): func2 (); or (condition )? Long statement: another long statement; ------------------------------------------------------------------------------ alignment indicates that the block positioning declaration block must be aligned. The reason is clear. Code blocks similar to variable initialization should be listed. The ?? Token shocould be adjacent to the type, not the name. for example, var $ mDate var & $ mrDate var & $ mrName var $ mName $ mDate = 0; $ mrDate = NULL; $ mrName = 0; $ mName = NULL; -------------------------------------------------------------------------------- each line of a statement can only write one statement unless these statements are closely related. ------------------------------------------------------------------------------ The code of the short method should be limited to one page. The reason is that every method represents a technology that achieves a separate purpose. In the long run, too many invalid parameters are incorrect. Calling a function is slower than not calling it, but you need to make a decision in detail (see the optimization of premature optimization ). -------------------------------------------------------------------------------- Record all empty statements for or while, so that you can clearly know whether the code is missing or intentionally not written. While ($ dest ++ = $ src ++); // do not use the default method to test the non-zero value. do not use the default value to test the non-zero value, that is, use: if (FAIL! = F () is better than the following method: if (f () even if FAIL can contain 0 values, that is, PHP considers false to indicate. When someone decides to use-1 instead of 0 as the return value for failure, an explicit test will help you. Even if the comparison value does not change, explicit comparison should be used. for example: if (! ($ Bufsize % strlen ($ str) should be written as: if ($ bufsize % strlen ($ str) = 0) to represent the test value (not Boolean) type. A common problem is to use strcmp to test a character equation, and the result will never be equal to the default value. If the non-zero test adopts the default value-based approach, other functions or expressions will be subject to the following restrictions: only 0 can be returned, indicating failure, not/with other values. Name it so that the return value of a true value is absolutely clear. call the IsValid () function instead of Checkvalid (). Most functions of the Boolean logic type return 0 when FALSE, but a non-0 value indicates TRUE. therefore, do not use the 1 (TRUE, YES, and so on) equation to detect a Boolean value, we should replace the inequality of 0 (FALSE, NO, and so on) with: if (TRUE = func ()){... it should be written as: if (FALSE! = Func ()){... ------------------------------------------------------------------------------ Avoid embedded assignment. sometimes we can see embedded assignment statements in some places. The structure is not a good method with less redundancy and readability. While ($! = ($ C = getchar () {process the character} ++ and -- operators are similar to value assignment statements. Therefore, for many purposes, side effects may occur when using functions. It is possible to use embedded assignment to improve the runtime performance. In any case, when using embedded value assignment statements, programmers need to consider the trade-off between the increase speed and the reduced maintainability. For example: a = B + c; d = a + r; do not write it as: d = (a = B + c) + r; although the latter can save a cycle. However, in the long run, with the increasing maintenance costs of the program, the program writers gradually forget the code and will reduce the optimization gains in maturity. ------------------------------------------------------------------------------ It is almost impossible to reuse the hard work between you and others without a general structure. Objects meet their existing service requirements. different processes have different service requirements environments, which makes Object Reuse very difficult. Developing a general structure requires a lot of effort in advance. When the effort fails, there are several ways to recommend it for whatever reason: ask! Sending emails to groups for help is rarely used. Some programmers think that if they ask for help from others, they will be very stupid! Do not do what others have done over and over again. If you need the source code of some items, if someone has already done so, send an email to the group for help. The results will be pleasantly surprised! In many large groups, individuals often do not know what others are doing. You can even find someone looking for something to do and voluntarily writing code for you. if people work together, there will always be a gold mine out there. Tell! When you are doing things, tell everyone that if you do something reusable, let others know. Don't be shy or hide your work achievements to protect your pride. Once you develop the habit of sharing work results, everyone will get more. Dont be Afraid of Small Libraries, a common problem for code reuse is that people do not make Libraries from the code they have done. A reusable class may be hidden in a program category and will never be shared, because the programmer will not split the class and add it to the database. One of these reasons is that people do not like to create a small library, which may be incorrect. Overcome this feeling, and the computer does not care about how many databases you have. If you have some code that can be reused and cannot be put into an existing library, create a new library. If people really think about reuse, the library won't be so small for a long time. 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 cocould 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 be double as man pages and repository entries. ------------------------------------------------------------------------------ the comment should be a story about Consider your comments a story describing the system. please Ct 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 showould weave together and inform someone else at another point of time just exactly what you did and why. document Decisions Comments shoshould document decisions. at every point where you had a choice of what

The layout of http://www.bkjia.com/PHPjc/532254.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/532254.htmlTechArticleIf Then Else format is determined by the programmer. Different curly braces may have slightly different styles. A common method is: if (condition 1) // note {} else if (condition 2 )//...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.