- Superfluous code is bloated code. Code should include only that which is required for its functionality.
- Robust code is focussed on one functionality, and not mess around with unrelated tasks. Does MATLAB's SQRT clear the command window? Or close all of the open figures? Or spam text to the command window?
- Independence is achieved by using functions and classes (just like all the MATLAB's toolboxes and all well-written code on FEX), not by clearing a shared workspace.
- They are anti-pattern cargo-cult code. Proof: the number of threads on this forum where beginners cause pointless bugs by placing those commands throughout functions and scripts (which they then cannot debug by themselves).
The Problem with clc; clear; close all; ?
583 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
atharva aalok
am 17 Okt. 2021
Kommentiert: kunal goswami
am 18 Mär. 2022
A few days ago someone pointed out that using:
clc; clear; close all;
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 Kommentare
per isakson
am 17 Okt. 2021
"clear: erases the variables from previous runs" use clearvars for that rather than clear , which does so much more.
Akzeptierte Antwort
Rik
am 17 Okt. 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 Kommentare
Walter Roberson
am 17 Okt. 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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!