Programming in the Real World. Dr. Baldassano Yu s Elite Education

Similar documents
Writing better code Loop invariants Correctness. John Edgar 2

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Testing. UW CSE 160 Winter 2016

CMSC 201 Spring 2019 Lab 06 Lists

1 Getting used to Python

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

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

Summer May 11, 2010

Software Testing Lecture 1. Justin Pearson

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

Software Testing. Software Testing. Theory, Practise and Reality IBM Corporation

Welcome to Python! If you re the type of person who wants to know

Introduction to Python Part 2

Testing and Debugging

COMP1730/COMP6730 Programming for Scientists. Testing and Debugging.

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

Lesson 1 Python: Interactive Fiction

Debugging. ICS312 Machine-Level and Systems Programming. Henri Casanova

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

Lab 08. Command Line and Git

Hello! ios Development

Exercises: Instructions and Advice

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

Stanko Tadić

PYTHON YEAR 10 RESOURCE. Practical 01: Printing to the Shell KS3. Integrated Development Environment

Debugging Your Python Code: For Dummies

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

Text Input and Conditionals

Getting started with GitHub

APPENDIX B. Fortran Hints

QUICK EXCEL TUTORIAL. The Very Basics

Module 6. Campaign Layering

Agenda. - Final Project Info. - All things Git. - Make sure to come to lab for Python next week

Intro to Algorithms. Professor Kevin Gold

Under the Debug menu, there are two menu items for executing your code: the Start (F5) option and the

Computer and Programming: Lab 1

What is version control? (discuss) Who has used version control? Favorite VCS? Uses of version control (read)

QUIZ. What are 3 differences between C and C++ const variables?

CMSC 201 Fall 2018 Python Coding Standards

CMSC 201 Spring 2017 Lab 05 Lists

... Fisheye Crucible Bamboo

These are notes for the third lecture; if statements and loops.

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0. L J Howell UX Software Ver. 1.0

Lesson 1A - First Java Program HELLO WORLD With DEBUGGING examples. By John B. Owen All rights reserved 2011, revised 2015

CSS Crash Course for Fearless Bloggers by Gill Andrews

Session 4 Starting the Air Raid Game

CMSC 201 Fall 2018 Lab 04 While Loops

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

Outlook is easier to use than you might think; it also does a lot more than. Fundamental Features: How Did You Ever Do without Outlook?

Excel Basics: Working with Spreadsheets

INTRODUCTION. In this guide, I m going to walk you through the most effective strategies for growing an list in 2016.

What is the Best Way for Children to Learn Computer Programming?

JavaScript Fundamentals_

Errors. And How to Handle Them

Steps for project success. git status. Milestones. Deliverables. Homework 1 submitted Homework 2 will be posted October 26.

LOOPS. Repetition using the while statement

(Python) Chapter 3: Repetition

ASCII Art. Introduction: Python

Introduction to Programming Style

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

EPISODE 23: HOW TO GET STARTED WITH MAILCHIMP

Lecture 1: Overview

QUIZ. What is wrong with this code that uses default arguments?

Lab # 2. For today s lab:

CSE 374 Programming Concepts & Tools

Welcome Back! Without further delay, let s get started! First Things First. If you haven t done it already, download Turbo Lister from ebay.

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

def F a c t o r i a l ( n ) : i f n == 1 : return 1 else : return n F a c t o r i a l ( n 1) def main ( ) : print ( F a c t o r i a l ( 4 ) )

CS 1110, LAB 3: MODULES AND TESTING First Name: Last Name: NetID:

Lecture 3: Processing Language Data, Git/GitHub. LING 1340/2340: Data Science for Linguists Na-Rae Han

Sorting. Dr. Baldassano Yu s Elite Education

Anders Fröberg TDDD80 STORAGE AND TESTING

GOOGLE APPS. GETTING STARTED Page 02 Prerequisites What You Will Learn. INTRODUCTION Page 03 What is Google? SETTING UP AN ACCOUNT Page 03 Gmail

