Introduction to Python Code Quality

Similar documents
Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

The Three Rules. Program. What is a Computer Program? 5/30/2018. Interpreted. Your First Program QuickStart 1. Chapter 1

Intro. Scheme Basics. scm> 5 5. scm>

Flow Control: Branches and loops

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Not-So-Mini-Lecture 6. Modules & Scripts

Documentation Nick Parlante, 1996.Free for non-commerical use.

printf( Please enter another number: ); scanf( %d, &num2);

CMPT 120 Basics of Python. Summer 2012 Instructor: Hassan Khosravi

Copied from: on 3/20/2017

Working with Strings. Husni. "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc.

Python for Non-programmers

Getting Started Values, Expressions, and Statements CS GMU

Text Input and Conditionals

Perl Basics. Structure, Style, and Documentation

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

NESTED IF STATEMENTS AND STRING/INTEGER CONVERSION

Sequence of Characters. Non-printing Characters. And Then There Is """ """ Subset of UTF-8. String Representation 6/5/2018.

PYTHON. Varun Jain & Senior Software Engineer. Pratap, Mysore Narasimha Raju & TEST AUTOMATION ARCHITECT. CenturyLink Technologies India PVT LTD

CSE 142/143 Unofficial Style Guide

Python I. Some material adapted from Upenn cmpe391 slides and other sources

CMSC 201 Computer Science I for Majors

6.170 Laboratory in Software Engineering Java Style Guide. Overview. Descriptive names. Consistent indentation and spacing. Page 1 of 5.

Decisions, Decisions. Testing, testing C H A P T E R 7

THE IF STATEMENT. The if statement is used to check a condition: if the condition is true, we run a block

Lecture 4. Defining Functions

C++ Programming Style Guide

Python Games. Session 1 By Declan Fox

Introduction to Computation for the Humanities and Social Sciences. CS 3 Chris Tanner

Constants. Variables, Expressions, and Statements. Variables. x = 12.2 y = 14 x = 100. Chapter

Variables, expressions and statements

At full speed with Python

MITOCW watch?v=0jljzrnhwoi

Java Programming Style Guide

Variables and Constants

Expanded Guidelines on Programming Style and Documentation

A lot of people make repeated mistakes of not calling their functions and getting errors. Make sure you're calling your functions.

Variables, Expressions, and Statements

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Functions and Decomposition

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

(Python) Chapter 3: Repetition

Programming with Python

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

Part III Appendices 165

Chapter 2. Editing And Compiling

Invent Your Own Computer Games with Python

The Big Python Guide

Some material adapted from Upenn cmpe391 slides and other sources

(Refer Slide Time: 01:12)

CSE : Python Programming

Hello, World! An Easy Intro to Python & Programming. Jack Rosenthal

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

SEO According to Google

LOOPS. Repetition using the while statement

Boolean Expressions. Is Equal and Is Not Equal

Supplement D: Expanded Guidelines on Programming Style and Documentation

Documentation Requirements Computer Science 2334 Spring 2016

History Installing & Running Python Names & Assignment Sequences types: Lists, Tuples, and Strings Mutability

Boolean Expressions. Is Equal and Is Not Equal

Python: common syntax

How to Edit Your Website

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

Loop structures and booleans

Chapter 2 Writing Simple Programs

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

Writing and Running Programs

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

Variables and. What Really Happens When You Run hello_world.py

Microsoft Excel Level 2

T H E I N T E R A C T I V E S H E L L

Python. Executive Summary

Scripting Languages. Python basics

Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in:

1. BASICS OF PYTHON. JHU Physics & Astronomy Python Workshop Lecturer: Mubdi Rahman

Full file at

Programming Fundamentals and Python

Chapter 2 Basic Elements of C++

Python Activity 2: Input and Variables in Python

Introduction to Python (All the Basic Stuff)

Computer Science Lab Exercise 1

Course May 18, Advanced Computational Physics. Course Hartmut Ruhl, LMU, Munich. People involved. SP in Python: 3 basic points

Part 1 Simple Arithmetic

