Is It Okay to Add Semicolons After Every Expression?

35 Ansichten (letzte 30 Tage)
Alexander
Alexander am 27 Jul. 2024
Kommentiert: John D'Errico am 30 Jul. 2024
Take, for example, this piece of code which adds semicolons even when there is no output to suppress:
clear;
clc;
x = 1:10;
y = (x+1).^2;
plot(1,y(1),"r*");
hold on;
for idx = 2:10
plot(idx,y(idx),"r*");
end
plot(x,y);
And this code which only includes semicolons when needed:
clear
clc
x = 1:10;
y = (x+1).^2;
plot(1,y(1),"r*")
hold on
for idx = 2:10
plot(idx,y(idx),"r*")
end
plot(x,y)
(These are just examples.) Is adding semicolons to every expression (the first example) wrong in any way?
It's just that when not using the editor you are not told if there is output to suppress, so I thought it would just be easier to use semicolons everywhere instead of trying to remember or guess where they are needed. So is it syntactically/traditionally/etc wrong in any way?

Akzeptierte Antwort

dpb
dpb am 27 Jul. 2024
Not "wrong", no. Superfluous in many instances, but not "wrong".
I think you'll find that with time in grade you'll begin to learn where they're needed and not and will gradually evolve to the less typing and clutter look. In the meantime, if you want to code that way and aren't bothered by the extra ones being in your m files to look at, "go for it!"
  4 Kommentare
Alexander
Alexander am 30 Jul. 2024
Ok, thank you!
John D'Errico
John D'Errico am 30 Jul. 2024
Be careful though, as the FDA (The Furry Dogs Association) has suggested there may be a link between excessive use of punctuation, expecially semi-colons, and cancer. It may in fact be valid, as a significant portion of the programmers who use semi-colons in excess will die of cancer, possibly within the next 50 years.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Biotech and Pharmaceutical finden Sie in Help Center und File Exchange

Produkte


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by