Tips for Coding

Whether you are taking PHY 210 and learning to code in Matlab or a MAT class and learning to use R or maybe your first CSC class, here are some tips on writing code in any language:

#1 Write or diagram your algorithm:

Think of an algorithm as being a road-map to solve the problem. This could take the form of a written step-by-step procedure or maybe a flow chart.

Start with either pen and paper or maybe a whiteboard or even a chalkboard. What is the problem you are trying to solve? What are the steps you need to go through to solve this problem? Write or draw these steps in order and in plain English or using appropriate mathematical equations. Are there any exceptions to the order of steps? Do some steps need to be repeated? If so, how many times should these steps be repeated?

Once you have a fully developed algorithm, then we can begin to think about how we might write code to solve this problem.

#2 Think through which concepts in programming might be useful in solving the problem:

Some basic concepts in programming include things like:

Variables: can be numbers, strings of characters (like a name or a sentence), or Boolean (true or false). Often times, variables are used or changed within an algorithm.

What are the variables in your algorithm? What values should be held in variables? What kind of variables should be used for each value?

Conditional statements: conditional statements allow you to do one block of a code if a particular statement is true and a different block of code if the statement is false. Example: if then statements, if else statements, switch statements, etc.

Are there places in your algorithm where you need to either do A or do B? What is the condition for deciding which step to take?

Loops: loops allow you to repeat a block of code a specified number of time (a for loop) or as long as a given statement remains true (a while loop)

Are there steps in your algorithm that need to be repeated? How many times should these steps be repeated?

If you are looking to get more familiar with the various concepts and structures available in programming, Scratch is a fun, interactive app for learning to code. Scratch is made to be accessible to children. Scratch App

#3 Reference the syntax for the computer programming language and go for it!

Now that you have thought through and mapped what concepts might be useful for turning your algorithm into code, it’s time to actually write your code!

Each programming language will have similar concepts and structures that can be used to create your code; however, the actual syntax (or language) used to make the code will be different. If you know you need a for loop and you are using python, you might do a Google search for “Syntax for a for loop in python” to find the information you need. Below are some starting points for learning a few commonly used programming languages:

Matlab:

Free 2-hour introduction to using Matlab from MathWorks (divided into 10 minute lessons)

Matlab basic commands reference sheet

Introduction to Matlab for Engineering Students by David Houcque

Matlab cheat sheet for mathematical operations from MIT

Free 4-hour video tutorial:

Python:

Python Beginners Guide with links to more training tutorials, exercises, and quizzes.

R:

2 hour introductory tutorial to R: