Making Good Code AKA: So, You Wrote Some Code. Now What? Ray Haggerty July 23, 2015.

Slides:



Advertisements
Similar presentations
Introducing JavaScript
Advertisements

CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 7: Errors 1 Based on slides created by Bjarne Stroustrup.
Programming Types of Testing.
16/13/2015 3:30 AM6/13/2015 3:30 AM6/13/2015 3:30 AMIntroduction to Software Development What is a computer? A computer system contains: Central Processing.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
CS 201 Functions Debzani Deb.
Testing an individual module
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Programming Fundamentals (750113) Ch1. Problem Solving
1 These courseware materials are to be used in conjunction with Software Engineering: A Practitioner’s Approach, 5/e and are provided with permission by.
Exceptions COMPSCI 105 S Principles of Computer Science.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Chapter 1 Introduction.
P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science.
© Janice Regan, CMPT 128, Jan CMPT 128 Introduction to Computing Science for Engineering Students Creating a program.
Structured COBOL Programming, Stern & Stern, 9th edition
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
GENERAL CONCEPTS OF OOPS INTRODUCTION With rapidly changing world and highly competitive and versatile nature of industry, the operations are becoming.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
1 Computing Software. Programming Style Programs that are not documented internally, while they may do what is requested, can be difficult to understand.
ICAPRG301A Week 4Buggy Programming ICAPRG301A Apply introductory programming techniques Program Bugs US Navy Admiral Grace Hopper is often credited with.
General Programming Introduction to Computing Science and Programming I.
How to think through your program [ principles of good program design ] Rachel Denison MATLAB for Cognitive Neuroscience ICN, 13 December 2007.
The Java Programming Language
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
Input, Output, and Processing
Python From the book “Think Python”
Object Oriented Software Development 8. Exceptions, testing and debugging.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Testing Methods Carl Smith National Certificate Year 2 – Unit 4.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
Software Development Process.  You should already know that any computer system is made up of hardware and software.  The term hardware is fairly easy.
DEBUGGING. BUG A software bug is an error, flaw, failure, or fault in a computer program or system that causes it to produce an incorrect or unexpected.
Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Controlling Execution Programming Right from the Start with Visual Basic.NET 1/e 8.
FT228/3 Web Development Error processing. Introduction READ Chapter 9 of Java Server Pages from O’reilly 2 nd Edition Need to be able to 1) Diagnose and.
1 Program Planning and Design Important stages before actual program is written.
CSE 351 GDB Introduction. Lab 1 Status? How is Lab 1 going? I’ll be available at the end of class to answer questions There are office hours later today.
Intermediate 2 Computing Unit 2 - Software Development.
School of Computer Science & Information Technology G6DICP - Lecture 6 Errors, bugs and debugging.
The Hashemite University Computer Engineering Department
Error Handling Tonga Institute of Higher Education.
Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging.
5.01 Understand Different Types of Programming Errors
The single most important skill for a computer programmer is problem solving Problem solving means the ability to formulate problems, think creatively.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
COMPUTER PROGRAMMING I SUMMER Understand Different Types of Programming Errors.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
1 ENERGY 211 / CME 211 Lecture 14 October 22, 2008.
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
Development Environment
Introduction to Computing Science and Programming I
5.01 Understand Different Types of Programming Errors
14 Compilers, Interpreters and Debuggers
Component 1.6.
Lecture 1 Introduction Richard Gesick.
Testing and Debugging.
Chapter 13 & 14 Software Testing Strategies and Techniques
5.01 Understand Different Types of Programming Errors
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
Programming Fundamentals (750113) Ch1. Problem Solving
Part B – Structured Exception Handling
CS 1111 Introduction to Programming Fall 2018
Programming Fundamentals (750113) Ch1. Problem Solving
Programming Fundamentals (750113) Ch1. Problem Solving
Review of Previous Lesson
Chapter 11: Exception Handling
Presentation transcript:

Making Good Code AKA: So, You Wrote Some Code. Now What? Ray Haggerty July 23, 2015

Step 1: Debugging

What is Debugging? The term “debugging” was popularized in the 1940s by Admiral Grace Hopper Debugging is, essentially, the fixing of errors in your code

