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 refactoring code, 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 = -127;
delete[] mySequence;
Python
In
Python 3
The programming language Python (programming language), Python was conceived in the late 1980s, and its implementation was started in December 1989 by Guido van Rossum at Centrum Wiskunde & Informatica, CWI in the Netherlands as a successor ...
, explicit deletion of variables requires the
del
keyword.
x = 15
my_sequence = for useless_variable in range(x)my_sequence = -127
del my_sequence
JavaScript
In
JavaScript
JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior.
Web browsers have ...
, 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
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
, 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 = -127;
my_sequence = null;
Other meanings
Code cleanup can also refer to the removal of all
computer programming
Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
from
source code
In computing, source code, or simply code or source, is a plain text computer program written in a programming language. A programmer writes the human readable source code to control the behavior of a computer.
Since a computer, at base, only ...
, or the act of removing
temporary files after a program has finished executing.
For instance, in a
web browser
A web browser, often shortened to browser, is an application for accessing websites. When a user requests a web page from a particular website, the browser retrieves its files from a web server and then displays the page on the user's scr ...
such as
Chrome browser or
Maxthon
Maxthon (, formerly named ''MyIE2'') is a freeware web browser, created by JeffChen in Singapore. It is available for Microsoft Windows, Windows, macOS, Linux, and as ''Maxthon Mobile'' for Android (operating system), Android, iOS, and Windows P ...
, 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
Data ( , ) are a collection of discrete or continuous values that convey information, describing the quantity, quality, fact, statistics, other basic units of meaning, or simply sequences of symbols that may be further interpreted for ...
. However, a file is treated as a permanent way to store a resizable list of
byte
The byte is a unit of digital information that most commonly consists of eight bits. Historically, the byte was the number of bits used to encode a single character of text in a computer and for this reason it is the smallest addressable un ...
s, 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 = 0, 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 ^= 2 // 'Squares the element.'
print string(element) + " is now... " + string(list
i++
end
References
{{Reflist
Other Resources
HTML Code CleanupFormatting and Cleaning Up CodeCode Formatter
Source code