Best Practices in Coding

Slides:



Advertisements
Similar presentations
Coding Standard: General Rules 1.Always be consistent with existing code. 2.Adopt naming conventions consistent with selected framework. 3.Use the same.
Advertisements

Coding Standards for Java An Introduction. Why Coding Standards are Important? Coding Standards lead to greater consistency within your code and the code.
Programming Logic and Design Fourth Edition, Introductory
Object Oriented Design An object combines data and operations on that data (object is an instance of class) data: class variables operations: methods Three.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
Modules, Hierarchy Charts, and Documentation
ASP.NET Programming with C# and SQL Server First Edition
SM3121 Software Technology Mark Green School of Creative Media.
Lecture 20 Coding Standards Tools for Debugging 1.
1 Shawlands Academy Higher Computing Software Development Unit.
Comp 245 Data Structures Software Engineering. What is Software Engineering? Most students obtain the problem and immediately start coding the solution.
Best Practices in Coding Some basic issues in every development team –fundamental discipline issues in coding –number of years of exp. does not match delivery.
Jun 16, 2014IAT 2651 Debugging. Dialectical Materialism  Dialectical materialism is a strand of Marxism, synthesizing Hegel's dialectics, which proposes.
1 Debugging and Testing Overview Defensive Programming The goal is to prevent failures Debugging The goal is to find cause of failures and fix it Testing.
Chapter 13: Implementation Phase 13.3 Good Programming Practice 13.6 Module Test Case Selection 13.7 Black-Box Module-Testing Techniques 13.8 Glass-Box.
GCSE OCR 3 A451 Computing Professional standards
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
CSC 395 – Software Engineering Lecture 12: Reusability –or– Programming was Bjarne Again.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
CS 350, slide set 5 M. Overstreet Old Dominion University Spring 2005.
Software Testing and Maintenance 1 Code Review  Introduction  How to Conduct Code Review  Practical Tips  Tool Support  Summary.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
The Software Development Process
Design - programming Cmpe 450 Fall Dynamic Analysis Software quality Design carefully from the start Simple and clean Fewer errors Finding errors.
INFO 637Lecture #71 Software Engineering Process II Product Implementation INFO 637 Glenn Booker.
Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging.
Principles of Programming. Achieving an Object-Oriented Design  Abstraction and Information Hiding  Object-Oriented Design  Functional Decomposition.
Introduction to Programming and App Inventor. Introduction What is a computer program? Introducing App Inventor Getting hands on with App Inventor.
Copyright © 2016 Curt Hill Static Code Analysis What it is and does.
Lecture 1b- Introduction
Copyright © Jim Fawcett Spring 2017
Effective Time Management
CSc 1302 Principles of Computer Science II
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
Lesson #6 Modular Programming and Functions.
GC101 Introduction to computers and programs
Lesson #6 Modular Programming and Functions.
CPSC 315 – Programming Studio Spring 2012
Software engineering – 1
Introduction to Computers
© 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Repeating code We could repeat code we need more than once: i = 1 print (i) i += 1 print (i) #… stop when i == 9 But each line means an extra line we might.
Design and Maintenance of Web Applications in J2EE
Lesson #6 Modular Programming and Functions.
Agenda Database Development – Best Practices Why Performance Matters ?
Designing and Debugging Batch and Interactive COBOL Programs
Unit# 9: Computer Program Development
Objects First with Java
Software Testing and Maintenance Modifying Code
Chapter 4 void Functions
Portability CPSC 315 – Programming Studio
Chapter 13 Quality Management
Computer Science Testing.
Programming.
Applied Software Project Management
Introduction When searching for a new mattress, you have to make sure you know where to go to find the best one. The mattress you sleep on is going to.
Lesson #6 Modular Programming and Functions.
Topic 5: Communication and the Internet
A Level Computer Science Exam Technique
Tonga Institute of Higher Education IT 141: Information Systems
An Introduction to Debugging
CMSC 345 Programming.
Configuration management
Tonga Institute of Higher Education IT 141: Information Systems
Coding practices For IT and Computing students 2014.
Arrays.
Programming Logic and Design Eighth Edition
Chapter 1: Creating a Program.
Design Contracts and Errors A Software Development Strategy
Presentation transcript:

Best Practices in Coding

Some basic issues in every development team fundamental discipline issues in coding number of years of exp. does not match delivery by the time they actually become good coders, they are promoted to leads 70% of the whole employees, belong 0-4 years of experience In India, senior developer means 3-5 years of exp, in USA, senior developer means 6- 10 years of exp – There is a huge gap in expectation and quality Coding is not rocket science The rework time in coding is the actual killer 20-25% of the time is spent in reworking in software – that means, fix the mistakes done

Coding Practices - Introduction In a mobile phone, can you insert sim card wrongly? You cannot. Because the sim card design is like that. So the highest level maturity is not to give an opportunity to make mistakes As a kid, you were asked and helped to brush the teeth. It was tough to brush teeth. One fine morning, you start brushing teeth by yourselves. Then it has become a habit. You need not be told to do brushing by anyone.

Commenting Commenting is necessary for maintaining any program Without commenting, the program is not complete Do not assume that the other person who reads this program will understand it clearly If commenting is not done at the beginning, it is forgotten. Program - a physical file – you need to comment the following at the top. at the top of the program - header comment what the program achieves who coded on what day Classes Comment at the beginning of the class about what the class does class Employee // this class is used to manage all information // about employees and retrieve specific data Methods within classes what this method/function/procedure does what are the parameters and their purpose what are the return values Comment before every loop. Comment before every file or database operation Comment before every if condition

Commenting If you are team leader, if your team member comes with a code, that does not have these comments – what will you do? You take over a program for maintenance from another person, and that program has no comments – what will you do? Your company hands over a project to client. Programs do not have comments. What the client will say?

Naming Conventions Readability improves understanding; that further improves maintainability If programs are not consistent, maintenance is tough If 20 developers work in a project and each one names functions in the way he/she wants, will the client approve the same? We must have a document on naming conventions Every document must be named properly and it must be stored in a proper folder.

Naming Conventions You need to have a convention for naming Programs Classes Methods Variables Labels in programs Reusable library functions There are established methods like Pascal casing, camel casing etc. Usually you will tend to use a mixture of uppercase, lowercase letters, underscore etc The ultimate aim is to achieve consistency across programs

Avoid Hard coding Placing a number or quoted text inside active code is hard coding This is deadly as the program restricts itself to this hard coded value To make a change, you need to change code, recompile and redeploy Usual way to avoid hard coding is to use constants at the top of a program; here also, you need to edit the program, but the change is in one place Other way is to put all configurable values in a csv or ini or dat file. Every program must read the name value pair from the file and later use them in the code Eg. MAX_LENGTH 150, PORT 8097

Modularity of code Modularity is important for easy maintenance Do not write lengthy methods or procedures We can split the modules based on A functional operation as per requirements A piece of code is used in more than one place Logical breakdown of events in the functionality Modularity must be decided right at the design level itself Modular programs are easy to debug Fixes done on modular programs are easy to isolate from other regression effects

Examine the loops A loop is - repeat some logic for specific number of times or as long as a condition is true/false If this is not checked, it can run forever, infinitely and can bring down the server Loop Sample gateway 1 // variables, flags xyz = 10 loop starts here ...gateway 2 // variables, flags, counters .. logic ... gateway 3 loop ends here gateway 4 Gateways are the check posts where we must watch the value of the variables, loop counters and loop flags Ensure proper resetting of flags and counters and check their values and gateways of the loops Never allocate memory inside a loop Never instantiate a class inside a loop; if needed, close it within the loop Usually companies do not suggest more than 3 levels in the loops

Exceptions Issues may be caused by programmer Issues may be caused by system Most of the languages allow try-catch or on-error-goto exceptions Exception is also an error condition we do not know when it will happen 1. Any file operations – handle exceptions, because file may not exist file is already opened by someone file does not have privilege file is already full 2. Any database operation – handle exceptions, because database you may not have rights database is down connections exhausted 3. Memory low memory exception pointers - writing in privileged memory array boundary breach 4. Any external evices your program accesses webcam or printer

Performance We need to monitor cpu, memory, network Memory is directly related to variables and object – do not declare huge arrays or objects Cpu is consumed more when you deal with db or files or devices Any database operations, open late and close the connection early. Usage of indexes in queries must be examined Any column used in where condition of db query must have index on it Any memory allocated must be freed – else the program will shut down after some time Any object instantiated must be released – else system will be depleted of memory and hence performance will come down

Program Logic Usually the logic design will be provided to the developer If it is not provided, take 30 minutes and write the logic of the program in English first Do not start coding right away. You will make so many assumptions and it will spoil the show Get the logic approved by the team lead and then start coding Developers usually feel that this takes time; but it reduces the time effectively while coding and reworking Since developers are not used to documenting, they feel it is not their job. Hence they miss a lot of finer points If you write the logic first, you will get a lot of clarifications at that time itself and hence the code will come out clean

What are Coding Standards Coding standards are guidelines for code style and documentation. The dream is that any developer familiar with the guidelines can work on any code that followed them. Standards range from a simple series of statements to involved documents.

Areas Typically Covered Program Design Naming Conventions Formatting Conventions Documentation Possibly Even Licensing

Why Have Coding Standards Greater consistency between developers Easier to develop and maintain Saves time and money

Prime Directive Document every time you violate a standard. No standard is perfect for every application, but failure to comply with your standards requires a comment

Ambler’s Law of Standards Industry Standards > organizational standards > project standards > no standards The more commonly accepted a standard the easier it is for team members to communicate Invent standards when necessary, but don’t waste time creating something that you won’t be able to use later. All languages have recommended coding standards available. It is well worth your effort to find and use industry standards Push for organizational standards whenever possible

Good Coding Style:Revisit 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 what

Member Functions Documentation Accessors use getVar() and setVar() functions on all class variable unless class is being used solely as a data 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 order

Three Rules Coding standards needn’t be onerous - find a standard that works for your team. Standardize early - the effort to bring your old work into the standard will be too great otherwise. Encourage a culture where standards are followed.

Thanks..