14/11/20181Coding StandardsIn PythonWeek 8What are Coding Standards• Coding standards are guidelines for code styleand documentation.•The dream is that any developer familiarwith the guidelines can work on any codethat followed them.•Standards range from a simple series ofstatements to involved documents.14/11/20182Coding StandardsIts offers advantages to;For the developers:For the quality assurance team:For the project managers:Areas Typically Covered• Program … Continue reading “Coding Standards In Python | My Assignment Tutor”
14/11/20181Coding StandardsIn PythonWeek 8What are Coding Standards• Coding standards are guidelines for code styleand documentation.•The dream is that any developer familiarwith the guidelines can work on any codethat followed them.•Standards range from a simple series ofstatements to involved documents.14/11/20182Coding StandardsIts offers advantages to;For the developers:For the quality assurance team:For the project managers:Areas Typically Covered• Program Design• Naming Conventions• Formatting Conventions• Documentation• Possibly Even Licensing14/11/20183For the developers:1.The source code will be more comprehensive and will becomeeasy-to-maintain. As the programmers became more and morefamiliar with the coding style as they implements the codingstandards on project after project.2.The uniform approach for solving problems will be handybecause the code standards documents reveal the recommendedmethods that were tried and tested on the earlier projects.3.Less communication between developers and managers will beneeded because the programmers will not asked anymore on thedetails of the specification document because the defaults are allstated in coding standards.4.Is common to the less experience programmer to re-invent thewheel. When there are coding standards, there is a big chancethat particular problem is not really a new problem, but in fact, asolution may be documented before.For the quality assurance team:• Well documented coding standards will aid the creationof “Test Scripts”. Having reviewed the source code andtested an application based on compliance to codingstandards, it added strong direction to ensure quality of thesoftware product.• Because code standards implements traceability, the itemids can be used to describe a violation in the “Test Results”document that both developers and testers are familiarwith.14/11/20184For the project managers:It is important for the project managers to maintain and securesource code quality on their projects. Implementing codingstandards could jumpstart this goal halfway to its realization.Repeated performance pitfalls could be avoided. It is a commoncase that a released software product could be less impressivewhen it comes to performance when the real data has beenloaded in the new developed database application.Lesser man-hour consumption as the sum of all effortsimplementing coding standards.It is also beneficial for the organization who are applying for ISO9001 license because coding standards is a complement fromorganization’s execution plan requirements.Why Have Coding Standards• Greater consistency between developers• Easier to develop and maintain• Saves time and money14/11/20185Prime Directive• Document every time you violate a standard.• No standard is perfect for every application, but failureto comply with your standards requires a commentAmbler’s Law of Standards• Industry Standards > organizational standards> project standards > no standards• The more commonly accepted a standard the easierit is for team members to communicate• Invent standards when necessary, but don’t wastetime creating something that you won’t be able touse later.• All languages have recommended coding standardsavailable. It is well worth your effort to find and useindustry standards• Push for organizational standards whenever possible14/11/20186Coding standards in PythonString formatting• Don’t use the old %s style string formatting, e.g. “i ama %s” % sub. This kind of string formatting is nothelpful for internationalization.• Use the new .format() method instead, and givemeaningful names to each replacement field, forexample:14/11/20187Layout in PythonIndentation: Use 4 spaces per indentation level.•Continuation lines should align wrapped elements eithervertically using Python’s implicit line joining insideparentheses, brackets and braces, or using a hanging indent.When using a hanging indent the following should beconsidered; there should be no arguments on the first line andfurther indentation should be used to clearly distinguish itselfas a continuation line.Example•# Aligned with opening delimiter.•foo = long_function_name(var_one, var_two,• var_three, var_four)•# More indentation included to distinguish this from the rest.•def long_function_name(•••var_one, var_two, var_three,var_four):print(var_one)Maximum Line Length• Limit all lines to a maximum of 79 characters.• For flowing long blocks of text with fewer structuralrestrictions (docstrings or comments), the line lengthshould be limited to 72 characters.14/11/20188Blank Lines•Surround top-level function and classdefinitions with two blank lines.•Method definitions inside a class aresurrounded by a single blank line.•Extra blank lines may be used (sparingly)to separate groups of related functions.Blank lines may be omitted between abunch of related one-liners (e.g. a set ofdummy implementations).•Use blank lines in functions, sparingly, toindicate logical sections.Whitespace in Expressions andStatements• Avoid extraneous whitespace in the following situations:• Immediately inside parentheses, brackets or braces.••Yes: spam(ham[1], {eggs: 2})No: spam( ham[ 1 ], { eggs: 2 } )• Between a trailing comma and a following close parenthesis.••Yes: foo = (0,)No: bar = (0, )• Immediately before a comma, semicolon, or colon:••Yes: if x == 4: print x, y; x, y = y, xNo: if x == 4 : print x , y ; x , y = y , x14/11/20189Comments• Comments that contradict the code are worse than nocomments. Always make a priority of keeping the commentsup-to-date when the code changes!• Comments should be complete sentences. The first wordshould be capitalized, unless it is an identifier that beginswith a lower case letter (never alter the case of identifiers!).• Block comments generally consist of one or more paragraphsbuilt out of complete sentences, with each sentence endingin a period.• You should use two spaces after a sentence-ending period inmulti- sentence comments, except after the final sentence.• When writing English, follow Strunk and White.Descriptive: Naming StylesThere are a lot of different naming styles. It helps to beable to recognize what naming style is being used,independently from what they are used for.• Don’t use like single character as variables name.• Try to use meaningful names like Sum_of_NumbersFunction and Variable Names• Function names should be lowercase, with wordsseparated by underscores as necessary to improvereadability.• Variable names follow the same convention as functionnames.• mixedCase is allowed only in contexts where that’salready the prevailing style (e.g. threading.py), to retainbackwards compatibility.14/11/201810Use of ExceptionsGood Coding Style•Names• Use full English descriptors• Use mixed case to make names readable• Use abbreviations sparingly and consistently• Avoid long names• Avoid leading/trailing underscores•Documentation• Document the purpose of every variable• Document why something is done not just what14/11/201811• Accessors• use getVar() and setVar() functions on all classvariable unless class is being used solely as adata structure (OOP)•Member Functions Documentation• What and why member function does what it does• Parameters / return value• How function modifies object• Preconditions /Postconditions• Concurrency issues• Restrictions•Internal Documentation• Control Structures• Why as well as what the code does• Difficult or complex code• Processing orderYour Culture should be this 3 Rules• Coding standards needn’t be onerous – find a standardthat works for your team.• Standardize early – the effort to bring your old work intothe standard will be too great otherwise.• Encourage a culture where standards are followed.