The document discusses common code smells and refactoring practices. It provides examples of code smells such as poor naming conventions, long parameter lists, duplicate code, nested conditions, and variable declarations. It then provides recommendations on how to refactor code to address these smells. This includes using meaningful names, splitting methods, avoiding unnecessary comments, following DRY principles, reducing nesting, and declaring variables closer to use. OOPs concepts like abstraction, encapsulation, polymorphism are also mentioned. The overall document aims to educate developers on identifying and fixing bad code smells to improve code quality.
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.