Difference between revisions of "Make Your Code Unreadable! Case Study"

From TekiWiki
Jump to: navigation, search
(Created page with "Consider the following code: Calculate new salary for employee. Calculations routines require a temporary file for tax and history purposes.: Create_Temp_File(Temp_Calc_...")
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
Consider the following code:
 
Consider the following code:
 
+
<pre>
 
/* Calculate new salary for employee.
 
/* Calculate new salary for employee.
 
Calculations routines require a temporary file for tax and history purposes. */
 
Calculations routines require a temporary file for tax and history purposes. */
Line 6: Line 6:
 
New_Salary = Calculate_New_Salary(Old_Salary, Gross_Salary_Increase, Temp_Calc_File)
 
New_Salary = Calculate_New_Salary(Old_Salary, Gross_Salary_Increase, Temp_Calc_File)
 
Delete_Temporary_File(Temp_Calc_File)
 
Delete_Temporary_File(Temp_Calc_File)
Firstly, we need to apply our first principles - remove comments (as this would waste time compiling), rename procedures and variables unreadably:
+
</pre>
  
 +
Firstly, we need to apply our first principles - remove comments (as this would waste time compiling), rename procedures and variables unreadably:
 +
<pre>
 
CrTmpF(t)
 
CrTmpF(t)
 
X=SalCal(temp2, inc, t)
 
X=SalCal(temp2, inc, t)
 
XTmpF(t)
 
XTmpF(t)
 +
</pre>
  
 
Now we are getting somewhere! We thought SalCal sounded quite good, so we broke the noun rule and kept an abbreviation of the verb in it.
 
Now we are getting somewhere! We thought SalCal sounded quite good, so we broke the noun rule and kept an abbreviation of the verb in it.
  
 
However, our mission is not quite complete. Next we need some meaningless indentation, and add the file deletion to the salary calculation, but leave the creation as a separate call:
 
However, our mission is not quite complete. Next we need some meaningless indentation, and add the file deletion to the salary calculation, but leave the creation as a separate call:
 
+
<pre>
 
     CrTmpF(t)
 
     CrTmpF(t)
 
X=SalCal(temp2, inc, t)
 
X=SalCal(temp2, inc, t)
 +
</pre>
  
 
The other problem we have is the number of parameters - far too many! So let's replace with global variables:
 
The other problem we have is the number of parameters - far too many! So let's replace with global variables:
 
+
<pre>
 
A1=temp2
 
A1=temp2
 
A3=t
 
A3=t
Line 26: Line 30:
 
     CrTmpF(t)
 
     CrTmpF(t)
 
X=SalCal()
 
X=SalCal()
 
+
</pre>
  
 
Note some nice touches - the A1-3 are not set in order, one procedure keeps its parameter and the other one does not, the setting of the variables for the procedure is done away from the procedure they are used in.
 
Note some nice touches - the A1-3 are not set in order, one procedure keeps its parameter and the other one does not, the setting of the variables for the procedure is done away from the procedure they are used in.
Line 33: Line 37:
 
Now we need to go into creative overdrive: add an unused global variable A4, reassign A2 after the file creation for no apparent reason and add some misleading comments:
 
Now we need to go into creative overdrive: add an unused global variable A4, reassign A2 after the file creation for no apparent reason and add some misleading comments:
  
 
+
<pre>
 
A1=temp2    /* SAL B4 */
 
A1=temp2    /* SAL B4 */
 
A3=t      /* increment a2 */
 
A3=t      /* increment a2 */
Line 41: Line 45:
 
A2=inc
 
A2=inc
 
X=SalCal()
 
X=SalCal()
 
+
</pre>
 
Now, we have "Made Our Code Unreadable".
 
Now, we have "Made Our Code Unreadable".
 +
 +
Return to [[The Real Standard of Development]]

Latest revision as of 06:46, 5 August 2016

Consider the following code:

/* Calculate new salary for employee.
Calculations routines require a temporary file for tax and history purposes. */
Create_Temp_File(Temp_Calc_File)
New_Salary = Calculate_New_Salary(Old_Salary, Gross_Salary_Increase, Temp_Calc_File)
Delete_Temporary_File(Temp_Calc_File)

Firstly, we need to apply our first principles - remove comments (as this would waste time compiling), rename procedures and variables unreadably:

CrTmpF(t)
X=SalCal(temp2, inc, t)
XTmpF(t)

Now we are getting somewhere! We thought SalCal sounded quite good, so we broke the noun rule and kept an abbreviation of the verb in it.

However, our mission is not quite complete. Next we need some meaningless indentation, and add the file deletion to the salary calculation, but leave the creation as a separate call:

    CrTmpF(t)
X=SalCal(temp2, inc, t)

The other problem we have is the number of parameters - far too many! So let's replace with global variables:

A1=temp2
A3=t
A2=inc
    CrTmpF(t)
X=SalCal()

Note some nice touches - the A1-3 are not set in order, one procedure keeps its parameter and the other one does not, the setting of the variables for the procedure is done away from the procedure they are used in.


Now we need to go into creative overdrive: add an unused global variable A4, reassign A2 after the file creation for no apparent reason and add some misleading comments:

A1=temp2    /* SAL B4 */
A3=t      /* increment a2 */
A2=temp2    /* file a3 */
A4=temp2   /* Set A4 */
    CrTmpF(t)
A2=inc
X=SalCal()

Now, we have "Made Our Code Unreadable".

Return to The Real Standard of Development