{ "cells": [ { "cell_type": "markdown", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "# Code Style & Documentation\n", "\n", "- readability\n", "- code style\n", "- PEP8\n", "- comments\n", "- documentation\n", "- linters\n", "- versioning" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Code Readability (API)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "\"Code is more often read than written\" - Guido van Rossum" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "So: code should be written to be readable by humans." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Note: one of those humans is future you." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## The Zen of Python" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "import this" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Writing Readable Code" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "So how do we write good code for humans?\n", "\n", "- Use good structure\n", "- Use good naming\n", "- Use code comments and include documentation" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "### Good Structure\n", "\n", "If you design your program using separate functions for each task, avoid copying + pasting (functions and loops instead), and consider structure beforehand, you'll be set up for success" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "### Good Naming\n", "\n", "Clear names are for humans. The computer doesn't care, but you and others reading your code do." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "### Code comments & Documentation\n", "\n", "Helpful comments and documentation take your code to the next level. The final piece in the trifecta of readable code! " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Good code has good documentation - but code documentation should _not_ be used to try and fix unclear names, or bad structure. \n", "\n", "Rather, comments should add any additional context and information that helps explain what the code is, how it works, and why it works that way. " ] }, { "cell_type": "markdown", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "source": [ "These will all be components of your final project grade." ] }, { "cell_type": "markdown", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "#### Clicker Question #1\n", "\n", "What does the following code do?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "editable": true, "scrolled": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [], "source": [ "def ff(jj):\n", " oo = list(); jj = list(jj) \n", " for ii in jj: oo.append(str(ord(ii)))\n", " return '+'.join(oo)\n", "ff('Hello World.')" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- A) Returns unicode code points, as a list\n", "- B) Encodes a string as a cypher, returning a string of alphabetical characters\n", "- C) Returns unicode code points, as a string\n", "- D) Encodes inputs alphabetical characters, returned as a list\n", "- E) This code will fail" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Improvement Considerations:\n", "- Structural considerations: indentations & spacing\n", "- Improved naming: functions & variables\n", "- Add Comments within code" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "def return_unicode(input_list):\n", " string = list()\n", " input_list = list(input_list)\n", " \n", " for character in input_list: \n", " string.append(str(ord(character)))\n", " \n", " output_string = '+'.join(string)\n", " return output_string\n", "\n", "return_unicode('Hello World.')" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- A) Returns unicode code points, as a list\n", "- B) Encodes a string as a cypher, returning a string of alphabetical characters\n", "- C) Returns unicode code points, as a string\n", "- D) Encodes inputs alphabetical characters, returned as a list\n", "- E) This code will fail" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Improvement Considerations:\n", "- Structural considerations: indentations & spacing\n", "- Improved naming: functions & variables\n", "- Add Comments within code\n", "- **Proper Documentation!**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "# Let's fix this code!\n", "def convert_to_unicode(input_string):\n", " \"\"\"Converts an input string into a string containing the unicode code points.\n", " \n", " Parameters\n", " ----------\n", " input_string : string\n", " String to convert to code points\n", " \n", " Returns\n", " -------\n", " output_string : string\n", " String containing the code points for the input string.\n", " \"\"\" \n", " \n", " output = list()\n", " # Converting a string to a list, to split up the characters of the string\n", " input_list = list(input_string)\n", " \n", " for character in input_list: \n", " temp = ord(character)\n", " output.append(temp)\n", "\n", " output_string = '+'.join(output)\n", " \n", " return output_string" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Code Style" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true }, "slideshow": { "slide_type": "fragment" } }, "source": [ "Or: How to be Pythonic" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "Reasons to be Pythonic:\n", "- user friendly for humans\n", "- extra work up-front on the developers (pays off on the long run)\n", "- best to practice this early on (I promise!)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Style Guides" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "