Вы находитесь на странице: 1из 26

Good Programming Skills

January 20, 2011

TCS SASTRA PUBLIC

Objective

To develop Skills to write programs that are more understandable, following good programming practices Code Refactoring to improve quality of code Introduction to Defensive Programming

TCS SASTRA Public

January 20, 2011

The Importance of Good Programming Skills


The gap between the best software engineering practice and the average practice is very wide perhaps wider than in any other engineering discipline. A tool that disseminates good practice would be important. - Fred Brooks in "The Mythical Man-Month"

In a survey in TCS, 89 Project Leaders & Group Leaders ranked important skills for entrants to ILP as follows:

1. 2. 3. 4. 5. 6.

Good Programming Practices Process Mindset Communication Skills Adaptability & Teamwork Concern for Quality Knowledge of Core Computer Science Topics

TCS SASTRA Public

January 20, 2011

A true project experience


A TCS project well appreciated by client for Correctness On Time Delivery Good project Managements Customer requested enhancements for the well delivered product but later complained that enhancement requests were pending with TCS for months. Root Cause : Poor Understandability of code

Improve Understandability of the code you write !


TCS SASTRA Public

January 20, 2011

Improving Understandability - Advantages


Self-review is easier leading to less defects Debugging takes less time Another developer will find it easier to maintain your program

TCS SASTRA Public

January 20, 2011

1.Follow Standards
All team members following the same standards, makes more understandable your code Follow the ILP coding guidelines and build this habit.

Each project will have its own coding guidelines based on TCS coding guidelines

TCS SASTRA Public

January 20, 2011

A Problem An employee less than 1 year of experience is a fresher.1-2 years of experience is a Junior Engineer.2-5 is a lead engineer.5-8 is a manager.8-15 a senior manager and 15 and above General manager. A fresher is eligible only for basic salary. A Junior Engineer is eligible for HRA as well. A lead engineer is eligible for HRA and LTA. All the managers are eligible for Medical allowances also. General managers are eligible for special allowance along with all the other allowances. Calculate the salary of an employee. HRA is 10% of basic salary, LTA is 2%,Medical allowance 15% and special allowance is 20% of basic salary.

TCS SASTRA Public

January 20, 2011

Can you understand this ?

TCS SASTRA Public

January 20, 2011

2. Refactor to Improve Understandability


A routine should perform one and only one operation. Describe everything the routine does In the routine's name The routine has too many parameters (9) , parameters >7 is not recommended Some of routines parameters unused (age) The routines parameters are poorly ordered

Avoid unnecessary parameters (allow1,allow2,allow3,allow4)

Write Programs for People First, Computers Second

TCS SASTRA Public

January 20, 2011

The routines name doesnt convey anything(getitwork21( )) The routine doesn't have a single purpose (It finds the employee type and computes salary) The routine has a bad layout.
(The physical organization of the code on the page gives few hints about its logical organization)

The variable/parameter names not meaningful. (allow1,e,Type)

The routine isn't documented.


The routine doesn't defend itself against bad data. (If basic salary is given as negative,
it doesn't throw any error )

TCS SASTRA Public

January 20, 2011

Simplify design and code - Start with interface


With people first in mind, refactor the interface to meet the standards discussed void getItwork21(String Type,int allow2,int bSAL,int allow1,int e,int allow3, int allow4,int age,String name)

Better interfaces

Better routine names

Less / only essential parameters

TCS SASTRA Public

January 20, 2011

Refactoring the function body


Better routine name

Single Task

Better layout

TCS SASTRA Public

January 20, 2011

Better variable names

TCS SASTRA Public

January 20, 2011

We did code refactoring to improve understandability, We can also refactor code to remove duplicate code enhance robustness adaptability, re-use or to apply some creative insight.

Refactoring Saves maintenance costs Makes it more comfortable for the maintainer. These factors must be balanced with the effort and time the refactoring. required for

TCS SASTRA Public

January 20, 2011

Specific Refactorings
Data-Level Statement-Level Routine-Level

Source: Code Complete, Second Edition by Steve McConnell

TCS SASTRA Public

January 20, 2011

Specific Refactorings
Data-Level Replace a magic number with a named constant Rename a variable with a clearer or more informative name Move an expression inline Replace an expression with a routine Introduce an intermediate variable Convert a multiuse variable to multiple single-use variables Use a local variable for local purposes rather than a parameter

TCS SASTRA Public

January 20, 2011

Specific Refactorings
Statement-Level Decompose a boolean expression Move a complex boolean expression into a well-named boolean function Consolidate fragments that are duplicated within different parts of a conditional Use break or return instead of a loop control variable Return as soon as you know the answer instead of assigning a return value within nested if-then-else statements

TCS SASTRA Public

January 20, 2011

Specific Refactorings
Routine-Level Extract routine/method Move a routine s code inline Substitute a simple algorithm for a complex algorithm Add/Remove a parameter Separate query operations from modification operations Combine similar routines by parameterizating them Separate routines whose behavior depends on parameters passed in

TCS SASTRA Public

January 20, 2011

Refactor safely
Refactor one at a time Save the code before refactoring Retest after each refactoring Add test cases Review the changes Keep refactoring small Make frequent checkpoints Adjust your approach depending on the risk of the refactoring

Source: Code Complete, Second Edition by Steve McConnell

TCS SASTRA Public

January 20, 2011

3. Comment Judiciously
Every function must provide its purpose For java, Javadoc is a good choice for documentation as - it is generally adequate - easy to understand and use - it is popular

TCS SASTRA Public

January 20, 2011

Improving Understandability
Comment judiciously Sample method comment /** Calculates the Salary based on years of experience and basic salary @param experienceYears - the years of experience of an employee @param basicSalary - the basic salary of an employee @return the total salary of an employee */ public static double calcSalary(int experienceYears,int basicSalary){

TCS SASTRA Public

January 20, 2011

Improving code correctness Why?

Construction accounts for about 75 percent of the errors on small projects and 50 to 75 percent on medium and large projects. Any activity that accounts for 50 to 75 percent of the errors presents a clear opportunity for improvement - Steve McConnell in Code Complete

TCS SASTRA Public

January 20, 2011

Improving Code Correctness - Defending Against Bad data


Defensive programming Similar to defensive driving style! Check the values of all data from external sources Check the values of all routine input parameters Check all return values from all methods to be in the acceptable range Always have an otherwise or default clause in conditional statements to catch those cases that are not expected and so not processed Ensure that your code is not used inappropriately
Source: Code Complete, Second Edition by Steve McConnell

TCS SASTRA Public

January 20, 2011

Defensive Programming
Why defensive programming helps ?
Programming is complex and a major challenge. With the best of intentions, we may still expect problems

Defensive programming techniques help us discover a defect as close as feasible.

TCS SASTRA Public

January 20, 2011

References
Code Complete, Second Edition by Steve McConnell Good Programming Practices & Skills by Abbas K. Sutarwala

TCS SASTRA Public

January 20, 2011

Happy Learning!

January 20, 2011

TCS SASTRA PUBLIC