Coding Standards and Best Programming Practices By Aseem

Coding Standards and Best Programming Practices By Aseem Sudha INTERACTIVE DIGITAL ARCHTECTS IOMEDIA

Purpose of coding standards and best programming practices • Easy maintenance • Better efficiency

Who cares ? Your Employer Your Co-workers, Present and Future

What happens ? At work , you’re part of a team. Awesome happens when all are at one page.

Anatomy of an Application ➢Modules ➢OOPS Concepts ➢Design Pattern

MODULES It is all about planning the application structure for easy maintenance and reusability.

OOPS Concepts ➢Inheritance continue…

OOPS Concepts ➢Interface

Design Patterns (Observer pattern) ➢Reusability ➢Flexibility ➢Reduce coupling

CODE PATTERN ➢ Variables ➢ Functions ➢ Folders and files ➢ Access Modifiers ➢ Loops ➢ Black Box ➢ Services ➢ Scope of variables ➢ Comments ➢ Modifying Classes / Libraries / Functions

VARIABLES ➢ Use logical names for variables. - Don’t worry about length. - Don’t use names such as “temp” , “my. Loader” , “foo” etc. ➢ Use camel case e. g. “device. Token” ➢ Different naming for public and private variables. - private variable to start with “_” e. g. “_device. Token”. - public variable to start without “_” e. g. “device. Token”. - getters and setters to be named as “get. Name” and “set. Name”. ➢ Avoid interference in global scope. ➢ Constants to named as CONSTANT_VALUES_LIKE_THIS ➢ Events to be named as EVENT_WHATEVER_THAT_EVENT_DOES Code Pattern continue…

VARIABLES ➢ Proper structure of conditional statements e. g. if (condition) { statements } else { statements } ➢ Statements must be easy to read. e. g. if( (name == “abc” && address == “xyz”) ) ➢ For variables in case there is some change write a comment stating the reason for the changes done Comment format : //ASI: 2014_01_14 the text to be written here Code Pattern

FUNCTIONS ➢ Use logical names(camel case) for functions. - Don’t worry about length. e. g. ➢ Function names should be like get. Name() , is. Valid() , get. Name(param. First. Name , param. Last. Name). Code Pattern

FOLDERS AND FILES ➢ Follow the folder structure. ➢ Give meaningful names to the files such as seat. Collection , doctor. Collection. ➢ Give meaningful names to the folder as well such as “collections”. ➢ script. min must be kept parallel to index. html Code Pattern

ACCESS MODIFIERS ➢ Public ➢ Private ➢ Protected Code Pattern

LOOPS ➢ Avoid nested loops. In case , nested loops are used exit when precondition is met. ➢ Use helper methods. ➢ Use most efficient loops such as “_. each” in underscore. js ➢ Use recursive methods. ➢ Stop naming the iterators as “i”, ”j” etc. Code Pattern

BLACK BOX It is a device, system or object which can be viewed in terms of its input, output and transfer characteristics without any knowledge of its internal workings. The implementation is opaque /black. Code Pattern

SERVICES ➢ Understand the whole structure of the service data. ➢ Understand the way it is going to be used in the application i. e. models and collections. ➢ Create sample data. Code Pattern

SCOPE OF VARIABLES / FUNCTIONS ➢ Global ➢ Local Javascript uses the “this” keyword to get a reference to the current execution context. Code Pattern

SCOPE OF VARIABLES / FUNCTIONS var g = “global”; function go(){ var l = “local” function go() { alert(this); } go(); } var obj = { go : function() { alert(this ); } }; obj. go(); alert(l); Code Pattern

COMMENTS ➢ Use Java docs for the comments. ➢ Format to be followed. E. g. /** * Initials : ASI * Description : What this function does * @param 1 : id the error ID that maps to an ID on a web page. * @param 2 : message human readable error. * @returns : what all it returns */ ➢ Each JS file should have a header comment such as this: /** * Description : What this js file does. */ Code Pattern

Modifying Classes / Libraries / Functions Code Pattern

OTHER POINTS TO REMEMBER ➢ Keep functions out of the html. ➢ Stop using short hands. e. g. var x = 3, big = ( x > 10)? “greater 10” : (x < 5 ) ? “less 5” : “btwn 5 and 10; ➢ Manage logs with a boolean. ➢ SVN -Don’t use IDE for committing the code on SVN. -Commit code at regular intervals. ➢ In callbacks , handle the error callback displaying properly styled pop-up with a meaningful message. ➢ Don’t use hard code values in the logic. Use constants. continue…

OTHER POINTS TO REMEMBER ➢ Use try-catch-exception wherever required. ➢ In switch statements use default and break keywords. ➢ Understand the IDE being used. - it might give auto generators e. g. emittor. - Don’t copy the code in the localhost. Use the facilities given by IDE. ➢ Jquery is not everything.

MEMORY and PERFORMANCE There must be a separate destroy function which will do the following things : ➢ Empty the Divs. ➢ Unbind the events. ➢ Clear memory e. g. pano instances.

CODE CHECKS

Questions ?

Thanks http: //aseemsudha. wordpress. com/2014/01/16/javascript-coding-standards/ INTERACTIVE DIGITAL ARCHTECTS IOMEDIA
- Slides: 28