際際滷

際際滷Share a Scribd company logo
Clean
Code
Practices
Parveen Soni
https://github.com/parveen-rx
Agenda
Common Code smells and Code refactoring
Common Code Smells
 Poor Names (mysterious name)
 Poor Naming conventions (meaningless name)
 Poor Method Signatures
 Long Parameter List
 Code comments
 Duplicate code
 Nested Conditions
 Variable Declaration on the Top?
 Magic Numbers
 OOPS Concepts
Poor Names (Mysterious Names)  Code Smell
 Mysterious Names
 SqlDataReader dr1;
 Int od;
 void Button1_click();
 class Page1 {}
 Need to investigate
documentation or usage and then
understand the meaning of the
declaration
 Code fix
 SqlDataReader sqlDataReader;
 Int order;
 void check_availability();
 class ViewCustomerPage {}
 Meaningful from the name itself
Poor Names (Meaningless Names)
 void
beginCheckFunctionality_StoreCl
ientSideCheckBoxIDsArray()
 Need to understand usage and
implementation to get idea of
what method is doing
 Usually, such function have 100
LOC to make it more complex.
 Code fix
 Method such be 20
LOC/30LOC
 Divide into sub-methods and
given short and meaningful
name to each functionality
Poor Method Signature
def user = userService.getUser(username, password,
false)
Fn getUser(String uName, String pass, bool login ) {
if(login){
//get from DB
return user
} else {
//get from redis
return user
}
}
 Code fix
 Divide if else into sub-method to avoid doing
that
Fn getUserFromDB(String uName, String pass) {
//get from DB
return user
}
Fn getUserFromRedis(String uName, String
pass) {
//get from Redis
return user
}
Long Parameter List
checkCurrentNotification
(userName, password, userType
DateTime.now())
checkNotificationForUser
(userName, password, userType)
checkTodayNotificationForUser
(userName, password, userType, 1
day diff)
 Code fix
 Create a subclass with below
property
class User {
String userName
String password
UserTypeEnum userType
}
Code Comments
 Dont write comments, rewrite
the code!
 Dont explain whats (the
obvious)
 Explain whys and hows
Avoid Duplicate Code
String fetchLastLoginInGMT(){
// some date logic
//convert in GMT
return datetime;
}
String fetchLastLogin(){
//some date logic
//convert to usertimezone
return datetime;
}
String fetchLastLogin( TZ tz = GMT) {
//some date logic
//covert to tz if defined
return datetime;
}
* Follow DRY (Dont repeat yourself)
Nested Conditions
1. If() {} else{}
2. If() {
if(){}
}
3. if(a){
if(b) { //set some params}
}
If(c) {
if(b) { //set some params}
}
1. Use ternary operator
2. Combine statements using
AND OR operations
3. Swap orders
if(b){
if(a) { //set some params}
if(c) { //set some params}
}
Nested Conditions (contd.)
 Writing multiple if else
 Overusing ternary operator
 Use switch statement
 Dont overuse  use it with
sense
Variables declaration on the top
Declaration of variables at top
method (){
int count =0;
int sub_count=0;
if() {
if(){
if() {
count =1; sub_count=2;
}
}
}
}
 Declare variables as close to the
first spot that you use them as
possible.
 Declare variables at top when need
to use globally and multiple times.
It's not really anything to do with efficiency
but makes your code much more readable.
The closer a variable is declared to where it
is used, the less scrolling/searching you
must do when reading the code later.
OOPS Concepts
 Abstraction Design Patterns
 Encapsulation
 Polymorphism
 Inheritance
 Association
 Aggregation
 Composition
Thank you

More Related Content

Clean code.pptx

  • 2. Agenda Common Code smells and Code refactoring
  • 3. Common Code Smells Poor Names (mysterious name) Poor Naming conventions (meaningless name) Poor Method Signatures Long Parameter List Code comments Duplicate code Nested Conditions Variable Declaration on the Top? Magic Numbers OOPS Concepts
  • 4. Poor Names (Mysterious Names) Code Smell Mysterious Names SqlDataReader dr1; Int od; void Button1_click(); class Page1 {} Need to investigate documentation or usage and then understand the meaning of the declaration Code fix SqlDataReader sqlDataReader; Int order; void check_availability(); class ViewCustomerPage {} Meaningful from the name itself
  • 5. Poor Names (Meaningless Names) void beginCheckFunctionality_StoreCl ientSideCheckBoxIDsArray() Need to understand usage and implementation to get idea of what method is doing Usually, such function have 100 LOC to make it more complex. Code fix Method such be 20 LOC/30LOC Divide into sub-methods and given short and meaningful name to each functionality
  • 6. Poor Method Signature def user = userService.getUser(username, password, false) Fn getUser(String uName, String pass, bool login ) { if(login){ //get from DB return user } else { //get from redis return user } } Code fix Divide if else into sub-method to avoid doing that Fn getUserFromDB(String uName, String pass) { //get from DB return user } Fn getUserFromRedis(String uName, String pass) { //get from Redis return user }
  • 7. Long Parameter List checkCurrentNotification (userName, password, userType DateTime.now()) checkNotificationForUser (userName, password, userType) checkTodayNotificationForUser (userName, password, userType, 1 day diff) Code fix Create a subclass with below property class User { String userName String password UserTypeEnum userType }
  • 8. Code Comments Dont write comments, rewrite the code! Dont explain whats (the obvious) Explain whys and hows
  • 9. Avoid Duplicate Code String fetchLastLoginInGMT(){ // some date logic //convert in GMT return datetime; } String fetchLastLogin(){ //some date logic //convert to usertimezone return datetime; } String fetchLastLogin( TZ tz = GMT) { //some date logic //covert to tz if defined return datetime; } * Follow DRY (Dont repeat yourself)
  • 10. Nested Conditions 1. If() {} else{} 2. If() { if(){} } 3. if(a){ if(b) { //set some params} } If(c) { if(b) { //set some params} } 1. Use ternary operator 2. Combine statements using AND OR operations 3. Swap orders if(b){ if(a) { //set some params} if(c) { //set some params} }
  • 11. Nested Conditions (contd.) Writing multiple if else Overusing ternary operator Use switch statement Dont overuse use it with sense
  • 12. Variables declaration on the top Declaration of variables at top method (){ int count =0; int sub_count=0; if() { if(){ if() { count =1; sub_count=2; } } } } Declare variables as close to the first spot that you use them as possible. Declare variables at top when need to use globally and multiple times. It's not really anything to do with efficiency but makes your code much more readable. The closer a variable is declared to where it is used, the less scrolling/searching you must do when reading the code later.
  • 13. OOPS Concepts Abstraction Design Patterns Encapsulation Polymorphism Inheritance Association Aggregation Composition