Skip to content
Surf Wiki
Save to docs
general/source-code

From Surf Wiki (app.surf) — the open knowledge base

Code cleanup

Aspect of computer programming


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.

Examples

C++

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

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

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 function.

$foo = 123;
unset($foo);

Python

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

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. 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.

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. 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.

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

Other Resources

References

  1. (15 December 2015). "Microsoft Talks Code Cleanup".
  2. "Code cleanup in C++".
  3. "PHP: unset - Manual".
  4. "Deletion of Variables in Python".
  5. (6 March 2025). "Memory Management - Mark-and-sweep algorithm".
  6. "Null in Java: The Pointer to Address 0".
  7. (26 July 2024). "DOM Storage - MDN".
  8. "Erasing Cookies and Temporary Files in Google Chrome - Google.com".
Info: Wikipedia Source

This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page.

Want to explore this topic further?

Ask Mako anything about Code cleanup — get instant answers, deeper analysis, and related topics.

Research with Mako

Free with your Surf account

Content sourced from Wikipedia, available under CC BY-SA 4.0.

This content may have been generated or modified by AI. CloudSurf Software LLC is not responsible for the accuracy, completeness, or reliability of AI-generated content. Always verify important information from primary sources.

Report