SlideShare una empresa de Scribd logo
1 de 16
Descargar para leer sin conexión
Week 10
Tracing, Testing,
Debugging, API
Design
© 2004 Pearson Addison-Wesley. All rights reserved 5-2
Copyright Warning
COMMONWEALTH OF AUSTRALIA
Copyright Regulations 1969
WARNING
This material has been copied and communicated to you by or
on behalf of Bond University pursuant to Part VB of the
Copyright Act 1968 (the Act).
The material in this communication may be subject to copyright
under the Act. Any further copying or communication of this
material by you may be the subject of copyright protection
under the Act.
© 2004 Pearson Addison-Wesley. All rights reserved 5-3
Robust Programs
• We have seen that our programs can be fragile
• Programs also need to be read by humans so they
can be fixed and extended
• This week we are looking at techniques to make
our programs more robust, and more easily
understood by other programmers
• We will be looking at:
– Tracing, debugging and unit testing
– Good API design
© 2004 Pearson Addison-Wesley. All rights reserved 5-4
Why Do Programs Have Bugs?
• Programs have bugs for several reasons:
– Computers DWIS not DWIM
– Inputs can have any or all possible values
– We don't understand the language subtleties
– Our understanding of the interfaces between the
modules in our program, and with external
modules, is wrong
• Let's look at some ways of combatting these
problems
© 2004 Pearson Addison-Wesley. All rights reserved 5-5
Tracing Programs
• We don't think like a computer, but to write good
programs we need to.
• Tracing a program means to step through some
code, by hand, and track all the variable values
and decisions made, to ensure that the code is
doing exactly what it should be doing.
• If we don't understand our own code, we can fall
into the trap of shotgun debugging: making
random changes in the hope that any problems
will disappear.
© 2004 Pearson Addison-Wesley. All rights reserved 5-6
Tracing Programs
• Trace the following code when it is given the input “Paul McCartney”
public static String changeString(String s)
{
String x = "";
for (int i=0; i < s.length(); i += 2)
{
if (s.charAt(i) < 'M')
x = x + ".";
x = x + Character.toUpperCase( s.charAt(i) );
}
return(x);
}
© 2004 Pearson Addison-Wesley. All rights reserved 5-7
Tracing Programs
• Why tracing?
• It forces us to read each line of code, think about
what it is doing, why it is there, and what side
effects each line has
• It also makes us do things slowly
• We will focus more deeply on the code
• Sometimes, we also reflect on questions such as
the quality of the code, could it be done better etc.
© 2004 Pearson Addison-Wesley. All rights reserved 5-8
Debugging Programs
• Debugging is like tracing but the computer looks
after the display of variables and the stepping
through the code
• Don't start at main(). Try to identify where the bug
is, and debug from just before the bug
• Learn how to:
– Set up breakpoints (stop signs)
– Step over lines of code
– Visualise the variables' values
– Step over, and into, methods
© 2004 Pearson Addison-Wesley. All rights reserved 5-9
Debugging Programs
• Find the bug(s) in this method:
// Return the position of the first ch in the String,
// or return -1 if ch is not in the String.
public static int findLetter(String str, char ch)
{
for (int i=0; i <= str.length(); i++)
if (str.charAt(i) == ch)
return(i);
return(-1);
}
© 2004 Pearson Addison-Wesley. All rights reserved 5-10
Methods and Inputs
• Every method has a declaration, which is its
contract: if you give me this input, I promise to
return this value
• Inputs, regardless of where they come from, are
problematic
• If an input is of type X, any valid X value may come
in as input:
 int: zero, negative values, large values
 floats: 0.0, INFINITY, very small values
 Strings: empty String, null
 char: non-printable chars, non-English chars
