Code cleanup

Short description: Aspect of computer programming

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

## Contents

* 1 Examples
* 1.1 C++
* 1.2 PHP
* 1.3 Python
* 1.4 JavaScript
* 1.5 Java
* 2 Other meanings
* 2.1 Loop cleanup
* 3 References
* 4 Other Resources

## 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;

### PHP

In PHP there is the `unset` function.[3]

$foo = 123;
unset($foo);

### Python

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

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

### JavaScript

In JavaScript, objects are garbage collected if they are unreachable from the global object.[5] One way to make an object unreachable is to overwrite the variables or properties that reference it.

let x = {}; // The variable x is declared and set to an object
x = null; // x is overwritten and the object becomes unreachable

### 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.[6]

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 source code 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 Chrome browser or Maxthon, code must be written in order to clean up files such as cookies and storage.[7] 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.[8]

### 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". 15 December 2015. http://msdn.microsoft.com/en-us/magazine/ee335722.aspx.
2. ↑ "Code cleanup in C++". https://stackoverflow.com/questions/76796/memory-management-in-c.
3. ↑ "PHP: unset - Manual" (in en). https://www.php.net/manual/en/function.unset.php.
4. ↑ "Deletion of Variables in Python". http://www.network-theory.co.uk/docs/pytut/delstatement.html.
5. ↑ "Memory Management - Mark-and-sweep algorithm". 6 March 2025. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management#Mark-and-sweep_algorithm.
6. ↑ "Null in Java: The Pointer to Address 0". http://mindprod.com/jgloss/null.html.
7. ↑ "DOM Storage - MDN". 26 July 2024. https://developer.mozilla.org/en-US/docs/DOM/Storage.
8. ↑ "Erasing Cookies and Temporary Files in Google Chrome - Google.com". https://support.google.com/chrome/bin/answer.py?hl=en&answer=95647.

## Other Resources

* HTML Code Cleanup
* Formatting and Cleaning Up Code
* Resharper Code Cleanup
* Code Formatter

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/Code cleanup. Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Code_cleanup&oldid=4228981"