Bad Smells in the code (22b0908 & 22b1010)
Project2:
Issue 1: Duplicate Code
Solution: Make a function and call it for each file, pass file name and list of string as parameter
to that function
ifstream e,f,g,h;
f.open("elements");
for( int i=0;i<118;i++) // input names of elements
from file "elements" into an array
f>>a[i];
f.close();
e.open("elements3");
for( int i=0;i<118;i++) // input discovery year of
elements from file "elements3" into an array
e>>b[i];
e.close();
g.open("elements2");
for( int i=0;i<118;i++) // input atomic weight of elements
from file "elements2" into an array
g>>c[i];
g.close();
h.open("elements4");
for( int i=0;i<118;i++) //input ionisation energy of
elements from file"elements4"into an array
h>>d[i];
h.close();
b1 = new Fl_Box (350-y,350,2,2, "");
b1->box(FL_UP_BOX);
b1->box(FL_FLAT_BOX);
b1->color (FL_GREEN);
b1 = new Fl_Box (350+y,350,2,2, "");
b1->box(FL_UP_BOX);
b1->box(FL_FLAT_BOX);
b1->color (FL_GREEN);
b1 = new Fl_Box (350+x,350+sqrt(y*y-x*x),2,2, ""); // creates upper
half of shell
b1->box(FL_UP_BOX);
b1->box(FL_FLAT_BOX);
b1->color (FL_GREEN);
//creates
lower half of shell
b1 = new Fl_Box (350+x,350-sqrt(y*y-x*x),2,2, "");
b1->box(FL_UP_BOX);
b1->box(FL_FLAT_BOX);
b1->color (FL_GREEN);
b1 = new Fl_Box (550,25,100,30,"Element name:");
b1->box(FL_UP_BOX);
b1->box(FL_FLAT_BOX);
b1->color (q);
b1->labelsize(20);
b1 = new Fl_Box (700,25,150,30, a[m-1].c_str()); // display
element's name
b1->box(FL_UP_BOX);
b1->box(FL_FLAT_BOX);
b1->color (q);
b1->labelsize(20);
b1->labelfont(FL_BOLD+FL_ITALIC);
b1 = new Fl_Box (550,125,100,30,"Discovery year:");
b1->box(FL_UP_BOX);
b1->box(FL_FLAT_BOX);
b1->color (q);
b1->labelsize(20);
b1 = new Fl_Box (700,125,150,30, d[m-1].c_str());
b1->box(FL_UP_BOX); // display
year of discovery of element
b1->box(FL_FLAT_BOX);
b1->color (q);
b1->labelsize(20);
b1->labelfont(FL_BOLD+FL_ITALIC);
b1 = new Fl_Box (550,225,100,30,"Atomic weight:");
b1->box(FL_UP_BOX);
b1->box(FL_FLAT_BOX);
b1->color (q);
b1->labelsize(20);
b1 = new Fl_Box (700,225,150,30, b[m-1].c_str());
b1->box(FL_UP_BOX); // display atomic weight
b1->box(FL_FLAT_BOX);
b1->color (q);
b1->labelsize(20);
b1->labelfont(FL_BOLD+FL_ITALIC);
b1 = new Fl_Box (570,325,100,30,"Ionisation energy:");
b1->box(FL_UP_BOX);
b1->box(FL_FLAT_BOX);
b1->color (q);
b1->labelsize(20);
b1 = new Fl_Box (710,325,150,30, c[m-1].c_str());
b1->box(FL_UP_BOX); // display ionisation energy
of element
b1->box(FL_FLAT_BOX);
b1->color (q);
b1->labelsize(20);
b1->labelfont(FL_BOLD+FL_ITALIC);
Issue 2: Main is too long
Solution: Break the implementation into different parts and structure the workflow
Issue 3: Too Many IF cases
Solution: Can use map to reduce number of If cases
if(n==1) j=7;
if(n==3) j=6.28318/(float)2;
if(n==11) j=6.28318/(float)8; //j= angle between adjacent electrons
in same shell
if(n==19) j=6.28318/(float)8;
if(n==37) j=6.28318/(float)18;
if(n==55) j=6.28318/(float)18;
if(n==87) j=6.28318/(float)32;
Issue 4: Variable names are not informative
Solution: Variable names can be changed to convey what they store and what their purpose is
Issue 5: Constants are being used as it is
Solution: Constants can be stored as variables at the top of the code, or read from a
configuration file