Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Software Construction: Identifying Bed Smell Signs in The Code

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

SOFTWARE CONSTRUCTION

SC SEMESTER PROJECT

IDENTIFYING BED SMELL SIGNS IN THE CODE


In computer programming, a code smell is any characteristic in the source
code of a program that possibly indicates a deeper problem. Determining what is and is not a
code smell is subjective, and varies by language, developer, and development methodology.
Code smells are usually not bugs; they are not technically incorrect and do not prevent the
program from functioning. Instead, they indicate weaknesses in design that may slow down
development or increase the risk of bugs or failures in the future. Bad code smells can be an
indicator of factors that contribute to technical debt. They are a set of common signs which
indicate that your code is not good enough and it needs refactoring to finally have a clean
code.

CODE:
package im;
/************* library for scanning input ***************/
import java.util.Scanner;
/* * * * ******************************************************* * * * *
******************************* CLASS STARTS ***************************
* * * * ******************************************************* * * * */
public class IM
{
public static void main(String[] args)
{
SC Semester Project

// main method
Signal s = new Signal();
s.Display();
}
};
/* * * * ******************************************************* * * * *
******************************* CLASS ENDS ***************************
* * * * ******************************************************* * * * */
/* * * * ******************************************************* * * * *
******************************* CLASS STARTS ***************************
* * * * ******************************************************* * * * */
class Signal
{
// to scan input values from user
Scanner sc = new Scanner(System.in);
// final: variable value is fixed
// NIL defined for initial setup of the incubator monitor
public static final int NIL = -999;
public static final int M = 10;
public static final int m = -10;
public int at=NIL;
public int rt=NIL;
public int o;
int ci;
int cd;
int gt;
boolean t;
int tin;
boolean r;
// contains all main the processing of code

1
SC Semester Project

void Display()
{
Set_IT(); // this method is called first when the code runs
String c="y";
do // for main menu
{
do // for making selection from 1 - 5
{
/************************ MAIN MENU ***********************/
System.out.println("\n\t **** IncubatorMonitor Tester ****\n");
System.out.println("1. Request Change In The Temperature");
System.out.println("2. Increase 1°C Temperature");
System.out.println("3. Decrease 1°C Temperature");
System.out.println("4. Get Actual Temperature");
System.out.println("Select any option: 1 - 4");
System.out.println();
o = sc.nextInt();
System.out.print("\n\n");
switch(o) // for making selection from 1 - 5
{
case 1:
// request change in temperature
rCH();
System.out.println("\nDo you want to continue: (y/n)");
c=sc.next();
break;
case 2:
// increase temperature
ci = Get_at();
if(ci<10 && ci!=NIL)

2
SC Semester Project

{
ci = Inc();
System.out.println("Temperature is increased...");
}
else
{
// if temperature is already at the maximum point
System.out.println("Cannot increase temperature...");
}
System.out.println("\nDo you want to continue: (y/n)");
c=sc.next();
break;
case 3:
// decrease temperature
cd = Get_at();
if(cd>-10 && cd!=NIL)
{
cd = Dec();
System.out.println("Temperature is decreased...");
}
else
{
// if temperature is already at the minimum point
System.out.println("Cannot decrease temperature...");
}
System.out.println("\nDo you want to continue: (y/n)");
c=sc.next();
break;
case 4:
// returns the cuurent temperature of the incubator monitor

3
SC Semester Project

gt = Get_at();
System.out.println(gt);
System.out.println("\nDo you want to continue: (y/n)");
c=sc.next();
break;
default :
// if user selected a number other than 1,2,3,4 or 5
System.out.println("You have Selected an Invalid Option.\n\n");
System.out.println("\nDo you want to continue: (y/n)");
c=sc.next();
}
}
// inner loop ends
while(o !=5);
}
// outer loop ends
while(c=="y" || c=="Y");
}
/*************************************************************************/
/******************************* METHODS START ***************************/
/*************************************************************************/
public boolean range(int t)
{
//this method checks if the temperature is in specified range
if(t >=m && t <= M)
{
return true;
}
else
{

4
SC Semester Project

return false;
}
}
// method
public void Set_IT()
{
// this method sets the initial temperature of the incubator monitor as soon as it starts
System.out.println();
System.out.println("Enter Initial Temperature: ");
System.out.println();
at = sc.nextInt();
t = range(at);
if(t==true)
{
// if valid input is inserted
System.out.println("Initial Temp has been set...");
}
else
{
// if the input is other than specified values or range
System.out.println("Invalid entry....");
}
}
// method
public int Get_at()
{
// returns actual temperature of the incubator monitor
return at;
}
// method

5
SC Semester Project

public void rCH()


{
// user sets the temperature as he/she wants
System.out.println();
System.out.print("Enter the temperature you want to set: ");
System.out.println();
tin=sc.nextInt();
r = range(tin);
rt = tin;
if(rt>at && r==true && at!=NIL)
{
do
{
at++;
}
while(at!=rt && r==true);
System.out.println("Temperature has been changed...");
System.out.println("Now temperature is "+at+"°C");
}
else if(rt<at && r==true && at!=NIL)
{
do
{
at++;
}
while(at!=rt && r==true);
System.out.println("Temperature has been changed...");
System.out.println("Now temperature is "+at+"°C");
}
else if(rt==at)

6
SC Semester Project

{
nothing();
}
}
// method
public int Inc()
{
// increase temperature by 1°C
at++;
return at;
}
// method
public int Dec()
{
// decrease temperature by 1°C
at--;
return at;
}
// method
public int nothing()
{
// do nothing and just returns the actual temperature of the incubator monitor
int at = Get_at();
return at;
}
};
/* * * * ******************************************************* * * * *
******************************* CLASS ENDS ***************************
* * * * ******************************************************* * * * */

