Is It Okay to Add Semicolons After Every Expression?

32 views (last 30 days)
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?

Accepted Answer

dpb
dpb on 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 Comments
John D'Errico
John D'Errico on 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.

Sign in to comment.

More Answers (0)

Categories

Find more on Biotech and Pharmaceutical in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!