Chapter 2 Programming in Python

Introduction to User Stories. CSCI 5828: Foundations of Software Engineering Lecture 05 09/09/2014

Game keystrokes or Calculates how fast and moves a cartoon Joystick movements how far to move a cartoon figure on screen figure on screen

Git Source Control: For the Rest of Us. Nolan Erck

WHAT IS GOOGLE+ AND WHY SHOULD I USE IT?

MARKETING VOL. 1

SECTION 2: HW3 Setup.

CIT 590 Homework 5 HTML Resumes

Graph Algorithms: Part 1. Dr. Baldassano Yu s Elite Education

Introduction to Scientific Computing

Close Your File Template

Excel Basics Fall 2016

Using GitHub to Share with SparkFun a

CS1 Lecture 5 Jan. 25, 2019

CS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise

OPEN THE HOTLINE CLIENT

If Statements, For Loops, Functions

Git Introduction CS 400. February 11, 2018

SECTION 1: CODE REASONING + VERSION CONTROL + ECLIPSE

2016 All Rights Reserved

Filter and PivotTables in Excel

Privacy and Security in Online Social Networks Department of Computer Science and Engineering Indian Institute of Technology, Madras

Introduction to Python (All the Basic Stuff)

CS Summer 2013

For Volunteers An Elvanto Guide

Transcription:

Programming in the Real World Dr. Baldassano chrisb@princeton.edu Yu s Elite Education

Our programs are getting bigger! Our game was already over 100 lines long - most programs are worked on by teams of people for months or years What kinds of problems start appearing in big programs?

#1: Communicating with code Programs are instructions for a computer, but also need to be understandable to humans Other people working on your program Yourself in the future, after some time has passed Coding in a way that is easy for humans to work with is called good programming style

Importance of style What does this code do? def DoTheThing(input) looper = sys.getsizeof(input) - \ sys.getsizeof('') i = 0 while i < looper: print(chr(ord(word[0]) - \ 32*(ord(word[0]) > 96)))

Importance of style What does this code do? def PrintUppercase(word) for i in range(len(word)): print(word[i].upper())

Importance of Style Sun Microsystems (creators of Java) pushed for coding style because: 80% of the lifetime cost of a piece of software goes to maintenance. Hardly any software is maintained for its whole life by the original author. Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly.

Basics of good style Use comments to explain complicated code Give variables useful names Break up the program into classes and functions Simple code is better than clever and fast code in many cases No magic numbers don t use hard-coded numbers without giving them a name Be consistent with tabs/spacing and variable naming (capitalization, using underscores _, etc.)

Programming Style All of these sum up 3 random numbers. Which of these has the best style? a) firstone = random.randint(0,10) For i in range(4/2): firstone = firstone + random.randint(0,10) b) variable = random.randint(0,10) + \ random.randint(0,10) + \ random.randint(0,10) c) randsum = 0 for i in range(3): randsum += random.randint(0,10)

Be a polite coder! Whenever you re writing code that you expect will be used for more than a few days, pay attention to programming style! Lots of online resources, like https://google.github.io/styleguide/pyguide.html Always code as if the person who ends up maintaining your code is a violent psychopath who knows where you live.

#2: Debugging programs Bugs are errors in programs Very first bug was found by Grace Hopper s research group in 1947

Two types of bugs Syntax bugs These crash the program because of an invalid command Usually the error tells us what we did wrong Runtime bugs The program runs, but gives the wrong answer! How can we fix these? What if the program is really big?

Debugging A debugger lets us run a program step-by-step to figure out where the error is It shows you the values of all variables, and sometimes will even let you try out changes without restarting the program

IDLE Debugger IDLE comes with a very basic debugger, under Debug->Debugger Step steps to next line (and goes into functions) Over steps to next line (but skips function calls) Can set breakpoints to quickly run to the part of the program causing trouble