COMP 110 Project 1 Programming Project Warm-Up Exercise

Advanced Python. Executive Summary, Session 1

S206E Lecture 19, 5/24/2016, Python an overview

Python for C programmers

Readers are wary of out of date content, so it's important to actively manage the information you publish.

CMSC 201 Spring 2016 Lab 08 Strings and File I/O

CMSC 201 Fall 2018 Python Coding Standards

Smart formatting for better compatibility between OpenOffice.org and Microsoft Office

Lecture 5 8/24/18. Writing larger programs. Comments. What are we going to cover today? Using Comments. Comments in Python. Writing larger programs

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

INTERMEDIATE LEVEL PYTHON PROGRAMMING SELECTION AND CONDITIONALS V1.0

MITOCW watch?v=rvrkt-jxvko

ORB Education Quality Teaching Resources

Introduction to Python

Transcription:

Introduction to Python Code Quality Clarity and readability are important (easter egg: type import this at the Python prompt), as well as extensibility, meaning code that can be easily enhanced and extended. Use of these coding standards in your homework is required. Style Standards 1. Variable names: all lowercase, with underscores between any words Please do not use camelcase, but instead words_with_underscores. 2. Use 4-space Indents, no tabs PyCharm takes care of this for us - inserting 4 spaces whenever we use the [Tab] key. If you are using a different editor, you must configure it to do the same. See me for details. 3. One space on either side of operators var = aa + bb # note one space on either side of = and + This might seem picky, but it's all for readability, which is extremely important in the professional world. 4. No space between function name and argument list (parentheses) string_length = len('hello') # not len ('hello') Naming and Clarity 5. Use descriptive variable names; never use the plural for a loop variable Variable names should be descriptive to aid the reader, who shouldn't have to hold the meaning of several variables in mind at once (as would be if named aa, bb, etc.). If a variable holds the user's first name, it should be called first_name, user_fname, fname, i.e. something descriptive. If you call first name name1 and last name name2 you're not being as clear. If you call first name aa then the variable's identity becomes opaque to the reader (until he or she traces it back in your code).

In particular there is one particular misnomer that I'd like you to avoid: fh = open('datafile.txt') for lines in fh: items = lines.split() # please use singular ('line') # are we splitting one line, or many? We use the for construct to loop through a file line-by-line, but the control variable lines is supposed to be only one element at a time. Therefore it's misleading to call it lines when it's really just one line. 6. Do Not Use the Built-In Names for Your Variables I'd prefer that Python prohibit the use of these names, but it does not. list, dict, file, len, float, int, str and other builtin function names can be overwritten if we assign to these labels. >>> len('hello') 5 >>> len = 0.00001 >>> len('hello') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object is not callable 7. Use the string format() method to combine values with strings. Some folks prefer to stick to string concatenation or use commas when printing -- instead, use the string format() method to combine values (numbers or strings) with strings. (This is not a style convention, but I feel something you should be familiar with - outside of class you are free to use whatever you prefer.) Compare this: name = 'Holden' age = 29 print('my name is ' + name + ' and my age is ' + str(age) + '.') to this: name = 'Holden' age = 29 print('my name is {} and my age is {}'.format(name, age))

