Skip to content
Antti Stenvall edited this page May 8, 2015 · 28 revisions

Short style guide for programming

The structure of the file header until the function really starts must be of following type (use this as the object variable name in class methods)

function [returnValues ...] = mFileName(this, parameter1, parameter2, varargin)
  % Summary of the method.
  %
  % Description of the method.
  %
  % return values:
  %  - returnValue1: description
  %
  % parameters:
  %  - parameter name {default value}: description
  %
  % varargin:
  %  - property {default value}: description
  %
  % Created: Name (e-mail)
  % Contributed: Name (e-mail)
  % Contributed: Name (e-mail)
  % ...
  %

  %% Instructions to init parameters from varargin
  % first set default parameters
  defaults.property1 = 'value1';
  defaults.property2 = 'value2';

  param = common.setDefaultParameters(defaults,varargin);
  % Now you can use param.property as you wish

  %% Actual method


end

List parameters in the order of appearance. Don't use default parameters if you use varargin. Thus, the example fails from this part. Also, if you use varargin, don't use nargin to test the number of arguments.

List varargin (Variable-length input argument list) (property,value)-pairs in alphabetical order.

If the function is a method of a class, then the first parameter is always named this. Explain constructor parameters in the class header. Write all the methods (including one liners, constructor with certain limitations: add method constructor to private and call it from actual constructor) to distinct files. Name class folder as @className and add all the methods there. For the labs, everything has been already made for you.

In varargin optional (property,value)-pairs are given in Matlab style like

plot(x,y,'linewidth',2)

In m-file optional parameters are loaded as

% first set default values
defaults.valueA = 1;
params = common.setDefaultParameters(defaults,varargin);

Then, it is easy to use these as params.valueA.

Always end function with end, though it is not necessary in Matlab.

Use indent size 2 and Function indenting format Indent all functions. These can be tuned in Preferences -> Editor/Debugger->Tab,Language.

Naming conventions should be quite obvious.

Clone this wiki locally