Upload
praba19
View
221
Download
1
Embed Size (px)
Citation preview
7/28/2019 Good Programming Skills
1/26
January 20, 2011 TCS SASTRA PUBLIC
Good Programming Skills
7/28/2019 Good Programming Skills
2/26
January 20, 2011TCS SASTRA Public2
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
Objective
7/28/2019 Good Programming Skills
3/26
January 20, 2011TCS SASTRA Public3
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"
The Importance of Good Programming Skills
In a survey in TCS, 89 Project Leaders & Group Leaders
ranked important skills for entrants to ILP as follows:
1. Good Programming Practices
2. Process Mindset3. Communication Skills
4. Adaptability & Teamwork
5. Concern for Quality
6. Knowledge of Core Computer Science Topics
7/28/2019 Good Programming Skills
4/26
January 20, 2011TCS SASTRA Public4
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 !
7/28/2019 Good Programming Skills
5/26
January 20, 2011TCS SASTRA Public5
Self-review is easier leading to less defects
Debugging takes less time
Another developer will find it easier to maintain your program
Improving Understandability - Advantages
7/28/2019 Good Programming Skills
6/26
January 20, 2011TCS SASTRA Public6
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
7/28/2019 Good Programming Skills
7/26
January 20, 2011TCS SASTRA Public7
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 managersare 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.
7/28/2019 Good Programming Skills
8/26
January 20, 2011TCS SASTRA Public8
Can you understand this ?
7/28/2019 Good Programming Skills
9/26
January 20, 2011TCS SASTRA Public9
A routine should perform one and only one operation.
Descr ibe everyth ing the rout ine do esIn 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)
2. Refactor to Improve Understandability
Write Programs for People First, Computers Second
7/28/2019 Good Programming Skills
10/26
January 20, 2011TCS SASTRA Public1
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 givesfew 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 )
7/28/2019 Good Programming Skills
11/26
January 20, 2011TCS SASTRA Public1
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
7/28/2019 Good Programming Skills
12/26
January 20, 2011TCS SASTRA Public1
Refactoring the function body
Better routine name
Single Task
Better layout
7/28/2019 Good Programming Skills
13/26
January 20, 2011TCS SASTRA Public1
Better variable names
7/28/2019 Good Programming Skills
14/26
January 20, 2011TCS SASTRA Public1
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 required for
the refactoring.
7/28/2019 Good Programming Skills
15/26
January 20, 2011TCS SASTRA Public1
Specific Refactorings
Data-Level
Statement-Level
Routine-Level
Source: Code Comp lete, Second Edit ion b y Steve McConn el l
7/28/2019 Good Programming Skills
16/26
January 20, 2011TCS SASTRA Public1
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
7/28/2019 Good Programming Skills
17/26
January 20, 2011TCS SASTRA Public1
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 withinnested if-then-else statements
7/28/2019 Good Programming Skills
18/26
January 20, 2011TCS SASTRA Public1
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
7/28/2019 Good Programming Skills
19/26
January 20, 2011TCS SASTRA Public1
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 Com plete, Second Edit ion by Steve McConnel l
7/28/2019 Good Programming Skills
20/26
January 20, 2011TCS SASTRA Public2
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
3. Comment Judiciously
7/28/2019 Good Programming Skills
21/26
January 20, 2011TCS SASTRA Public2
Comment jud ic ious ly Sample method comment
/**
Calculates the Salary based on years of experienc e and b asic salary
@param experienc eYears - the years of experience of an emplo yee
@param basicSalary - the basic salary of an emp loyee
@return the tota l salary o f an emp loyee
*/
publ ic stat ic do ub le calcSalary(int experienceYears, int basicSalary){
Improving Understandability
7/28/2019 Good Programming Skills
22/26
January 20, 2011TCS SASTRA Public2
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
Improving code correctness Why?
7/28/2019 Good Programming Skills
23/26
January 20, 2011TCS SASTRA Public2
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 Sourc e: Code Comp lete, Second Edit ion by Steve McConnell
Improving Code Correctness - Defending Against Bad data
7/28/2019 Good Programming Skills
24/26
January 20, 2011TCS SASTRA Public2
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 disc over a defect asclos e as feasible.
Defensive Programming
7/28/2019 Good Programming Skills
25/26
January 20, 2011TCS SASTRA Public4
Code Comp lete, Second Edit ion by Steve McConnel l
Good Programm ing Pract ices & Ski l ls by Abbas K. Sutarwala
References
7/28/2019 Good Programming Skills
26/26
January 20, 2011 TCS SASTRA PUBLIC
Happy Learning!