Programming Fundamentals

Our modern digital creations are complex: a key job of programmers is to express that complexity as simply as possible.

"The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct." -- Donald Knuth

Lesson 1: Some programming fundamentals

Humans have been programming computers for seven or so decades now. DevOps is a hot new topic in that practice, but the current popularity of the term does not mean we should ignore the many findings on how to write the best programs possible that came before DevOps should be ignored!

  • DRY
    This stands for Don't Repeat Yourself! It means that any part of your system that might ever need to change should have a single place where you can make the change. Don't copy blocks of code to wherever you need them in your program: write a function and call it from each of those places. Don't define your data tables in your database, and also in your code: find a way (like the Django models.py file) to define your data one place and use that definition to generate both the database and the code that uses the DB.
  • No magic constants.
    This is a special case of DRY. It is very tempting, when coding your NYU scheduling app, to write code assuming there are two (major) semesters per year. This will be fine... until NYU adopts a tri-mester system. Instead, define a constant NUM_SEMS = 2. You might get away with writing day_of_week = day mod 7, since that number probably will never change. But you really ought to write hour_of_day = hour mod CLOCK_PERIOD, since both 12 and 24 hour timekeeping methods exist.
  • Make functions do one job.
    Funcitons that perform a single job are simpler to understand, easier to change or eliminate, and render the overall system more comprehensible. For instance, if the county writes a tax program with a function called calc_taxes, it would be natural to eliminate that function if the job is later passed off to a microservice running on the cloud. But, if the coders also happened to include the code to clear tax liens (county claims against the property for unpaid taxes) in the same function... Oops! No one who ever had a tax lien can sell their property, because the lien never gets cleared.
  • Keep functions short.
    This is related to the previous principle, but focuses on the size of the one job that should be done. A function named handle_yearly_taxes() is doing one job, but probably way to big a job. It would make more sense to have create_tax_roll(), calculate_taxes(), send_bills(), record_payments(), and perhaps more.
  • Format and indent properly.
    Different languages have different conventions for how to name variables (camelCase, with_underscores, MixedCase, and so on), how to space operators, where to put braces, and so on. You should follow those conventions, unless there is a strong reason not to. Consistent indentation is especially important: it allows a reader of your code to easily line up blocks of control. Irregular indentation is a significant source of bugs, as people modifying the code will make mistakes, for example, about which else goes with which if.
  • Comment judiciously.
    Code should contain some comments, especially things like docstrings for classes that can be extracted to produce a guide to the system, and comments explaining what particularly tricky or unusual bits of code do. But commenting is no substitute for writing clear, readable code in the first place! The best explanation of what your code does is, if you write it correctly, your code itself. Remember that we could, and once did, write code just as a sequence of 1s and 0s. And all higher-level languages need to be translated into such code in the end. So why bother with C, Java, or Python? These languages exist for humans, not for computers: they make it easier for us to understand and reason about what a program will do. The upshot: you should look at your code as being every bit as much about communicating to humans as about directing a computer.
  • Go for the golden mean in naming.
    Sometimes, names of functions and variables can be way too cryptic: there are examples in the widely used CLRS Algorithm book where I have found as many as six single-letter variable names used at once. On the other hand, naming a function something like take_input_of_employee_w2_and_calculate_employee_tax_rate() is absurdly long: please remember, other programmers will have to type your function names in order to call your functions! Such immense names also make it extremely difficult to stay within guidelines like PEP 8's dictum of "no lines longer than 79 characters." A more reasonable middle ground might be something like calc_tax_rate(), where an employee's W2 might be a parameter for the function.
  • Test, test, test!
    Write an automated test to go with every program or new feature you write. Test as completely by hand as you can: don't just test that your code fetches the data from the DB correctly: test that it still works properly if there is no data in the DB, or, indeed, if there is no DB! ("Properly" here could mean "Display an informative error message instead of crashing.")
Lesson 2: Python coding standards

For this lesson, please read the Python coding standard, PEP 8. It is a very good example of what a coding standard is like, and most of the guidelines can be applied in other languages. Our JavaScript team is choosing a standard at present, and soon we will link to that here as well.

Other Readings
Quiz

    According to PEP 8, constants should...?

    1. BE_IN_ALL_CAPS
    2. beInCamelCase
    3. be_defined_with_underscores
    4. be defined on a per project basis

    DRY means your code should ...?

    1. not be all wet
    2. Delete Random Years
    3. Dally Really Youthfully
    4. not repeat itself

    Our functions should...?

    1. precisely embody the mathematical notion of a function
    2. do as many tasks as possible, since functions calls take time
    3. do one clearly definable task
    4. none of the above

    The best way to make your code understandable is...?

    1. to use as many comments as possible
    2. to use the longest variable names possible
    3. to write clear code
    4. none of the above

    When it comes to naming variables, we should...?

    1. always use camel case
    2. always employ underscores
    3. keep everything in ALL Caps
    4. use the convention most widespread in our coding language

    Comments should be...?

    1. employed only when necessary
    2. scattered through the code as widely as possible
    3. used for every line of code
    4. never used at all

    Function and variable names should be...?

    1. as short as possible
    2. as long as possible
    3. hitting the mean between very short and very long
    4. in all upper case

    PEP 8 recommends that when indenting, we should use..?

    1. tabs
    2. spaces
    3. underscores
    4. asterisks

    PEP 8 recommends using blank lines in functions...?

    1. whenever the heck you want to
    2. sparingly, to indicate logical sections
    3. never
    4. as much as possible

    The most important reason to use consistent indentation is...?

    1. it makes it easy for readers to see the control blocks
    2. to pass the tests of code-style tools
    3. it looks prettier that way
    4. all of the above