How to write good code Part 2 Content

  • Slides: 17
Download presentation
How to write good code Part 2

How to write good code Part 2

Content Part 1: • Naming Conventions • Code Layout • Commenting Part 2: •

Content Part 1: • Naming Conventions • Code Layout • Commenting Part 2: • Functionising • Conceptualisation • Externalisation Part 3: • Debugging • Testing

Why write good code? • ‘Good code is a present for your future self’

Why write good code? • ‘Good code is a present for your future self’

Example code

Example code

What we discussed last week • Naming conventions • Code Layout • • White

What we discussed last week • Naming conventions • Code Layout • • White space Indenting Paragraphs Character limit line • Commenting • Inform reading of aim • Variables • ‘If out’

Use of functions • Compartmentalize • Easier to share • Easier to reuse •

Use of functions • Compartmentalize • Easier to share • Easier to reuse • Reduce workspace clutter • Remember scope! • Make an API

 • Make a function that takes folder/file as input • Only output those

• Make a function that takes folder/file as input • Only output those four variables

Code as function Running it Workspace after

Code as function Running it Workspace after

Signs of poor conceptualisation • Copy and paste • Numbers in variable names •

Signs of poor conceptualisation • Copy and paste • Numbers in variable names • Using eval (matlab) • Using clear/clear all

Improve this code:

Improve this code:

Improved code

Improved code

Signs of poor externalisation • Using ‘cd’ in your code • Will not work

Signs of poor externalisation • Using ‘cd’ in your code • Will not work when run on other computers • Hardcoding numbers that are used more than once • Put into variable • Hardcoding any type of path • Adding code to path in your code

Content Part 1: • Naming Conventions • Code Layout • Commenting Part 2: •

Content Part 1: • Naming Conventions • Code Layout • Commenting Part 2: • Functionising • Conceptualisation • Externalisation Part 3: • Debugging • Testing

brainenergylab. com/sncc

brainenergylab. com/sncc

Schedule • 30 May – Kira & Dori – Good code part 3: testing

Schedule • 30 May – Kira & Dori – Good code part 3: testing and debugging • 6 June – Devin – SPSS/imaging processing • 13 June – Ben – Stochastic Programming • 20 June – Dori – Git/github