© 2004 Pearson Addison-Wesley. All rights reserved 5-11
Unit Testing
• All large programs have bugs: usual estimate is 1
bug per 100 lines of code
• Most bugs are logic bugs: the programmer thinks
it works, but the code does something different
• It's nearly impossible to tell if a program is 100%
bug-free
• Implication: programs should be tested thorougly
to give assurance there are no bugs
• Unit testing: test a method with a large set of
inputs, to ensure that it produces the correct
output
• The test set is designed to stress the method as
much as possible
© 2004 Pearson Addison-Wesley. All rights reserved 5-12
Unit Testing
• You should design the method interface first
• THEN, construct a suitable test set for it
 Include good and bad inputs
• ONLY THEN, write the actual method code
• Once you think you have written the method, you
can then test the method with the unit tests
• “Coding isn't complete until all the unit tests pass”
• ALWAYS generate the tests by hand. Don't use a
program to generate the test set. Why not?
• Bluej provides a framework to construct unit tests.
• You will see it in the labs, and you will be
assessed on unit testing
© 2004 Pearson Addison-Wesley. All rights reserved 5-13
Unit Test Example
• Design the declaration of a method isATriangle()
• Given 3 side lengths, return true if they represent a
triangle, false otherwise
• Create a set of unit tests (inputs, output) that you
can use to test the method.
• In BlueJ, write the method to always return false
 So we can write the unit tests, which will fail
• Now go and write the code.
• Once all the unit tests pass, you now have
confidence that the code works
© 2004 Pearson Addison-Wesley. All rights reserved 5-14
API Design
• A method provides an interface: name, inputs,
output
• A Java class provides a set of interfaces, known
as an API: application program interface
• APIs can be designed well, or extremely bad
• Here are some tips on good API design
• Names mean everything. Choose wisely
• Methods perform actions. Use action names, e.g.
getUserName() not UserName()
• Make it obvious if a method is a getter or a setter
• Boolean methods: use “Is”, e.g. isEnrolled()
© 2004 Pearson Addison-Wesley. All rights reserved 5-15
API Design
• Method arguments: keep consistent order across
the methods in the API, e.g. (hroom, wroom) in
Assignment 2.
• Make it hard for user to get wrong, easy to get
right
 e.g if method takes 2 ints and a char, make it
method(int, char, int), so hard to get the ints the
wrong way around
• Encapsulation: private data, public methods,
public constants, private helper methods
© 2004 Pearson Addison-Wesley. All rights reserved 5-16
API Design
• A method provides an interface: name, inputs,
output
• Clear and concise documentation at the top of
each method tells the user what it does, and how it
deals with input errors.
• Design a clear description of the isATriangle()
method

Más contenido relacionado

La actualidad más candente

Code Review
Code ReviewCode Review
Code ReviewDivante
 
Diffy : Automatic Testing of Microservices @ Twitter
Diffy : Automatic Testing of Microservices @ TwitterDiffy : Automatic Testing of Microservices @ Twitter
Diffy : Automatic Testing of Microservices @ TwitterPuneet Khanduri
 
YAGNI Principle and Clean Code
YAGNI Principle and Clean CodeYAGNI Principle and Clean Code
YAGNI Principle and Clean CodeLuan Reffatti
 
Coding standards
Coding standardsCoding standards
Coding standardsMimoh Ojha
 
Introduction to Pseudocode
Introduction to PseudocodeIntroduction to Pseudocode
Introduction to PseudocodeSabahtHussein
 
PART 10 - Python Tutorial | Functions In Python With Examples
PART 10 - Python Tutorial | Functions In Python With ExamplesPART 10 - Python Tutorial | Functions In Python With Examples
PART 10 - Python Tutorial | Functions In Python With ExamplesShivam Mitra
 
Chapter 7 review questions
Chapter 7 review questionsChapter 7 review questions
Chapter 7 review questionsloayshabaneh
 
Chapter 8 review questions
Chapter 8 review questionsChapter 8 review questions
Chapter 8 review questionsloayshabaneh
 
Best Practices of Software Development
Best Practices of Software DevelopmentBest Practices of Software Development
Best Practices of Software DevelopmentFolio3 Software
 
PART 0 - Python Tutorial | Why should you learn python
PART 0 - Python Tutorial | Why should you learn pythonPART 0 - Python Tutorial | Why should you learn python
PART 0 - Python Tutorial | Why should you learn pythonShivam Mitra
 
