Is It Okay to Add Semicolons After Every Expression?

7 次查看(过去 30 天)
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?

采纳的回答

dpb
dpb 2024-7-27
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 个评论
John D'Errico
John D'Errico 2024-7-30
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Biotech and Pharmaceutical 的更多信息

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by