8. Comment your code only when needed for clarity, and please remove diagnostic code Most code if clearly written can be read and understood easily, but some code is by nature a little harder to read (such as list comprehensions, lambdas, code involving multidimensional structures, etc.). In these cases it makes sense to add a comment to explain what's happening. Some students like to comment each of their code steps. One method is to start with a comment outline and then write code to fulfill each comment. This is fine for draft work, but please remove unnecessary comments before submitting. 9. Put a String Comment at the top of the script with title description and author This is a great habit that you will thank yourself for acquiring, and it will help me in reviewing as well. No matter how short, every script should have an identifier that will make it instantly recognizable to you when you open the script later (otherwise you are forced to start reading it to get a sense of what it is supposed to be doing). """david_blaikie_1.2.py -- calculate a tip based on user input""" This "docstring" should be the first line of your code. Such strings can also be used for automatically generated documentation. Customarily we use a "triple-quoted string" for this purpose, although it can be a single- or double-quoted string as well. Code Organization 10. Conceptualize the logical "steps" of your code. Consider that every program takes discrete steps; separate your code visually (with blank lines) so these steps can be quickly identified by the reader as well. Note: keep in mind that these are guidelines and don't need to be followed in every case; you'll begin to develop good judgment about what's readable, what's well organized, etc. a. Take input. Most programs begin by taking user input or reading command-line input, which tells the program how to function. b. Validate and/or "normalize" input. If the input is coming from the user, it has to be checked to see if it is valid. It may also need to be converted into a usable form -- for example, if the user is asked to input an integer, the string needs to be converted to an integer. If the user is asked to input a filename, the program must determine if the file exists and can be read. c. Process or compute the data. This is the main event - once everything has been normalized and is ready to go, then we go through the computations -- summing, aggregating, etc.

d. Output the results. Finally, we inform the user of the results by printing the results of the computation. Again, these steps don't always apply -- but if you can avoid mixing them (for example, normalizing while you're in the middle of computation) then your code will be much clearer and more straightforward. 11. Use Blank Lines Sparingly, but Use Them to Separate Logical Steps Think of your code as an essay with paragraphs. Note in the code above that each step is comprised of a few statements and these are single-spaced, and that between steps we have placed a blank line. This indicates to the reader what the steps are and how best to read the code. This sort of logical separation greatly aids readability. 12. "Flat is better than nested" (Note that this will become important starting with the 2nd session.) Nested code creates logical dependencies, which means that one section of code is dependent on the other to be fully understood by the reader. If the dependencies are not needed, we have unnecessary logical complexity. Compare these two code snippets -- in the first below, notice that the computational while loop (bolded below) is logically dependent on the if and the other while, even though it doesn't need to be. Also note that the if and else are separated, requiring the reader to unite those two pieces in order to make sense of it. counter = 0 max_count = input('enter an int: ') if max_count.isdigit(): while counter < max_count: print(counter) print('done') else: print('sorry, bad input, try again') In a flatter (i.e., less indented) version of the same code below, each step (take and validate user input; convert input to int(); perform the computation) is separate, flatter and easier to read. It is also easier to maintain. The mind doesn't need to take in the whole of the code in order to comprehend the logic - instead one can understand the first step, then move on to the next step without having to remember anything. counter = 0 max_count = input('enter an int: ') if max_count.isdigit(): break print('sorry, try again') max_count = int(max_count) while counter < max_count: print(counter)

13. Avoid Repetition (DRY principle) Don't Repeat Yourself: if you find yourself issuing the same statement or group of statements more than once, ask whether the repetition can be eliminated. When you have finished your program, go over it once more and look for repetitions. your_guess = input("your guess? (q to quit): ") if your_guess == "q": exit() if int(your_guess) < number_to_guess: print("your guess is LOWER than the number I'm thinking of.") if int(your_guess) > number_to_guess: print("your guess is HIGHER than the number I'm thinking of.") if int(your_guess) == number_to_guess: print("you got it! You guessed it in {} tries".format(counter)) quit() Can you move the to a different location where it only needs to appear once? Can you also perform the int() conversion once so it is not repeated? 14. Avoid Using Intermediate Structures This one is harder to explain in specific, but I have found that many students get used to using one container structure (in particular, list is a favorite) and so try to do everything with that structure. They also like to break down steps into multiple loops, by looping through one list, modifying an element and appending it to a new list, then looping through the new list, modifying an element and appending the element to a third list, etc., when the whole thing could be done in one list, by performing successive operations in the one loop. In this example, stripped_lines, an intermediate structure, is not necessary: stripped_lines = [] ids = [] for line in open(filename).readlines(): line = line.rstrip() stripped_lines.append(line) for line in stripped_lines: elements = line.split(':') ids.append(elements[-1]) These two loops could be consolidated into one loop: ids = [] for line in open(filename).readlines(): line = line.rstrip() elements = line.split(':') ids.append(elements[-1])