Clean code - Getting your R&D on board
Clean code - Getting your R&D on boardClean code - Getting your R&D on board
Clean code - Getting your R&D on boardRuth Sperer
 
Python programming introduction
Python programming introductionPython programming introduction
Python programming introductionSiddique Ibrahim
 
An Introduction To Software Development - Test Driven Development, Part 1
An Introduction To Software Development - Test Driven Development, Part 1An Introduction To Software Development - Test Driven Development, Part 1
An Introduction To Software Development - Test Driven Development, Part 1Blue Elephant Consulting
 

La actualidad más candente (20)

Code Inspection
Code InspectionCode Inspection
Code Inspection
 
Code Review
Code ReviewCode Review
Code Review
 
Unit tests benefits
Unit tests benefitsUnit tests benefits
Unit tests benefits
 
Microservices
MicroservicesMicroservices
Microservices
 
Diffy : Automatic Testing of Microservices @ Twitter
Diffy : Automatic Testing of Microservices @ TwitterDiffy : Automatic Testing of Microservices @ Twitter
Diffy : Automatic Testing of Microservices @ Twitter
 
YAGNI Principle and Clean Code
YAGNI Principle and Clean CodeYAGNI Principle and Clean Code
YAGNI Principle and Clean Code
 
Coding standards
Coding standardsCoding standards
Coding standards
 
Introduction to Pseudocode
Introduction to PseudocodeIntroduction to Pseudocode
Introduction to Pseudocode
 
PART 10 - Python Tutorial | Functions In Python With Examples
PART 10 - Python Tutorial | Functions In Python With ExamplesPART 10 - Python Tutorial | Functions In Python With Examples
PART 10 - Python Tutorial | Functions In Python With Examples
 
Code Review
Code ReviewCode Review
Code Review
 
Chapter 7 review questions
Chapter 7 review questionsChapter 7 review questions
Chapter 7 review questions
 
Code Review
Code ReviewCode Review
Code Review
 
Chapter 8 review questions
Chapter 8 review questionsChapter 8 review questions
Chapter 8 review questions
 
Best Practices of Software Development
Best Practices of Software DevelopmentBest Practices of Software Development
Best Practices of Software Development
 
PART 0 - Python Tutorial | Why should you learn python
PART 0 - Python Tutorial | Why should you learn pythonPART 0 - Python Tutorial | Why should you learn python
PART 0 - Python Tutorial | Why should you learn python
 
Unit testing in PHP
Unit testing in PHPUnit testing in PHP
Unit testing in PHP
 
Clean code - Getting your R&D on board
Clean code - Getting your R&D on boardClean code - Getting your R&D on board
Clean code - Getting your R&D on board
 
Code Review
Code ReviewCode Review
Code Review
 
Python programming introduction
Python programming introductionPython programming introduction
Python programming introduction
 
An Introduction To Software Development - Test Driven Development, Part 1
An Introduction To Software Development - Test Driven Development, Part 1An Introduction To Software Development - Test Driven Development, Part 1
An Introduction To Software Development - Test Driven Development, Part 1
 

Destacado (20)

Ddp pp1
Ddp pp1Ddp pp1
Ddp pp1
 
Tipos de educación
Tipos de educaciónTipos de educación
Tipos de educación
 
Diário Oficial de Guarujá
Diário Oficial de GuarujáDiário Oficial de Guarujá
Diário Oficial de Guarujá
 
En la esquina
En la esquina En la esquina
En la esquina
 
Week09
Week09Week09
Week09
 
Week06
Week06Week06
Week06
 
Desafio de ser docente
Desafio de ser docenteDesafio de ser docente
Desafio de ser docente
 
English workout visual
English workout visualEnglish workout visual
English workout visual
 
Q7
Q7Q7
Q7
 
C has zemli_2013_dlja_shkol
C has zemli_2013_dlja_shkolC has zemli_2013_dlja_shkol
C has zemli_2013_dlja_shkol
 
