Is It Okay to Add Semicolons After Every Expression?

40 ビュー (過去 30 日間)
Alexander
Alexander 2024 年 7 月 27 日
コメント済み: John D'Errico 2024 年 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 件のコメント
Alexander
Alexander 2024 年 7 月 30 日
Ok, thank you!
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 Center および File ExchangeBiotech and Pharmaceutical についてさらに検索

製品


リリース

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by