Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Quiz time

Write a program to calculate the


area of a circle.
[User will input the radius]
Answer !!!

public double abc(double a)


{
double b=3.1416;
double m=b* a * a;
return m;
}

Any problem in the above code?

Can u make it better?


How about this?

public double GetArea(double radius)


{
const double pi=3.1416;
double area=pi * radius * radius;
return area;
}

Any problem in the code?

Can u make it even better?


How about this code?

const double pi=3.1416;

public double GetCircleArea(double radius)


{
return pi * radius * radius;
}

Do u think its better than the first one?


In a single page:

public double abc(double a)


{
double b=3.1416;
double m=b* a * a;
return m;
}

const double pi=3.1416;


public double GetCircleArea(double radius)
{
return pi * radius * radius;
}
So?? Whats the point?

"Write code for human, not for machine"


But how can we write good
code?
Characteristics of Good Code

Simplicity
Readability
Modularity
Layering
Design
Efficiency
Elegance
Clarity
So, when your code smells,
change it
What? How would my code
smells? Are you crazy?
Code Smell

A code smell is a hint that something has gone wrong


somewhere in your code
A code smell is a surface indication that usually
corresponds to a deeper problem in the system.
Note that a CodeSmell is a hint that something might be
wrong, not a certainty
It is first coined by Kent Beck
Oohh...Ok..I get it. But what
are code smells?
Some common smells

Inappropriate naming
Unnecessary
Unnecessary Comments
Dead code
Duplicate code
Large method
Large class etc
Inappropriate Naming

Make your code understandable while reading it.


Name the variables meaningfully and clearly.
The name of the variable should express its usage in the
code.
Give method names on the purpose.
Name of the methods should be meaningful and express its
responsibility.
Which is better?

private double a; private double salary;


Which is better?

private double a; private double salary;


Which is better?

public double ABCD() public double GetSalary()


Which is better?

public double ABCD() public double GetSalary()


Unnecessary Comments

Avoid unnecessary commenting


A beautiful code talks by itself
Don't write code for the dumbs but write for another
programmer
Sum odd numbers from 1 to 10
//This method calculates sum of odd numbers from 1 to 10
public double Sum()
{
//Initialize the sum
double s = 0;
for(int i = 1;i <11; i++)
{
//Check for the odd
if(i%2!=0)
{
//Add the odd number with the sum
s+=i;
}
}
}
Another solution of previous problem
public double GetSum()
{
double sum = 0;
for(int count = 1; count <11; count ++)
{
if(IsOdd(count))
{
sum+=count;
}
}
}
public bool IsOdd(double number)
{
return (number %2 != 0);
}
Dead code

Dead code is that code which is executed but not used from
anywhere in the code.
Don't drive your code reviewers crazy !!!
Don't waste your application's cycle time because it slows
down the execution time
Make your code valuable
Ok. I get it. These are the
smells. But how do i remove
these smells from my code?
These now start stinking me
!!!!
Not today. May be in next
week.
Foysal
Programmer
The Databizsoftware Limited
26th May, 2010

You might also like