API Design CPSC 315 – Programming Studio Fall 2008 Follows Kernighan and Pike, The Practice of Programming and Joshua Bloch’s Library-Centric Software.

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

Coding Standard: General Rules 1.Always be consistent with existing code. 2.Adopt naming conventions consistent with selected framework. 3.Use the same.
Test-Driven Development and Refactoring CPSC 315 – Programming Studio.
Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
© The McGraw-Hill Companies, 2006 Chapter 15. © The McGraw-Hill Companies, 2006 Exceptions an exception is an event that occurs during the life of a program.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
Debugging Techniques1. 2 Introduction Bugs How to debug Using of debugger provided by the IDE Exception Handling Techniques.
Lecture 28 More on Exceptions COMP1681 / SE15 Introduction to Programming.
Road Map Introduction to object oriented programming. Classes
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Portability CPSC 315 – Programming Studio Spring 2008 Material from The Practice of Programming, by Pike and Kernighan.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
. Memory Management. Memory Organization u During run time, variables can be stored in one of three “pools”  Stack  Static heap  Dynamic heap.
From C++ to C#. Web programming The course is on web programming using ASP.Net and C# The course is on web programming using ASP.Net and C# ASP.Net is.
1 Shawlands Academy Higher Computing Software Development Unit.
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from.
Chapter 12: Exception Handling
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
COSC 1P03 Data Structures and Abstraction 4.1 Abstract Data Types The advantage of a bad memory is that one enjoys several times the same good things for.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Object Oriented Programming Elhanan Borenstein Lecture #4.
Exceptions1 Syntax, semantics, and pragmatics. Exception create If (some error){ throw new SomeException(”some message”); } Exceptions2.
SE: CHAPTER 7 Writing The Program
23-Oct-15 Abstract Data Types. 2 Data types A data type is characterized by: a set of values a data representation, which is common to all these values,
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.
Testing and Debugging Session 9 LBSC 790 / INFM 718B Building the Human-Computer Interface.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Code Complete Steve McConnell. 20. The Software-Quality Landscape.
The Software Development Process
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Design - programming Cmpe 450 Fall Dynamic Analysis Software quality Design carefully from the start Simple and clean Fewer errors Finding errors.
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
Software Engineering and Object-Oriented Design Topics: Solutions Modules Key Programming Issues Development Methods Object-Oriented Principles.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
FIT Objectives By the end of this lecture, students should: understand the role of constructors understand how non-default constructors are.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
Collections Using Generics in Collections. 2 Chapter Objectives Define the concept and terminology related to collections Explore the basic structure.
Effective Java, Chapter 9: Exceptions Items Last modified Fall 2012 Paul Ammann.
Chapter 10 Software quality. This chapter discusses n Some important properties we want our system to have, specifically correctness and maintainability.
Introduction to Exceptions in Java CS201, SW Development Methods.
1 Handling Errors and Exceptions Chapter 6. 2 Objectives You will be able to: 1. Use the try, catch, and finally statements to handle exceptions. 2. Raise.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
CS 265 Jianbo Zhao. What is interface? Interface is a detailed boundary between code that provides a service and code that uses it. An interface defines.
Eighth Lecture Exception Handling in Java
Copyright © Jim Fawcett Spring 2017
Data Abstraction: The Walls
Jim Fawcett CSE687 – Object Oriented Design Spring 2001
Jim Fawcett CSE687 – Object Oriented Design Spring 2015
Distributed Shared Memory
Review What is an object? What is a class?
Portability CPSC 315 – Programming Studio
Effective Java, 3rd Edition Chapter 10: Exceptions
CMSC 345 Programming.
SWE 619 Last modified Fall 2007 Saket Kaushik, Paul Ammann
Interface Principles Omar Kabeer.
Effective Java, Chapter 9: Exceptions
Lecture 7: RPC (exercises/questions)
CMSC 202 Exceptions.
Presentation transcript:

