Skip to content

How to optimize your code ?

alizeelopez edited this page Oct 2, 2014 · 2 revisions

Abstraction

  • Functions

Encapsulation : inputs / outputs

One good thing to do : only one function and not several times the (almost) same script.

Allows you to test your function : once it works, you don't have to take care of it anymore

Behavioral driven programming : define what you want your function is for

"Keep it dry" : first rule --> Don't repeat yourself ! Avoid Copy-paste !

(With a function, output can stock the input (and save parameters you used to get your results/output)) Not more than 7 parameters in your function (you don't want to count your inputs...)

option structures : create a structure that contains special paramaters

ex :

input : options.type_discount options.normalisation

output : results.options results.fit result.R2

[new function in the toolbox : options=check_struct(options,...) --> Add default fields to your options structure if they are missing]

  • Structures

The problem with concatenation in structures : try to keep the same (first) field level to deal with it. Ex : [result.RT] concatenates first levels of your structure (RT are vectors) ; [result. iSubj]=deal(1:18) : attribute element of vector to fields

-Structures vs Matrices :

one matrix with everything (one line per subj, one variable per column) --> may be complicated to access some data, ex : (data(data(:,h)==5,:) one structure with one field per variable --> simplify access another solution to simplfy access : tables (matlab 2014b) (like R)

  • Design pattern :

model/view/controller architecture (general pattern to apply to simplify your analysis)

ex :

data.RT data.answer mean([data.RT]) model : initial data to data always presented in the same way (structure for example)

view : Plot part, create a group structure with same fields as subject structures (you'll be able to see individual and group results with the same plot_function)

controller : analysis

  • Code smells

(-"code sniffers" do not exist on matlab, but usually, they look for weird part or unoptimised parts in your code)

Be careful with :

  • duplicated code : when you have duplicated code, try to replace it by a function

  • long method : don't put too many inputs in your function

  • abuse of literals : when you directly put numbers in your code (data(4,:) is a lack of abstraction in your code : try to define the column number at the beginning of your code)

  • cyclomatic complexity (or spaghetti coding) :( for ... for .. for ... if... if... end end end end end) : try to avoid this ! Use substructures, subfunctions.

Example : factorial testing of models

function results = bDCM_gonogo_launcher % construct factorial model space modelList = factorial_struct( ... 'action' , {'drive','withold'} , ... 'action_mode' , {'linear','quadratic'} , ... 'inhib' , {'serial','parallel','modulate'} , ... 'encod' , {'direct','full'} ...);

% invert all models parfor iModel = 1:numel(modelList) theModel = modelList(iModel) ; [posterior,out] = bDCM_gonogo(theModel); results(iModel).modele = theModel ; results(iModel).posterior = posterior; results(iModel).out = out; end

  • feature envy : lot of embeded functions to try to structure your code but it's always the same variables that are used (functions are not autonomus anymore) (lasagna coding :) )
Clone this wiki locally