Jump to content

Code cleanup

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Botondar (talk | contribs) at 13:26, 27 January 2015 (The C++ example wouldn't compile, because it is syntactically incorrect.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Code cleanup refers to the act of writing code so that it cleans up leftover data structures and other unwanted materials from memory and the filesystem. It is not the same as refactoring code, which involves making the source code itself easier to understand, maintain, and modify.[1]

Examples

C++

In C++, code cleanup involves deallocating previously allocated dynamic memory.

This is usually done with the C++ delete and delete[] operations.[2]

int x = 15;
int* mySequence = new int[x];
for (int i = 0; i < x; i++) {
    mySequence[i] = 0;
}
mySequence[0] = -127;
delete[] mySequence;

Python

In Python 3, explicit deletion of variables requires the del keyword.[3]

x = 15
my_sequence = [0 for useless_variable in range(x)]
my_sequence[0] = -127
del my_sequence

JavaScript

In JavaScript, deleting a variable requires the delete keyword.[4]

var x = 15;
my_sequence = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
my_sequence[0] = -127;
delete my_sequence;

Java

In Java, variables cannot be truly deleted. The most that can be done is to set the variable to null, which works with any Java object, including arrays.[5]

int x = 15;
int[] my_sequence = new int[x];
for (int i = 0; i < x; i++) {
    my_sequence[i] = 0;
}
my_sequence[0] = -127;
my_sequence = null;

Other Meanings

Code cleanup can also refer to the removal of all comments from source code, or the act of removing temporary files after a program has finished executing.

For instance, in a web browser such as Google Chrome or Maxthon, code must be written in order to clean up files such as cookies and HTML5 storage.[6] The deletion of temporary files is similar to the deletion of unneeded lists and arrays of data. However, a file is treated as a permanent way to store a resizable list of bytes, and can also be removed from existence.[7]

Loop Cleanup

Another technical term sometimes called "code cleanup" is loop cleanup.

/* 'The i++ part is the cleanup for the for loop.' */
for i = 0; i < 100; i++
    print i
end

import type
list = [10, 20, 30, 40, 50]
/* 'Even in a for each loop, code cleanup with an incremented variable is still needed.' */
i = 0
for each element of list
    list[i] ^= 2   // 'Squares the element.'
    print string(element) + " is now... " + string(list[i])
    i++
end

References

  1. ^ "Microsoft Talks Code Cleanup".
  2. ^ "Code cleanup in C++".
  3. ^ "Deletion of Variables in Python".
  4. ^ "JavaScript Variable Deletion - The delete keyword".
  5. ^ "Null in Java: The Pointer to Address 0".
  6. ^ "DOM Storage - MDN".
  7. ^ "Erasing Cookies and Temporary Files in Google Chrome - Google.com".

Other Resources

HTML Code Cleanup
Formatting and Cleaning Up Code
Resharper Code Cleanup