Make Your Code Unreadable! Case Study

From TekiWiki
Jump to: navigation, search

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