API Design CPSC 315 – Programming Studio Fall 2008 Follows Kernighan and Pike, The Practice of Programming and Joshua Bloch’s Library-Centric Software Design ’05 Keynote Talk: ”How to Design a Good API and Why It Matters”

API Application Programming Interface Source code interface For library or OS Provides services to a program At its base, like a header file But, more complete

Why is API Design Important? Company View Can be asset – big user investment in learning and using Bad design can be source of long-term support problems Once used, it’s tough to change Especially if there are several users Public APIs – One chance to get it right

Characteristics of Good APIs Easy to learn Easy to use even without documentation Hard to misuse Easy to read and maintain code that uses it Sufficiently powerful to satisfy requirements Easy to extend Appropriate to audience

Designing an API Gather requirements Don’t gather solutions Extract true requirements Collect specific scenarios where it will be used Create short specification Consult with users to see whether it works Flesh it out over time Hints: Write plugins/use examples before fully designed and implemented Expect it to evolve

Broad Issues to Consider in Design 1. Interface The classes, methods, parameters, names 2. Resource Management How is memory, other resources dealt with 3. Error Handling What errors are caught and what is done Information Hiding How much detail is exposed Impacts all three of the above

1. Interface Principles Simple General Regular Predictable Robust Adaptable

Simple Users have to understand! Do one thing and do it well Functionality should be easy to explain As small as possible, but never smaller Conceptual weight more important than providing all functionality Avoid long parameter lists Choose small set of orthogonal primitives Don’t provide 3 ways to do the same thing

General Implementation can change, API can’t Hide Information! Don’t let implementation detail leak into API Minimize accessibility (e.g. private classes and members) Implementation details can confuse users Be aware of what is implementation Don’t overspecify behavior of modules Tuning parameters are suspect

Regular Do the same thing the same way everywhere Related things should be achieved by related means Consistent parameter ordering, required inputs Functionality (return types, errors, resource management) Names matter Self explanatory Consistent across API Same word means same thing in API Same naming style used Consistent with related interfaces outside the API

Predictable Don’t violate the principle of Least Astonishment User should not be surprised by behavior Even if this costs performance Don’t reach behind the user’s back Accessing and modifying global variables Secret files or information written Be careful about static variables

Predictable Try to minimize use of other interfaces Make as self-contained as possible Be explicit about external services required Document! Every class, method, interface, constructor, parameter, exception When states are kept, this should be very clearly documented

Robust Able to deal with unexpected input Error Handling (see later)

Adaptable API can be extended, but never shortened Heavily used APIs likely will be extended Information Hiding Implementation details should not affect API

2. Resource Management Determine which side is responsible for Initialization Maintaining state Sharing and copying Cleaning up Various resources Memory Files Global variables

Resource Management Generally, free resources where they were allocated Return references or copies? Can have huge performance and ease of use impact Multi-threaded code makes this especially critical Reentrant: works regardless of number of simultaneous executions Avoid using anything (globals, static locals, other modifications) that others could also use Locks can be important

3. Error Handling Catch errors, don’t ignore them “Print message and fail” is not always good Especially in APIs Need to allow programs to recover or save data Detect at low level, but handle at high level Generally, error should be handled by calling routine The callee can leave things in a “nice” state for recovery, though Keep things usable in case the caller can recover

Fail Fast Report as soon as an error occurs Sometimes even at compile time! Use of static types, generics

Error Management Return values Should be in form the calling function can use Return as much useful information as possible Sentinel values only work if function cannot return all possible values of that type Define pairs, or return another parameter to indicate errors Use error “wrapper function” if needed Consistent way of marking, reporting error status Encourages use But, can add complexity

Exceptions Generally indicate a programming error Programming construct Set exception value (e.g. as return) Other program operation when exception thrown Exceptions usually in global registry Include information about failure For repair and debugging Exceptions should generally be unchecked Automatically process globally, rather than require explicit checks over and over

Exceptions Only use in truly exceptional situations Never use as a control structure The modern GOTO Never use exceptions for expected return values e.g. Invalid file name passed to library is “common”, not an exception