The Three Major Types of Bugs Compilation Errors These prevent your code from executing at all Examples: mismatched delimiters, not ending loops Run-time Errors These occur when your code attempts an impossible operation Examples: dividing by zero, calling a function that doesn’t exist Logic Errors These will not throw an error, but give unexpected results Example: using ‘and’ & ‘or’ incorrectly, off by one

Quick & Dirty: Using Print Statements

ExpectationReality

Interpreting Error Messages References script or function error was generated in Gives the line at which the error was detected This does not necessarily mean the line the error is on Short explanation of what the computer attempted and couldn’t accomplish

Absolutely Correct: Using a Debugger Debuggers act like version control for your programs, saving the state of every variable at every instruction When the program breaks, you may execute instructions on the last state of the program Also allows backtracing to find the faulty piece of code directly

Absolutely Correct: Using a Debugger Debuggers require a debug-mode compile and are slow and memory intensive Check your code in release mode after running it through the debugger. Sample debuggers: Python includes the extensible Pdb debugger MATLAB has sldebug, the simulink debugger R can use the Dr. Mingw “just-in-time” debugger

Step 2: Testing

What is Testing? Just because your code worked on one specific problem, that DOES NOT mean it will necessarily work on other, similar problems Just because you didn’t see an error message, that DOES NOT mean, it is running correctly Testing is a way to ensure that your code will behave appropriately under the circumstances it promises to Need to make sure that your output is actually scientifically correct

The Four Major Testing Cases Base Case This is expected input Edge Case This is a minimum or maximum acceptable parameter Corner Case This is the intersection of multiple Edge Cases Boundary Case This is near the Edge Case, on either side

Quick & Dirty: Test Various Inputs Manually test your code by testing each of the types of testing cases and comparing the results to what you expect For example if the function expects the input to be between 0 and 100, we might test the following inputs: 22, 50, 89 (Base Cases) 0, 100 (Edge Cases) -1, 1, 99, 101 (Boundary Cases)

Absolutely Correct: Unit Testing Unit tests are pieces of test code that exist simply for the purpose of testing your code Since they exist outside of the code, they are durable. The same unit test can check multiple versions of your code However, you need to be very careful writing them Good idea: write a unit test every time you fix a bug

Exception Handling When your code encounters an error, you want it to alert the user to the problem and exit as gracefully as possible Exception Handling changes the flow of a program to handle abnormal or unexpected conditions For example, if a FASTA file contains an unexpected symbol- tell where it is and be informative

Exception Handling Try/Catch Statements allows you to override the default error behavior

Exception Handling Asserts Allows you to throw custom errors if condition is not met

Step 3: Using Best Practices

Why Should You Use Best Practices? In all likelihood, you will not be the only person using or looking at your code In all likelihood, you will want to make revisions to your own code at some point In all likelihood, you will want to have fast code

Encapsulation Encapsulation is breaking your code down into smaller parts and assembling them like legos This is actually a somewhat complex topic, but in the simplest sense, it is breaking out sections of your code into new subfunctions, and calling those within your main script or function Encapsulation has several advantages: Code readability Better for testing Future-proofing Code reuse Controls variable scope issues

Code Testing Since encapsulation breaks your code down into individual functions, it makes unit testing very straightforward Ensuring that each subfunction works appropriately is critical for the other benefits of encapsulation

Future-Proofing Since your code is encapsulated into small, independent blocks, it is very easy to reuse and replace each subfunction If you decide you want to change the way part of your code works, you can easily replace it without worrying about the entire script breaking

Code Reuse Conversely, you may find yourself needing to write a lot of different scripts that perform the same function(s) You could just copy/paste, but that is sloppy and could introduce errors If you’ve already used encapsulation, then you can easily reuse a subfunction in a new script

Variable Scope The scope of a variable refers to which parts of the code can “see” and access that variable Generally, you want your variables to be of a smallest scope possible, so they are protected There are lots of different scopes, but the two main ones are: Local Only visible to a small section of the code (e.g. a subfunction) Non-local Visible to a larger section or all of the code

Header Comments A header comment is a block comment at the top of your code to help readers of your code It should include: Explanation of what the code does (Input/Output) Name and contact info Date edited Requirements (packages, software versions)

Header Comment Examples