7
SC Semester Project

8
SC Semester Project

9
SC Semester Project

10
SC Semester Project

BAD SMELL SIGNS:

1) Clean code doesn’t contain duplication:


Duplicate code used in increment, decrement, and request change method.

2) Structure less:
Code is not organized, all variables, methods and important points are defined in
one class.

3) Dispensables:
Code is filled with explanatory comments. (If you feel that a code fragment can’t
be understood without comments, try to change the code structure in a way that makes
comments unnecessary.)

4) Use intention revealing names (variables):


Variable names are not intention revealing e.g. m, M, c, at, rt, etc. they should be
named so that one can easily understand their role in the code.

5) Use descriptive names (methods):

11
SC Semester Project

Method name should be descriptive. But here, the method names used in the code
are rather for the programmer ease instead of developing understandability for the user
e.g. Set_IT, Get_at, rCH, Inc, Dec, etc.

6) Nomenclature convention:
Nomenclature convention is not being properly followed especially for the
methods. Class name should be named after a noun and method name after a verb.

7) Bloaters:
Long code in a single class is also a bad smell sign. Here Signal class contains all
the processing. It is a bad approach in programming.

8) Vertical openness between components:


No vertical formatting is implemented. No space is inserted between different
components, so the code looks congested and so complicated to understand.

9) Variable declaration:
Variable should be declared near to its usage. But in the given code this rule is
being neglected e.g. ci, cd, gt, tin, and r are the temporary variables, but they are declared
at the start of the class. This is not necessary also it occupies extra space in the memory.

10) Blocks and indentation:


Indentation is not applied on the code. So it is difficult to understand where a
class starts, where a method starts and ends, from where different statements start or
where they end. In short, code is not organized and formatted well.

REFACTORING THE CODE


Refactoring is a systematic process of improving code without creating new
functionality that can transform a mess into clean code and simple design. The concept is to
improve the internal structure of an existing program's source code, while preserving its external
behavior. 

12
SC Semester Project

IMPROVEMENTS:
1) Clean code doesn’t contain duplication:
The process of increment and decrement in the requested temperature method is
done by calling the functions specified for the purpose.

2) Structure less:
An abstract class is implemented, all necessary variables and methods are
declared there to give users an idea about the basic structure of the code and to improve
the code functionality.

3) Dispensables:
Unnecessary explanatory comments are removed. Code is structured in the way
that makes comments unnecessary. Anyone can easily understand the functionality of
code from the improvised version without comments.

4) Use intention revealing names (variables):


Variable names are changed and are named with the intention revealing words,
e.g. “MinTemp” is used for minimum temperature instead of ‘m’, “MaxTemp” is used
for maximum temperature instead of ‘M’, “Choice” is used for choosing yes or no to
proceed instead of ‘c’, “ActTemp” is used for actual temperature instead of ‘at’,
“ReqTemp” is used for requested temperature instead of ‘rt’, etc.

5) Use descriptive names (methods):


Methods’ names are changed and the names that they describe their functionality
are used, e.g. range is renamed with “InRange”, Set_IT is renamed with
“SetInitialTemp”, Get_at is renamed with “GetActualTemp”, rCH is renamed with
“RequestChange”, Inc is renamed with “Increment”, Dec is renamed with “Decrement”,
nothing is renamed with “Do_Nothing”, etc.

6) Nomenclature convention:
Nomenclature convention is properly followed. Class name is named after a noun
and method are named after a verb e.g. class names “Signal”, “IncubatorMonitors” are
noun and the methods names “SetInitialTemp”, “GetActualTemp”, “Increment”, etc are
verb.

7) Bloaters:
A part of the code from Signal class is now being processed in the abstract class.