#3: Testing Programs How can we make sure our program is working correctly? Can we ever be positive? How sure should we be, if: We re writing a game for fun? We re writing a game to sell? We re writing business software that makes paychecks? We re writing software to control pacemakers? We re designing a firewall for nuclear codes?

Famous Bugs: Therac-25 Therac-25 was a radiation therapy machine in 1985 At least six people were given massive radiation overdoses due to a software bug

Famous Bugs: Ariane 5 In 1996, the European Space Agency s prototype Ariane 5 rocket (costing $1 billion) blew up after launching, due to a type error bug

Famous Bugs: Mars Climate Orbiter In 1999, a NASA probe to Mars burned up in Martian atmosphere, due to a bug in unit conversion between two of its subsystems

Famous Bugs: 2003 Blackout In 2003, the Northeast US and Canada had an electrical grid blackout for 7 hours, due to a bug in the alarm system of an Ohio power plant

Bugs are everywhere More famous bugs: https://en.wikipedia.org/wiki/list_of_software_bugs US NIST estimates that software bugs cost the US about $59 billion every year Our best defense against bugs: testing!

Automated testing If we care about our program being correct, we need to write another program, to test our program This test program will run our code with lots of inputs for which we know the right answer, and make sure that everything matches up Sometimes you work with a team to write the tests first, so that everyone agrees on exactly what the program should do before you write it

Running tests Let s say my testing program runs 10 tests The first 9 pass, but the last one fails I figure out the mistake and think I ve fixed it Which tests should I re-run? Regression testing: Always re-run all tests, to make sure our fix didn t break something else!

Testing example What would be good test cases for: A program that calculates the day of the week for a given date?

Testing example What would be good test cases for: A website where people can buy products?

Testing example What would be good test cases for: A program that shuffles a deck of cards?

Tests in big programs How can we write tests for big programs, like a big video game (League of Legends)? Testing the whole thing at once is good, but we also need to test each piece of the program individually Unit tests are small tests that only test one particular part of a program (like one function) Why are these useful?

#4: Keeping track of code Two related problems when working on a programming project: How can we coordinate between multiple people working on the same program? How can we keep track of the changes we re making, in case we break something and need to undo changes?

Version Control Version Control systems are a way to keep a master copy of our program Keep track of edits over time, so we can always roll back to a previous version Allows multiple people to merge together their work into a single master copy Originally designed for code, but these can be used for other things too, like websites or even just text documents!

Version Control Systems Most popular version control system today is called git Free services like GitHub and BitBucket can back up your master copy, and let others view your code or even propose changes! For any project you ll be working on for more than a few weeks and/or with other people, definitely think about using version control Git has a great tutorial online at https://try.github.io/levels/1/challenges/1

#5: Software for writing programs This seems like a lot to deal with keeping track of programming style, debuggers, testing, version control Most programmers use an Integrated Development Environment (IDE) that lets them write code and do all of these other important things within a single piece of software We ve been using IDLE, which is a very basic IDE Some IDE features we ve been using: syntax highlighting, function hints, automatic indenting

#5: Python IDEs: PyCharm

#5: Python IDEs: Spyder

#5: Python IDEs: Komodo

IDE Features Code completion will try to autocomplete variable and function names Syntax highlighting will colorize text to make syntax more clear, and flag lines with syntax errors Code style suggestions will flag lines that may not be formatted clearly (though a lot is still up to you) Integrated debugging unlike IDLE, you can step through your code right in the same window Unit testing can define tests for individual classes, and have them run automatically Version control can show you whether each file is upto-date or how it differs from the master version

Summary For building real programs, we need to think about some new issues: Coding style Debugging Testing Version control IDEs All of these things have to be learned through practice, and every programmer develops their own set of tools that they like to use

Homework: Debugging a program Download the palindrome_buggy.py file from the website The program is supposed to check if a string is a palindrome (same forwards and backwards) but has bugs and is failing its tests Use the IDLE debugger to help find the bugs so that it passes all tests