Whitespace guidelines

Whitespace in statements cc4m_logo_inline

IDLAYOUT-3
TitleSurround operators with spaces.
PriorityRecommended
Severity level9
DescriptionSurround the following operators with spaces:
= : ^ == <= >= < > ~= & && | || + - * .* /
Do not add a space between the unary minus-sign and its operand.
RationaleWhitespace around operators often leads to improved readability. As an exception, do not use spaces around the equals signs when using the Name=Value syntax introduced in MATLAB R2021a.

Avoid:

isValid=y>0&&x==-1;

Instead use:

isValid = y > 0 && x == -1;

Whitespace after characters cc4m_logo_inline

IDLAYOUT-4
TitleCommas, semicolons and keywords shall be followed by a space unless at the end of the statement.
PriorityRecommended
Severity level9
DescriptionCommas, semicolons and keywords (for, while, if etc.) shall be followed by a space unless at the end of the statement.
RationaleA clear separation of keywords from other code improves readability.

Avoid:

while(ismember(x, [3,5,7,11,13]))
    x = x + 2;
end

Instead use:

while ismember(x, [3, 5, 7, 11, 13])
    x = x + 2;
end

Whitespace at end of line cc4m_logo_inline

IDLAYOUT-12
TitleDo not use whitespace at the end of a line.
PriorityRecommended
Severity level9
DescriptionTo keep things clean, do not use white space at the end of a line of code unless it is followed by a comment.
RationaleThis prevents smart-indenting a file from inducing changes to those lines.

Whitespace around blocks of code

IDLAYOUT-16
TitleSeparate blocks of code by one or more blank lines.
PriorityRecommended
Severity level9
DescriptionSeparate coherent blocks of code by one or more blank lines.
RationaleIncreases the readability of the code.

Avoid:

deltaX          = mean(xIn);
xOut            = xIn - deltaX;
scalingFactor   = newZ / currentZ;
xOut            = xOut .* scalingFactor;
xOut            = xOut + deltaX;

Instead use:

% Shift to origin.
deltaX  = mean(xIn);
xOut    = xIn - deltaX;

% Calculate scaling.
scalingFactor   = newZ / currentZ;
xOut            = xOut .* scalingFactor;

% Shift back.
xOut = xOut + deltaX;