2.2. Good Coding#

Some advice about coding well here.

Let’s start off with something very important: if you can’t write it out on paper, you can’t code it. Knowing the syntax for a language does not mean shit. No one cares about what you can do at the keyboard—yeah, true statement.

Here is the deal: in order to understand how a model works, you have to be able to write out the algorithm on paper. By the way, doing this is good practice for the job interview; it is exactly the type of thing you will be asked to do when you are given a whiteboard question. Coding languages are similar—they can only do certain things: basic math, conditional statements, iterations (i.e., loops), ability to write functions, various methods to contain data (arrays, lists, dictionaries, etc.). The important part is knowing how the algorithm works, and this is language independent. Once you know how it works, you can write it in any language you choose—that is the easy part. The important stuff does not happen at the keyboard.

2.2.1. Pseudo Code#

Pseudocode is a language that illustrates the steps in a computer algorithm. There are various forms of pseudocode, and there is no clear definition of pseudocode. You should be able to read pseudocode and write your own pseudocode.

Here is an example of pseudo code for finding the maximum value in an array. Notice that the pseudo code is language independent.

ALGORITHM: Find Maximum Value
INPUT: array A of size n
OUTPUT: maximum value

max_value = A[0]
FOR i = 1 to n-1
    IF A[i] > max_value THEN
        max_value = A[i]
    END IF
END FOR
RETURN max_value

Here is the Python code.

import numpy as np

def max_val(A): 

    """
    Find the maximum value in an array.
    
    Parameters:
    A (list or np.array): Input array of numbers
    
    Returns:
    float/int: Maximum value in the array
    """

    # length of the array and initial maximum value
    n = len(A)
    max_value = A[0]

    # loop through each element, first element already
    # accounted for, start with second index 
    for i in range(1, n): 
        
        # if element A[i] is greater than max_value, reassign
        if A[i] > max_value: 
            max_value = A[i]

    return max_value 

A = np.array([10, 60, -80, 35]) 
print("The maximum of A is:", max_val(A))
The maximum of A is: 60

2.2.2. Incremental Coding#

Don’t go for the final product on the first attempt. Work incrementally, building up to the final product. Do this:

  1. Write it on paper first.

  2. Don’t start with functions—write simple code blocks that you know will work, and then wrap that code block in a function.

  3. Test your code. Develop ways to test your code.

  4. Know that if you don’t work incrementally, I will likely not help you.

2.2.3. Expect Bugs#

Coding bugs are a part of the game—get over it, you wanted to do this. You will get errors. It is your job to learn how to fix those errors. If I fix the errors for you, you will never learn how to fix them yourself.

2.2.4. Documentation and Comments#

Use docstrings and comment your code well. Documentation helps others know what your code does, and it helps you understand what you wrote and helps you organize your code.

Check this out. Consider the max_val function above, in a code cell execute the following: print(max_val.doc). You will see the docstring.

2.2.5. Style Guide#

Follow the Python style guide. No, you are not free to choose the style as you like—again, follow the Python style guide.

2.2.6. From Scratch Code versus Professional Grade Code#

We will be writing a lot of code from scratch. The main purpose of doing this is so we can learn the underlying methods and algorithms. Knowing exactly how a method works is invaluable—it will greatly enhance your ability to use, debug, and adapt these methods effectively.

However, when you need to put code into production or work on real-world projects, don’t use your from-scratch implementations. Instead, use professional-grade libraries.