How to write good code Part 2

Slides:



Advertisements
Similar presentations
Introduction to the SPL Interpreter
Advertisements

Lecture 14 User-defined functions Function: concept, syntax, and examples © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Links and Comments.
Introducing Java.
MATLAB File Management. MATLAB User File Management Matlab provides a group of commands to manage user files. For more information, type help iofun. pwd.
Programming. What is a Program ? Sets of instructions that get the computer to do something Instructions are translated, eventually, to machine language.
ICAPRG301A Week 4Buggy Programming ICAPRG301A Apply introductory programming techniques Program Bugs US Navy Admiral Grace Hopper is often credited with.
General Programming Introduction to Computing Science and Programming I.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Getting Started with MATLAB (part2) 1. Basic Data manipulation 2. Basic Data Understanding 1. The Binary System 2. The ASCII Table 3. Creating Good Variables.
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
Feb. 8, 2008 UHCO Graduate Course in MATLAB Core Programming Module Best Practices Core Grant Programming Module Best Practices (Coding Conventions) General.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Files: By the end of this class you should be able to: Prepare for EXAM 1. create an ASCII file describe the nature of an ASCII text Use and describe string.
Files Tutor: You will need ….
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
1 Project 7: Looping. Project 7 For this project you will produce two Java programs. The requirements for each program will be described separately on.
Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions Debugging using eclipse The Java API.
OCR A Level F453: The function and purpose of translators Translators a. describe the need for, and use of, translators to convert source code.
Dreamweaver – Setting up a Site and Page Layouts
Canvas and Arrays in Apps
Introduction to Computing Science and Programming I
Fundamentals of Python: First Programs
Topic: Python’s building blocks -> Variables, Values, and Types
Release Numbers MATLAB is updated regularly
Links and Comments in HTML5
Prof. Mark Glauser Created by: David Marr
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
Learning to Program D is for Digital.
CS 1110 Introduction to Programming Spring 2017
Matlab Training Session 4: Control, Flow and Functions
C++ coding standard suggestion… Separate reasoning from action, in every block. Hi, this talk is to suggest a rule (or guideline) to simplify C++ code.
Links and Comments.
Sussex Neuroscience Coding Club title slide
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Learning to Program in Python
Learning to Program in Python
Number and String Operations
Use of Mathematics using Technology (Maltlab)
Lesson 16: Functions with Return Values
Building Web Applications
Links and Comments.
Theory of Computation Turing Machines.
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
Functions In Matlab.
Fundamentals of Data Structures
Coding Concepts (Basics)
Coding Concepts (Standards and Testing)
Variables Title slide variables.
Unit 6 Assignment 2 Chris Boardley.
Random Access Files / Direct Access Files
How to write good code Part 1
Stata Basic Course Lab 2.
Hank Childs, University of Oregon
Links and Comments.
Tonga Institute of Higher Education IT 141: Information Systems
Links and Comments.
CMSC 345 Programming.
Java objects: a first view
Using Script Files and Managing Data
Tonga Institute of Higher Education IT 141: Information Systems
Mapping a network drive
CSCE 206 Lab Structured Programming in C
Software Development Techniques
The IF Revisited A few more things Copyright © Curt Hill.
Presentation transcript:

How to write good code Part 2 Title slide: how to write good code part 2

Content Part 1: Naming Conventions Code Layout Commenting Part 2: Functionising Conceptualisation Externalisation Part 3: Debugging Testing Last time (part 1) we covered naming conventions, code layout and commenting. We will cover part two today, discussing conceptualisation, externalisation and the use of functions. Conceptualisation  thinking about the best way to organise your code and variables, e.g. – don’t copy and paste, don’t use numbers in variable names Externalisation  if you run code on a different machine, it should still work, e.g. don’t use cd, don’t hard code variables, use fullfile to access saved files or to save figures and variables Part three (next week) will cover debugging and testing your code

Why write good code? ‘Good code is a present for your future self’ Inspirational quote: Good code is a present for your future self But also a legacy for future lab members, who should be able to run and understand your code, even if you are not there to help them.

Example code The slide shows a screenshot of example code in matlab. We will discuss how this code could be improved, considering what was discussed last week. Some of the improvements: Uniform indenting ‘if out’ pieced of code that are not used Hardcoding of path Make meaningful paragraphs Use white space in a better way Comment what the variables mean

What we discussed last week Naming conventions Code Layout White space Indenting Paragraphs Character limit line Commenting Inform reading of aim Variables ‘If out’ An overview of 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 Reduce workspace clutter Remember scope! Make an API Why would you use functions? It compartmentalizes your code, meaning you can use pieces of code in different analyses It makes your code easier to share It makes the code easier to reuse It reduces your workspace clutter, which is related to the idea of scope. This is mainly seen in Matlab, where you only have the output of a function in your workspace, not all the intermediate variables It allows you to make an API (Application programming interface) for other people to use

Make a function that takes folder/file as input Only output those four variables This shows an example of what my workspace looked like after running my code (roughly 30 variables) with the 4 that I actually use highlighted. The best way to solve this, is by making the code into a function with the needed input and those four variables as output.

Code as function Running it Workspace after This shows how the code was changed to a function in Matlab, and when running it, I only have 6 variables (2 input, 4 outpu) in my workspace in total.

Signs of poor conceptualisation Copy and paste Numbers in variable names Using eval (matlab) Using clear/clear all Another important thing for coding is conceptualisation. This means knowing the concepts in your code and using it to optimalize it. This slide lists examples of poor conceptualisation: Copy and pasting code, this means that you could have written a loop or a function to do what you are trying to do. Numbers in variable names. Unless it is a physical constant, you would ideally use arrays, cell arrays or vectors for these cases, because if you are numbering things, they are probably part of the same concept. Using eval (in Matlab), this means you are transforming strings into code, which you should in principle never do. Using clear/clear all. Functionising should take care of superfluous variables. Also, this causes your code to do unexpected things if there are already variables in the workspace previously.

Improve this code: Example of ImageJ code with poor conceptualisation, the next slide will show the improvements.

Improved code Improvements: Better commenting Removed variables with numbers in them, created a loop instead Better indenting Making an array with the possible options, instead of having them as separate variables

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 Externalisation is important for your code to work as a separate entity, allowing it to work on a range of computers and by a range of users. Some signs of poor conceptualisation: Using ‘cd’ in your code (i.e. moving to a certain folder). This will usually not work on other computers, and may even cause your function to not be able to run twice in a row. Hardcoding numbers that are used more than once, or should be changed by a user. These should be variables (and in some cases even input variables). Hardcoding any type of path, this will cause the code to error on other computers. Adding code to your path in the code (this includes downloading packages, though in some cases it is allowed, e.g. when using jQuery).

Another example of bad code, where the code makes a bunch of folders Another example of bad code, where the code makes a bunch of folders. However, a good thing about this piece of code is that it lets the user select the output folder, thus making it work on different computers. Another good thing is the use of ‘file separator’ instead of just a slash, so it’ll work on different systems.

Improved version of the code, where the folder names are all in an array except for in separate variables. The code then loops through this array and creates the folder.

Content Part 1: Naming Conventions Code Layout Commenting Part 2: Functionising Conceptualisation Externalisation Part 3: Debugging Testing We have now finished part 2, and next week will be part 3, discussing debugging and testing.

brainenergylab.com/sncc Remember, you can find all the slides on brainenergylab.com/sncc, while we are working on getting a Canvas site.

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 The schedule for the next few weeks: 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