Wikiwand AI

Code cleanup

From Wikipedia, the free encyclopedia

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]

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

References

Other Resources

Related Articles

Timelines

Top Qs

Fact Checks