8) Vertical openness between components:

13
SC Semester Project

Vertical formatting is implemented, space is inserted between different


components and the code is improved so that it looks more organized and less
complicated from earlier and is easier to understand.

9) Variable declaration:
Some changes are also made in the variable declaration. Variables are declared
near to their usage. Temporary variables are declared and defined in the method where
they are being used.

10) Blocks and indentation:


Indentation is applied and blocks are used to make the code more organized and
enhance its readability and understandability.

REFACTORED CODE:
package incubatormonitors;
import java.util.Scanner;

public class IncubatorMonitors


{
public static void main(String[] args)
{
Signal s = new Signal();
s.Display();
}
};

abstract class important_methods


{
public static final int NIL = -999;
public static final int MaxTemp = 10;
public static final int MinTemp = -10;
public int ActTemp=NIL;

14
SC Semester Project

public int ReqTemp=NIL;


public int option;
public abstract void SetInitialTemp();
public abstract void RequestChange();
public abstract int Increment();
public abstract int Decrement();
};

class Signal extends important_methods


{
Scanner sc = new Scanner(System.in);

void Display()
{
SetInitialTemp();
String choice="y";
do
{
do
{
System.out.println("\n\t **** IncubatorMonitor Tester ****\n");
System.out.println("1. Request Change In The Temperature");
System.out.println("2. Increase 1°C Temperature");
System.out.println("3. Decrease 1°C Temperature");
System.out.println("4. Get Actual Temperature");
System.out.println("Select any option: 1 - 4");
System.out.println();
option = sc.nextInt();
System.out.print("\n\n");
switch(option)

15
SC Semester Project

{
case 1:
RequestChange();
System.out.println("\nDo you want to continue: (y/n)");
choice=sc.next();
break;
case 2:
int ciTemp = GetActualTemp();
if(ciTemp<10 && ciTemp!=NIL)
{
ciTemp = Increment();
System.out.println("Temperature is increased...");
}
else
{
System.out.println("Cannot increase temperature...");
}
System.out.println("\nDo you want to continue: (y/n)");
choice=sc.next();
break;
case 3:
int cdTemp = GetActualTemp();
if(cdTemp>-10 && cdTemp!=NIL)
{
cdTemp = Decrement();
System.out.println("Temperature is decreased...");
}
else
{
System.out.println("Cannot decrease temperature...");

16
SC Semester Project

}
System.out.println("\nDo you want to continue: (y/n)");
choice=sc.next();
break;
case 4:
int getTemp = GetActualTemp();
System.out.println(getTemp);
System.out.println("\nDo you want to continue: (y/n)");
choice=sc.next();
break;
default :
System.out.println("You have Selected an Invalid Option.\n\n");
System.out.println("\nDo you want to continue: (y/n)");
choice=sc.next();
}
}
while(option !=5);
}
while(choice=="y" || choice=="Y");
}

// METHODS
public boolean InRange(int t)
{
if(t >=MinTemp && t <= MaxTemp)
{
return true;
}
else
{

17
SC Semester Project

return false;
}
}
// method
public void SetInitialTemp()
{
System.out.println();
System.out.println("Enter Initial Temperature: ");
System.out.println();
ActTemp = sc.nextInt();
boolean t = InRange(ActTemp);
if(t==true)
{
System.out.println("Initial Temp has been set...");
}
else
{
System.out.println("Invalid entry....");
}
}

// method
public int GetActualTemp()
{
return ActTemp;
}

// method
public void RequestChange()
{

18
SC Semester Project

System.out.println();
System.out.print("Enter the temperature you want to set: ");
System.out.println();
int tempIn=sc.nextInt();
boolean result = InRange(tempIn);
ReqTemp = tempIn;
if(ReqTemp>ActTemp && result==true && ActTemp!=NIL)
{
do
{
Increment();
}
while(ActTemp!=ReqTemp && result==true);
System.out.println("Temperature has been changed...");
System.out.println("Now temperature is "+ActTemp+"°C");
}

else if(ReqTemp<ActTemp && result==true && ActTemp!=NIL)


{
do
{
Decrement();
}
while(ActTemp!=ReqTemp && result==true);
System.out.println("Temperature has been changed...");
System.out.println("Now temperature is "+ActTemp+"°C");
}

else if(ReqTemp==ActTemp)
{

19
SC Semester Project

Do_Nothing();
}
}

// method
public int Increment()
{
ActTemp++;
return ActTemp;
}

// method
public int Decrement()
{
ActTemp--;
return ActTemp;
}

// method
public int Do_Nothing()
{
int at = GetActualTemp();
return at;
}
};

20
SC Semester Project

21
SC Semester Project

22
SC Semester Project

23
SC Semester Project

THE END…

24

You might also like