Entorno de trabajo de power point,,alejandra lara suñiga
Entorno de trabajo de power point,,alejandra lara suñigaEntorno de trabajo de power point,,alejandra lara suñiga
Entorno de trabajo de power point,,alejandra lara suñiga
 
Sdec boletin febrero 2013
Sdec boletin febrero 2013Sdec boletin febrero 2013
Sdec boletin febrero 2013
 
Ags,anticuerpos,hla 11
Ags,anticuerpos,hla 11Ags,anticuerpos,hla 11
Ags,anticuerpos,hla 11
 
pengertian, objek kajian, tujuan dan manfaat studi Masail fiqhiyah
pengertian, objek kajian, tujuan dan manfaat studi Masail fiqhiyahpengertian, objek kajian, tujuan dan manfaat studi Masail fiqhiyah
pengertian, objek kajian, tujuan dan manfaat studi Masail fiqhiyah
 
Is your buidling an asset alan trauger
Is your buidling an asset   alan traugerIs your buidling an asset   alan trauger
Is your buidling an asset alan trauger
 
Abr13 boletin del cepails
Abr13 boletin del cepailsAbr13 boletin del cepails
Abr13 boletin del cepails
 
Fdi ch.7
Fdi ch.7Fdi ch.7
Fdi ch.7
 
Social realism
Social realismSocial realism
Social realism
 
Week07
Week07Week07
Week07
 
Week08
Week08Week08
Week08
 

Similar a Week10style

Week12
Week12Week12
Week12hccit
 
Week05
Week05Week05
Week05hccit
 
How to Actually DO High-volume Automated Testing
How to Actually DO High-volume Automated TestingHow to Actually DO High-volume Automated Testing
How to Actually DO High-volume Automated TestingTechWell
 
Software testing
Software testingSoftware testing
Software testingBala Ganesh
 
Automation Testing theory notes.pptx
Automation Testing theory notes.pptxAutomation Testing theory notes.pptx
Automation Testing theory notes.pptxNileshBorkar12
 
Introduction of Software Engineering
Introduction of Software EngineeringIntroduction of Software Engineering
Introduction of Software EngineeringZafar Ayub
 
SE - Lecture 8 - Software Testing State Diagram.pptx
SE - Lecture 8 - Software Testing  State Diagram.pptxSE - Lecture 8 - Software Testing  State Diagram.pptx
SE - Lecture 8 - Software Testing State Diagram.pptxTangZhiSiang
 
Indy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-muleIndy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-muleikram_ahamed
 
How to Test PowerShell Code Using Pester
How to Test PowerShell Code Using PesterHow to Test PowerShell Code Using Pester
How to Test PowerShell Code Using PesterChris Wahl
 
Test What Matters Most
Test What Matters MostTest What Matters Most
Test What Matters MostRemedy IT
 
Dev buchan 30 proven tips
Dev buchan 30 proven tipsDev buchan 30 proven tips
Dev buchan 30 proven tipsBill Buchan
 
DSR Testing (Part 1)
DSR Testing (Part 1)DSR Testing (Part 1)
DSR Testing (Part 1)Steve Upton
 
Six simple steps to unit testing happiness
Six simple steps to unit testing happinessSix simple steps to unit testing happiness
Six simple steps to unit testing happinessSteven Feuerstein
 
Transferring Software Testing Tools to Practice
Transferring Software Testing Tools to PracticeTransferring Software Testing Tools to Practice
Transferring Software Testing Tools to PracticeTao Xie
 
The Professional Programmer
The Professional ProgrammerThe Professional Programmer
The Professional ProgrammerDave Cross
 

Similar a Week10style (20)

Week12
Week12Week12
Week12
 
Week05
Week05Week05
Week05
 
Slides chapters 13-14
Slides chapters 13-14Slides chapters 13-14
Slides chapters 13-14
 
Software testing
Software testingSoftware testing
Software testing
 
How to Actually DO High-volume Automated Testing
How to Actually DO High-volume Automated TestingHow to Actually DO High-volume Automated Testing
How to Actually DO High-volume Automated Testing
 
