Coding conventions

download Coding conventions

of 35

  • date post

    19-May-2015
  • Category

    Education

  • view

    699
  • download

    0

Embed Size (px)

description

Coding Standard, Coding Best Practices

Transcript of Coding conventions

  • 1. CODE STANDARDS &BEST PRACTICESMd. Ibrahim Rashid [ irashid.com ]

2. Good Code Vs. Bad Code 3. Good Code Vs. Bad CodeThe best applications are coded properlyThis sounds like an obvious statement, but byproperly, I mean that the code not only does itsjob well, but is also easy to add to, maintain anddebug. 4. Ask Yourself? Is your code well organized and maintainable? Is you code well documented? 5. General Practices Naming Conventions Indentation Brace Style Commenting Code consistency Readability Vs. Compression 6. What should coding standardsprovide? File, class, variable naming conventions Code formatting conventions Guidelines for consistency across the code Uniformity 7. Naming Conventions Class names are MixedCase [ ex. MyClass ] Method names are camelCase [ ex. myMethod() ] Constants are ALL_CAPS [ MY_CONSTANT ] Properties and variables are camelCase [ ex. myMethod() ] Non-public class members are _underscorePrefixed [Ex. _myPrivateVariable ] 8. Various ConventionsClass namesMyClassMethod names my_function()myFunction()MyFunctionConstantsMY_CONSTANTProperties and my_variablemyVariablevariablesNon-public class _my_private_variable _myPrivateVariablmembers eFilenamesMyFile.php myFile.phpmy_file.phpClass FilenamesClassMyFile.phpclassMyFile.php class_my_file.phpCase insensitive : MyFile.php and myfile.php are same inwindows 9. Example 10. Scenario 1 11. Scenario 2 12. Scenario 3 13. Senerio 4(!BAD Dont MIX) 14. IndentationPHP(Drupal) Wordpress(PHP) C(K&R standard)Use an indent of 2 Use real Tab = 4spaces, with notabs and notspacestabs spaces, 15. Indentation 16. Indentation Always MakeProper Indent. 17. Indentation Use of Real Tabs, 4 space as Tabs, onlyspaces iscontroversial. Its because same source code loaded intodifferent editors with distinct setting will notlook alike. Use lines less than 80 characters. 18. Indentation Brace Style 19. Indentation Brace Style 20. Indentation Use of Real Tabs, 4 space as Tabs, onlyspaces iscontroversial. Its because same source code loaded intodifferent editors with distinct setting will notlook alike. Use lines less than 80 characters. 21. Indentation 22. Commenting Always try to put comments on your code. 23. Commenting 24. Commenting 25. Commenting 26. Self documenting code Use of Long Method Name that reflects thepurpose of the method. Still It needs to be commented. 27. Code Readability Always try to make readable code. 28. Readability Vs. Compression YES!! I saved lots of bytes. Code is now compact.There are lots of tools for making code compact. You dont have to write inunreadable compact form. 29. Code consistency Let a project has 3 members. They watch this slide very carefully , andrealized the importance of coding standard &best practices. Now, they are told to do the project perfectly. Each members uses his/her coding conventionand submitted the project. What will be the output ? 30. Code consistency Always use same standard throughout aproject. All members of a project must choose a fixedconvention before starting a project. 31. Learn from Others Dont invent your own standard. All of theissues have already been debated to deathby many others. Use an established standardMinimize politics by choosing an externalstandard Choose a standard compatible with the librariesyou use Use the standard as a requirement whenoutsourcing Stick to the standard you establish, dont mix 32. How To Write Unmaintainable CodeEnsure a job for life ;-) Read This Site Carefully with negating everyconcept. http://thc.org/root/phun/unmaintain.html 33. References http://wiki.mozilla.org/WebDev:FrontendCodeStandards http://na.isobar.com/standards/ http://en.wikibooks.org/wiki/C%2B%2B_Programming/Programming_Languages/C%2B%2B/Code/Style_Conventions http://en.wikipedia.org/wiki/Best_Coding_Practices http://codex.wordpress.org/WordPress_Coding_Standards http://drupal.org/coding-standards Java Code Conventions http://www.sitepoint.com/coding-standards/ http://www.programming4scientists.com/2008/09/26/good-code-bad-code-an-example/ http://thc.org/root/phun/unmaintain.html