Software Construction: Identifying Bed Smell Signs in The Code
Software Construction: Identifying Bed Smell Signs in The Code
Software Construction: Identifying Bed Smell Signs in The Code
SC SEMESTER PROJECT
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
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
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.)
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.
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.
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.
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.
13
SC Semester Project
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.
REFACTORED CODE:
package incubatormonitors;
import java.util.Scanner;
14
SC Semester Project
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)
{
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