Software testing
Software testingSoftware testing
Software testing
 
Automation Testing theory notes.pptx
Automation Testing theory notes.pptxAutomation Testing theory notes.pptx
Automation Testing theory notes.pptx
 
Introduction of Software Engineering
Introduction of Software EngineeringIntroduction of Software Engineering
Introduction of Software Engineering
 
Engineering Software Products: 9. testing
Engineering Software Products: 9. testingEngineering Software Products: 9. testing
Engineering Software Products: 9. testing
 
SE - Lecture 8 - Software Testing State Diagram.pptx
SE - Lecture 8 - Software Testing  State Diagram.pptxSE - Lecture 8 - Software Testing  State Diagram.pptx
SE - Lecture 8 - Software Testing State Diagram.pptx
 
Indy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-muleIndy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-mule
 
How to Test PowerShell Code Using Pester
How to Test PowerShell Code Using PesterHow to Test PowerShell Code Using Pester
How to Test PowerShell Code Using Pester
 
Test What Matters Most
Test What Matters MostTest What Matters Most
Test What Matters Most
 
Dev buchan 30 proven tips
Dev buchan 30 proven tipsDev buchan 30 proven tips
Dev buchan 30 proven tips
 
DSR Testing (Part 1)
DSR Testing (Part 1)DSR Testing (Part 1)
DSR Testing (Part 1)
 
lab1.ppt
lab1.pptlab1.ppt
lab1.ppt
 
Six simple steps to unit testing happiness
Six simple steps to unit testing happinessSix simple steps to unit testing happiness
Six simple steps to unit testing happiness
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Transferring Software Testing Tools to Practice
Transferring Software Testing Tools to PracticeTransferring Software Testing Tools to Practice
Transferring Software Testing Tools to Practice
 
The Professional Programmer
The Professional ProgrammerThe Professional Programmer
The Professional Programmer
 

Más de hccit

Snr ipt 10_syll
Snr ipt 10_syllSnr ipt 10_syll
Snr ipt 10_syllhccit
 
Snr ipt 10_guide
Snr ipt 10_guideSnr ipt 10_guide
Snr ipt 10_guidehccit
 
3 d modelling_task_sheet_2014_yr12
3 d modelling_task_sheet_2014_yr123 d modelling_task_sheet_2014_yr12
3 d modelling_task_sheet_2014_yr12hccit
 
3 d modelling_task_sheet_2014_yr11
3 d modelling_task_sheet_2014_yr113 d modelling_task_sheet_2014_yr11
3 d modelling_task_sheet_2014_yr11hccit
 
10 ict photoshop_proj_2014
10 ict photoshop_proj_201410 ict photoshop_proj_2014
10 ict photoshop_proj_2014hccit
 
Photoshop
PhotoshopPhotoshop
Photoshophccit
 
Flash
FlashFlash
Flashhccit
 
University partnerships programs email
University partnerships programs emailUniversity partnerships programs email
University partnerships programs emailhccit
 
Griffith sciences pathway programs overview
Griffith sciences pathway programs overviewGriffith sciences pathway programs overview
Griffith sciences pathway programs overviewhccit
 
Griffith info tech brochure
Griffith info tech brochureGriffith info tech brochure
Griffith info tech brochurehccit
 
Pm sql exercises
Pm sql exercisesPm sql exercises
Pm sql exerciseshccit
 
Repairs questions
Repairs questionsRepairs questions
Repairs questionshccit
 
Movies questions
Movies questionsMovies questions
Movies questionshccit
 
Australian birds questions
Australian birds questionsAustralian birds questions
Australian birds questionshccit
 
Section b
Section bSection b
Section bhccit
 
Section a
Section aSection a
Section ahccit
 
Ask manual rev5
Ask manual rev5Ask manual rev5
Ask manual rev5hccit
 
Case study report mj
Case study report mjCase study report mj
Case study report mjhccit
 

Más de hccit (20)

