Академический Документы
Профессиональный Документы
Культура Документы
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
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
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
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.
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)
Better interfaces
Single Task
Better layout
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
Specific Refactorings
Data-Level Statement-Level Routine-Level
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
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
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
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
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
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){
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
Defensive Programming
Why defensive programming helps ?
Programming is complex and a major challenge. With the best of intentions, we may still expect problems
References
Code Complete, Second Edition by Steve McConnell Good Programming Practices & Skills by Abbas K. Sutarwala
Happy Learning!