词条 | Best coding practices |
释义 |
}}Coding best practices are a set of informal rules that the software development community has learned over time which can help improve the quality of software.[1] Many computer programs remain in use for far longer than the original authors ever envisaged (sometimes 40 years or more),[2] so any rules need to facilitate both initial development and subsequent maintenance and enhancement by people other than the original authors. In Ninety-ninety rule, Tom Cargill is credited with this explanation as to why programming projects often run late: "The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time." Any guidance which can redress this lack of foresight is worth considering. The size of a project or program has a significant effect on error rates, programmer productivity, and the amount of management needed.[3] Software quality{{main article|Software quality}}As listed below, there are many attributes associated with good software. Some of these can be mutually contradictory (e.g. very fast versus full error checking), and different customers and participants may have different priorities. Weinberg provides an example of how different goals can have a dramatic effect on both effort required and efficiency.[4] Furthermore, he notes that programmers will generally aim to achieve any explicit goals which may be set, probably at the expense of any other quality attributes. Sommerville has identified four generalised attributes which are not concerned with what a program does, but how well the program does it:[5]
Weinberg has identified four targets which a good program should meet:[6]
PrerequisitesBefore coding starts, it is important to ensure that all necessary prerequisites have been completed (or have at least progressed far enough to provide a solid foundation for coding). If the various prerequisites are not satisfied then the software is likely to be unsatisfactory, even if it is completed. From Meek & Heath: "What happens before one gets to the coding stage is often of crucial importance to the success of the project."[8] The prerequisites outlined below cover such matters as:
For small simple projects involving only one person, it may be feasible to combine architecture with design and adopt a very simple life cycle. Life cycle{{Main article|Software development methodology}}A software development methodology is a framework that is used to structure, plan, and control the life cycle of a software product. Common methodologies include waterfall, prototyping, iterative and incremental development, spiral development, agile software development, rapid application development, and extreme programming. The waterfall model is a sequential development approach; in particular, it assumes that the requirements can be completely defined at the start of a project. However, McConnell quotes three studies which indicate that, on average, requirements change by around 25% during a project.[9] The other methodologies mentioned above all attempt to reduce the impact of such requirement changes, often by some form of step-wise, incremental, or iterative approach. Different methodologies may be appropriate for different development environments. Requirements{{Main article|Requirements engineering}}McConnell states: "The first prerequisite you need to fulfill before beginning construction is a clear statement of the problem the system is supposed to solve."[10] Meek and Heath emphasise that a clear, complete, precise, and unambiguous written specification is the target to aim for.[11] Note that it may not be possible to achieve this target, and the target is likely to change anyway (as mentioned in the previous section). Sommerville distinguishes between less detailed user requirements and more detailed system requirements.[12] He also distinguishes between functional requirements (e.g. update a record) and non-functional requirements (e.g. response time must be less than 1 second). Architecture{{Main article|Software architecture}}Hoare points out: "there are two ways of constructing a software design: one way is to make it so simple that there are obviously no deficiencies; the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult."[13] Software architecture is concerned with deciding what has to be done, and which program component is going to do it (how something is done is left to the detailed design phase, below). This is particularly important when a software system contains more than one program since it effectively defines the interface between these various programs. It should include some consideration of any user interfaces as well, without going into excessive detail. Any non-functional system requirements (response time, reliability, maintainability, etc.) need to be considered at this stage.[14] The software architecture is also of interest to various stakeholders (sponsors, end-users, etc.) since it gives them a chance to check that their requirements can be met. Design{{Main article|Software design}}The main purpose of design is to fill in the details which have been glossed over in the architectural design. The intention is that the design should be detailed enough to provide a good guide for actual coding, including details of any particular algorithms to be used. For example, at the architectural level, it may have been noted that some data has to be sorted, while at the design level it is necessary to decide which sorting algorithm is to be used. As a further example, if an object-oriented approach is being used, then the details of the objects must be determined (attributes and methods). Choice of programming language(s)Mayer states: "No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes. Understanding the problem and associated programming requirements is necessary for choosing the language best suited for the solution."[15] From Meek & Heath: "The essence of the art of choosing a language is to start with the problem, decide what its requirements are, and their relative importance since it will probably be impossible to satisfy them all equally well. The available languages should then be measured against the list of requirements, and the most suitable (or least unsatisfactory) chosen."[16] It is possible that different programming languages may be appropriate for different aspects of the problem. If the languages or their compilers permit, it may be feasible to mix routines written in different languages within the same program. Even if there is no choice as to which programming language is to be used, McConnell provides some advice: "Every programming language has strengths and weaknesses. Be aware of the specific strengths and weaknesses of the language you're using."[17] Coding standards{{main article|Coding conventions}}This section is also really a prerequisite to coding, as McConnell points out: "Establish programming conventions before you begin programming. It's nearly impossible to change code to match them later."[18] As listed near the end of Coding conventions, there are different conventions for different programming languages, so it may be counterproductive to apply the same conventions across different languages. The use of coding conventions is particularly important when a project involves more than one programmer (there have been projects with thousands of programmers). It is much easier for a programmer to read code written by someone else if all code follows the same conventions. For some examples of bad coding conventions, Roedy Green provides a lengthy (tongue-in-cheek) article on how to produce unmaintainable code.[19] CommentingDue to time restrictions or enthusiastic programmers who want immediate results for their code, commenting of code often takes a back seat. Programmers working as a team have found it better to leave comments behind since coding usually follows cycles, or more than one person may work on a particular module. However, some commenting can decrease the cost of knowledge transfer between developers working on the same module. In the early days of computing, one commenting practice was to leave a brief description of the following:
The "description of the module" should be as brief as possible but without sacrificing clarity and comprehensiveness. However, the last two items have largely been obsoleted by the advent of revision control systems. Modifications and their authorship can be reliably tracked by using such tools rather than by using comments. Also, if complicated logic is being used, it is a good practice to leave a comment "block" near that part so that another programmer can understand what is exactly happening. Unit testing can be another way to show how code is intended to be used. Naming conventionsUse of proper naming conventions is considered good practice. Sometimes programmers tend to use X1, Y1, etc. as variables and forget to replace them with meaningful ones, causing confusion. In order to prevent this waste of time, it is usually considered good practice to use descriptive names in the code since it’s about real data. Example: A variable for taking in weight as a parameter for a truck can be named TrkWeight or TruckWeightKilograms, with TruckWeightKilograms being the more preferable one, since it is instantly recognisable. See CamelCase naming of variables. Keep the code simpleThe code that a programmer writes should be simple. Complicated logic for achieving a simple thing should be kept to a minimum since the code might be modified by another programmer in the future. The logic one programmer implemented may not make perfect sense to another. So, always keep the code as simple as possible.[20] For example, consider these equivalent lines of C code: and and The 1st approach, which is much more commonly used{{dubious|date=December 2017}}, is considerably larger than the 3rd. In particular, it consumes 5 times more screen vertical space (lines), and 97 characters versus 52 (though editing tools may reduce the difference in actual typing). It is arguable, however, which is "simpler". The first has an explicit if/then else, with an explicit return value obviously connected with each; even a novice programmer should have no difficulty understanding it. The 2nd merely discards the braces, cutting the "vertical" size in half with little change in conceptual complexity. In most languages the "return" statements could also be appended to the prior lines, bringing the "vertical" size to only one more line that the 3rd form. The third form obviously minimizes the size, but may increase the complexity: It leaves the "true" and "false" values implicit, and intermixes the notions of "condition" and "return value". It is likely obvious to most programmers, but a novice might not immediately understand that the result of evaluating a condition is actually a value (of type Boolean, or its equivalent in whatever language), and thus can be manipulated or returned. In more realistic examples, the 3rd form could have problems due to operator precedence, perhaps returning an unexpected type, where the prior forms would in some languages report an error. Thus, "simplicity" is not merely a matter of length, but of logical and conceptual structure; making code shorter may make it less or more complex. For large, long lived programs using verbose alternatives could contribute to bloat.{{dubious|date=December 2017}} Compactness can allow coders to view more code per page, reducing scrolling gestures and keystrokes. Given how many times code might be viewed in the process of writing and maintaining, it might amount to a significant savings in programmer keystrokes in the life of the code. This might not seem significant to a student first learning to program. But when producing and maintaining large programs which often reach thousands or even millions of lines, it becomes apparent{{citation needed|date=January 2018}} how much a minor code simplification might speed work{{dubious|date=December 2017}}, and lessen finger, wrist and eye strain, which are common medical issues suffered by production coders and information workers.[21] Terser coding speeds compilation very slightly, as fewer symbols need to be processed. Furthermore, the 3rd approach may allow similar lines of code to be more easily compared, particularly when many such constructs can appear on one screen at the same time. Finally, very terses layouts might better utilize modern wide-screen computer displays.{{citation needed|date=December 2017}} In the past screens were limited to 40 or 80 characters (such limits originated far earlier: manuscripts, printed books, and even scrolls, have for millennia used quite short lines (see for example Gutenberg Bible). Modern screens can easily display 200 or more characters, allowing extremely long lines. Most modern coding styles and standards do not take up that entire width. Thus, if using one window as wide as the screen, a great deal of available space is wasted. On the other hand, with multiple windows, or using an IDE or other tool with various information in side panes, the available width for code is in the range familiar from earlier systems. It is also worth noting that the human visual system is greatly affected by line length; very long lines slightly increase reading speed, but reduce comprehension [https://blog.codinghorror.com/text-columns-how-long-is-too-long/] and add to eye-tracking errors. Some studies suggest that longer lines fare better online than in print , but this still only goes up to about 10 inches, and mainly for raw speed of reading prose. PortabilityProgram code should not contain "hard-coded" (literal) values referring to environmental parameters, such as absolute file paths, file names, user names, host names, IP addresses, URLs, UDP/TCP ports. Otherwise the application will not run on a host that has a different design than anticipated. A careful programmer can parametrize such variables and configure them for the hosting environment outside of the application proper (for example in property files, on an application server, or even in a database). Compare the mantra of a "single point of definition"[22] (SPOD). As an extension, resources such as XML files should also contain variables rather than literal values, otherwise the application will not be portable to another environment without editing the XML files. For example, with J2EE applications running in an application server, such environmental parameters can be defined in the scope of the JVM and the application should get the values from there. Construction guidelines in briefA general overview of all of the above:
Code developmentCode buildingA best practice for building code involves daily builds and testing, or better still continuous integration, or even continuous delivery. Testing{{Main article|Software testing}}Testing is an integral part of software development that needs to be planned. It is also important that testing is done proactively; meaning that test cases are planned before coding starts, and test cases are developed while the application is being designed and coded. Debugging the code and correcting errorsProgrammers tend to write the complete code and then begin debugging and checking for errors. Though this approach can save time in smaller projects, bigger and complex ones tend to have too many variables and functions that need attention. Therefore, it is good to debug every module once you are done and not the entire program. This saves time in the long run so that one does not end up wasting a lot of time on figuring out what is wrong. Unit tests for individual modules, and/or functional tests for web services and web applications, can help with this. Deployment{{Main article|Software deployment}}Deployment is the final stage of releasing an application for users. Some best practices are:[23][24]
See also
References1. ^{{cite book| title = Code Complete| edition = Second| last = McConnell| first = Steve| year = 2004| publisher = Microsoft Press| isbn = 0-7356-1967-0}} 2. ^{{cite book| title = Software Engineering| edition = Seventh| last = Sommerville| first = Ian| year = 2004| publisher = Pearson| isbn = 0-321-21026-3| page = 38}} 3. ^{{cite book| title = Code Complete| edition = Second| last = McConnell| first = Steve| year = 2004| publisher = Microsoft Press| isbn = 0-7356-1967-0| pages = 649–659}} 4. ^{{cite book| title = The Psychology of Computer Programming| edition = Silver anniversary| last = Weinberg| first = Gerald| year = 1998| publisher = Dorset House Publishing, New York| isbn = 978-0-932633-42-2| pages = 128–132}} 5. ^{{cite book| title = Software Engineering| edition = Seventh| last = Sommerville| first = Ian| year = 2004| publisher = Pearson| isbn = 0-321-21026-3| pages = 12–13}} 6. ^{{cite book| title = The Psychology of Computer Programming| edition = Silver anniversary| last = Weinberg| first = Gerald| year = 1998| publisher = Dorset House Publishing, New York| isbn = 978-0-932633-42-2| pages = 15–25}} 7. ^{{cite journal| last = Hoare| first = C.A.R.| title = The Quality of Software| journal = Software Practice and Experience| volume = 2| pages = 103–105| publisher = Wiley| date = 1972| doi=10.1002/spe.4380020202}} 8. ^{{Citation | last = Meek | first = Brian | last2 = Heath | first2 = Patricia | title = Guide to Good Programming Practice | publisher = Ellis Horwood, Wiley | year = 1980 | page = 14}} 9. ^{{cite book| title = Code Complete| edition = Second| last = McConnell| first = Steve| year = 2004| publisher = Microsoft Press| isbn = 0-7356-1967-0| page = 40}} 10. ^{{cite book| title = Code Complete| edition = Second| last = McConnell| first = Steve| year = 2004| publisher = Microsoft Press| isbn = 0-7356-1967-0| page = 36}} 11. ^{{Citation| last = Meek| first = Brian| last2 = Heath| first2 = Patricia| title = Guide to Good Programming Practice| publisher = Ellis Horwood, Wiley| year = 1980| page = 15}} 12. ^{{cite book| title = Software Engineering| edition = Seventh| last = Sommerville| first = Ian| year = 2004| publisher = Pearson| isbn = 0-321-21026-3| pages = 118–123}} 13. ^{{cite journal | last =Hoare | first =C.A.R | title =The Emperor's Old Clothes | journal =Communications of the ACM | volume =24 | issue =2 | pages =75–83 | publisher =ACM | date =1981 | url =http://delivery.acm.org/10.1145/360000/358561/p75-hoare.pdf?ip=86.149.222.132&id=358561&acc=OPEN&key=BF13D071DEA4D3F3B0AA4BA89B4BCA5B&CFID=376760624&CFTOKEN=68991969&__acm__=1383824348_7b82781d1f57e0bdc065e0717663d1c3 | accessdate =7 Nov 2013 | doi =10.1145/358549.358561 }}{{dead link|date=July 2017 |bot=InternetArchiveBot |fix-attempted=yes }} 14. ^{{cite book| title = Software Engineering| edition = Seventh| last = Sommerville| first = Ian| year = 2004| publisher = Pearson| isbn = 0-321-21026-3| pages = 242–243}} 15. ^{{cite book | last = Mayer | first = Herbert | title = Advanced C programming on the IBM PC | publisher = Windcrest Books | date = 1989 | page = xii (preface) | isbn = 0830693637}} 16. ^{{Citation| last = Meek| first = Brian| last2 = Heath| first2 = Patricia| title = Guide to Good Programming Practice| publisher = Ellis Horwood, Wiley| year = 1980| page = 37}} 17. ^{{cite book| title = Code Complete| edition = Second| last = McConnell| first = Steve| year = 2004| publisher = Microsoft Press| isbn = 0-7356-1967-0| page = 70}} 18. ^{{cite book| title = Code Complete| edition = Second| last = McConnell| first = Steve| year = 2004| publisher = Microsoft Press| isbn = 0-7356-1967-0| page = 70}} 19. ^{{cite web| url = http://www.mindprod.com/jgloss/unmain.html| title = unmaintainable code : Java Glossary| author = Roedy Green| accessdate = 2013-11-26 }} 20. ^{{cite web | url= http://docforge.com/wiki/Best_practices | title=Best practices | author=Multiple (wiki) | work=Docforge | accessdate=2012-11-13 }} 21. ^{{cite web|title=Repetitive Strain Injury|url=http://web.eecs.umich.edu/~cscott/rsi.html|accessdate=30 October 2016}} 22. ^See for example:{{cite web | url = http://jeffkemponoracle.com/2010/06/11/single-point-of-definition-by-example/ | title = Single-Point-of-Definition by Example | access-date = 2015-11-30 | quote = 'Don't repeat anything. Aim for a Single Point of Definition for every aspect of your application [...]'.}} 23. ^https://dzone.com/articles/7-application-deployment-best 24. ^https://lwn.net/Articles/562333/ 25. ^blog.fortrabbit.com/multi-stage-deployment-for-website-development 26. ^https://www.wired.com/insights/2013/04/why-30-of-app-deployments-fail/ 27. ^http://emphaticsolutions.com/2009/09/06/the-rules-of-software-deployment.html 28. ^https://airbrake.io/blog/software-development/speed-up-deployment-match-demand 29. ^https://www.linux.com/news/devops-and-art-secure-application-deployment 30. ^https://www.awsarchitectureblog.com/2014/05/organizing-software-deployments-to-match-failure-conditions.html 31. ^http://www.theserverside.com/news/1364556/Best-Practices-for-Risk-Free-Deployment 32. ^http://www.drdobbs.com/effective-software-deployment/184415760 33. ^http://searchitoperations.techtarget.com/tip/Enterprise-application-deployment-The-humanity-of-software-implementation 34. ^https://18f.gsa.gov/2014/05/14/hacking-bureaucracy-improving-hiring-and-software/ 35. ^http://www.intact-tech.com/why-a-bad-software-deployment-is-worse-than-doing-nothing/
External links
2 : Software development process|Computer programming |
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。