SlideShare a Scribd company logo
1 of 16
Download to read offline
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

More Related Content

What's hot

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
 

What's hot (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
 

Viewers also liked (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 to 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 to 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
 

More from 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
 

More from 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
 

Recently uploaded

Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsPrecisely
 

Recently uploaded (20)

Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power Systems
 

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