Snr ipt 10_syll
Snr ipt 10_syllSnr ipt 10_syll
Snr ipt 10_syll
 
Snr ipt 10_guide
Snr ipt 10_guideSnr ipt 10_guide
Snr ipt 10_guide
 
3 d modelling_task_sheet_2014_yr12
3 d modelling_task_sheet_2014_yr123 d modelling_task_sheet_2014_yr12
3 d modelling_task_sheet_2014_yr12
 
3 d modelling_task_sheet_2014_yr11
3 d modelling_task_sheet_2014_yr113 d modelling_task_sheet_2014_yr11
3 d modelling_task_sheet_2014_yr11
 
10 ict photoshop_proj_2014
10 ict photoshop_proj_201410 ict photoshop_proj_2014
10 ict photoshop_proj_2014
 
Photoshop
PhotoshopPhotoshop
Photoshop
 
Flash
FlashFlash
Flash
 
University partnerships programs email
University partnerships programs emailUniversity partnerships programs email
University partnerships programs email
 
Griffith sciences pathway programs overview
Griffith sciences pathway programs overviewGriffith sciences pathway programs overview
Griffith sciences pathway programs overview
 
Griffith info tech brochure
Griffith info tech brochureGriffith info tech brochure
Griffith info tech brochure
 
Pm sql exercises
Pm sql exercisesPm sql exercises
Pm sql exercises
 
Repairs questions
Repairs questionsRepairs questions
Repairs questions
 
Movies questions
Movies questionsMovies questions
Movies questions
 
Australian birds questions
Australian birds questionsAustralian birds questions
Australian birds questions
 
Section b
Section bSection b
Section b
 
B
BB
B
 
A
AA
A
 
Section a
Section aSection a
Section a
 
Ask manual rev5
Ask manual rev5Ask manual rev5
Ask manual rev5
 
Case study report mj
Case study report mjCase study report mj
Case study report mj
 

Último

TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxfnnc6jmgwh
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkPixlogix Infotech
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024TopCSSGallery
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Nikki Chapple
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 

Último (20)

TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App Framework
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 

