The Problem with clc; clear; close all; ?

610 views (last 30 days)
A few days ago someone pointed out that using:
clc; clear; close all;
Is considered as a case of code smell and cargo cult programming.
But does that really have to be the case?
clc: cleans up the command window and now one can work without getting confused with the commands for previous runs
clear: erases the variables from previous runs this will reduce chances of error in subsequent runs and the programmer does not have to worry about unnecessary trash variables.
close all: closes all currently open figures. This can be very helpful during subsequent runs of the same script. If the figure from the previous run has not been closed then the subsequent run will plot the data on the already open figure. Which of course is a total waste.
What is the problem with using these commands?
  3 Comments
per isakson
per isakson on 17 Oct 2021
"clear: erases the variables from previous runs" use clearvars for that rather than clear , which does so much more.

Sign in to comment.

Accepted Answer

Rik
Rik on 17 Oct 2021
My main point is that people will get into the habit of using this, and will also use it in functions.
Clear is almost never needed in normal use, as you can use functions to keep your workspace clean. Close all should also be avoided, as you should use explicit handles to make your code robust. If there is an unexpected close all in a function, that will be annoying.
The big exception is during debugging. If you're using a script to quickly check something, these are fine. That is why all Image Analyst's demos start with it, and why you probably won't find any of them in his functions (with the possible exception of clc).
  2 Comments
atharva aalok
atharva aalok on 17 Oct 2021
Thanks makes much more sense now.
Walter Roberson
Walter Roberson on 17 Oct 2021
There are cases where clearing a variable can be warranted, if you are using large enough variables that you are likely to run across memory limits, and the variable is not being used in a tight loop.
Clearing a variable forces the optimizer to recompute whether a future reference to the name should be treated as undefined or as a function call. For example,
gamma = 1:10;
gamma(5)
clear gamma
gamma(5)
After the clear, gamma did not become undefined: it became a reference to a function.
Much of the time, MATLAB can make block-level predictions about whether any given name resolves to a variable or a function, but when you clear a variable it cannot do that anymore, so the code slows down.
Thus, if you have the memory, it is more efficient to leave the clearing to be automatic from returning from a function.
See my timing tests at https://www.mathworks.com/matlabcentral/answers/60240-what-is-the-difference-between-the-effect-of-clear-and-clearvars#answer_757137 -- which, by the way, indicate that clearvars can be much much slower than clear.

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!