필터 지우기
필터 지우기

Is It Okay to Add Semicolons After Every Expression?

조회 수: 7 (최근 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 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