Week10style

  • 2. © 2004 Pearson Addison-Wesley. All rights reserved 5-2 Copyright Warning COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been copied and communicated to you by or on behalf of Bond University pursuant to Part VB of the Copyright Act 1968 (the Act). The material in this communication may be subject to copyright under the Act. Any further copying or communication of this material by you may be the subject of copyright protection under the Act.
  • 3. © 2004 Pearson Addison-Wesley. All rights reserved 5-3 Robust Programs • We have seen that our programs can be fragile • Programs also need to be read by humans so they can be fixed and extended • This week we are looking at techniques to make our programs more robust, and more easily understood by other programmers • We will be looking at: – Tracing, debugging and unit testing – Good API design
  • 4. © 2004 Pearson Addison-Wesley. All rights reserved 5-4 Why Do Programs Have Bugs? • Programs have bugs for several reasons: – Computers DWIS not DWIM – Inputs can have any or all possible values – We don't understand the language subtleties – Our understanding of the interfaces between the modules in our program, and with external modules, is wrong • Let's look at some ways of combatting these problems
  • 5. © 2004 Pearson Addison-Wesley. All rights reserved 5-5 Tracing Programs • We don't think like a computer, but to write good programs we need to. • Tracing a program means to step through some code, by hand, and track all the variable values and decisions made, to ensure that the code is doing exactly what it should be doing. • If we don't understand our own code, we can fall into the trap of shotgun debugging: making random changes in the hope that any problems will disappear.
  • 6. © 2004 Pearson Addison-Wesley. All rights reserved 5-6 Tracing Programs • Trace the following code when it is given the input “Paul McCartney” public static String changeString(String s) { String x = ""; for (int i=0; i < s.length(); i += 2) { if (s.charAt(i) < 'M') x = x + "."; x = x + Character.toUpperCase( s.charAt(i) ); } return(x); }
  • 7. © 2004 Pearson Addison-Wesley. All rights reserved 5-7 Tracing Programs • Why tracing? • It forces us to read each line of code, think about what it is doing, why it is there, and what side effects each line has • It also makes us do things slowly • We will focus more deeply on the code • Sometimes, we also reflect on questions such as the quality of the code, could it be done better etc.
  • 8. © 2004 Pearson Addison-Wesley. All rights reserved 5-8 Debugging Programs • Debugging is like tracing but the computer looks after the display of variables and the stepping through the code • Don't start at main(). Try to identify where the bug is, and debug from just before the bug • Learn how to: – Set up breakpoints (stop signs) – Step over lines of code – Visualise the variables' values – Step over, and into, methods
  • 9. © 2004 Pearson Addison-Wesley. All rights reserved 5-9 Debugging Programs • Find the bug(s) in this method: // Return the position of the first ch in the String, // or return -1 if ch is not in the String. public static int findLetter(String str, char ch) { for (int i=0; i <= str.length(); i++) if (str.charAt(i) == ch) return(i); return(-1); }
  • 10. © 2004 Pearson Addison-Wesley. All rights reserved 5-10 Methods and Inputs • Every method has a declaration, which is its contract: if you give me this input, I promise to return this value • Inputs, regardless of where they come from, are problematic • If an input is of type X, any valid X value may come in as input:  int: zero, negative values, large values  floats: 0.0, INFINITY, very small values  Strings: empty String, null  char: non-printable chars, non-English chars
  • 11. © 2004 Pearson Addison-Wesley. All rights reserved 5-11 Unit Testing • All large programs have bugs: usual estimate is 1 bug per 100 lines of code • Most bugs are logic bugs: the programmer thinks it works, but the code does something different • It's nearly impossible to tell if a program is 100% bug-free • Implication: programs should be tested thorougly to give assurance there are no bugs • Unit testing: test a method with a large set of inputs, to ensure that it produces the correct output • The test set is designed to stress the method as much as possible
  • 12. © 2004 Pearson Addison-Wesley. All rights reserved 5-12 Unit Testing • You should design the method interface first • THEN, construct a suitable test set for it  Include good and bad inputs • ONLY THEN, write the actual method code • Once you think you have written the method, you can then test the method with the unit tests • “Coding isn't complete until all the unit tests pass” • ALWAYS generate the tests by hand. Don't use a program to generate the test set. Why not? • Bluej provides a framework to construct unit tests. • You will see it in the labs, and you will be assessed on unit testing
  • 13. © 2004 Pearson Addison-Wesley. All rights reserved 5-13 Unit Test Example • Design the declaration of a method isATriangle() • Given 3 side lengths, return true if they represent a triangle, false otherwise • Create a set of unit tests (inputs, output) that you can use to test the method. • In BlueJ, write the method to always return false  So we can write the unit tests, which will fail • Now go and write the code. • Once all the unit tests pass, you now have confidence that the code works
  • 14. © 2004 Pearson Addison-Wesley. All rights reserved 5-14 API Design • A method provides an interface: name, inputs, output • A Java class provides a set of interfaces, known as an API: application program interface • APIs can be designed well, or extremely bad • Here are some tips on good API design • Names mean everything. Choose wisely • Methods perform actions. Use action names, e.g. getUserName() not UserName() • Make it obvious if a method is a getter or a setter • Boolean methods: use “Is”, e.g. isEnrolled()
  • 15. © 2004 Pearson Addison-Wesley. All rights reserved 5-15 API Design • Method arguments: keep consistent order across the methods in the API, e.g. (hroom, wroom) in Assignment 2. • Make it hard for user to get wrong, easy to get right  e.g if method takes 2 ints and a char, make it method(int, char, int), so hard to get the ints the wrong way around • Encapsulation: private data, public methods, public constants, private helper methods
  • 16. © 2004 Pearson Addison-Wesley. All rights reserved 5-16 API Design • A method provides an interface: name, inputs, output • Clear and concise documentation at the top of each method tells the user what it does, and how it deals with input errors. • Design a clear description of the isATriangle() method