48
Well-behaved objects Well-behaved objects Improving your coding skills 1.0

Well-behaved objects Improving your coding skills 1.0

  • View
    213

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Well-behaved objects Improving your coding skills 1.0

Well-behaved objectsWell-behaved objects

Improving your coding skills

1.0

Page 2: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 2

Main concepts to be coveredMain concepts to be covered

• Testing

• Debugging

• Test automation

• Writing for maintainability

Page 3: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 3

We have to deal with errorsWe have to deal with errors

• Early errors are usually syntax errors.• The compiler will spot these.

• Later errors are usually logic errors.• The compiler cannot help with these.• Also known as bugs.

• Some logical errors have no immediately obvious manifestation.• Commercial software is rarely error free.

Page 4: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 4

Prevention vs DetectionPrevention vs Detection(Developer vs Maintainer)(Developer vs Maintainer)

• We can lessen the likelihood of errors.• Use software engineering techniques, like

encapsulation.

• We can improve the chances of detection.• Use software engineering practices, like

modularization and documentation.

• We can develop detection skills.

Page 5: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 5

Testing and debuggingTesting and debugging

• These are crucial skills.

• Testing searches for the presence of errors.

• Debugging searches for the source of errors.• The manifestation of an error may well occur

some ‘distance’ from its source.

Page 6: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 6

Testing and debugging Testing and debugging techniquestechniques

• Unit testing (within BlueJ)

• Test automation

• Manual walkthroughs

• Print statements

• Debuggers

Page 7: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 7

Unit testingUnit testing

• Each unit of an application may be tested.• Method, class, module (package in Java).

• Can (should) be done during development.• Finding and fixing early lowers development

costs (e.g. programmer time).• A test suite is built up.

Page 8: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 8

Testing fundamentalsTesting fundamentals

• Understand what the unit should do – its contract.• You will be looking for violations.• Use positive tests and negative tests.

• Test boundaries.• Zero, One, Full.

• Search an empty collection.

• Add to a full collection.

Page 9: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 9

Unit testing within BlueJUnit testing within BlueJ

• Objects of individual classes can be created.• Individual methods can be invoked.• Inspectors provide an up-to-date view of an

object’s state.• Explore through the diary-prototype

project.

Page 10: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 10

Test automationTest automation

• Good testing is a creative process, but ...• ... thorough testing is time consuming and

repetitive.• Regression testing involves re-running tests.• Use of a test rig or test harness can relieve

some of the burden.• Classes are written to perform the testing.• Creativity focused in creating these.

Page 11: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 11

Test automationTest automation

• Explore through the diary-testing project.• Human analysis of the results still required.

• Explore fuller automation through the diary-test-automation project.• Intervention only required if a failure is

reported.

Page 12: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 12

Modularization and interfacesModularization and interfaces

• Applications often consist of different modules.• E.g. so that different teams can work on them.

• The interface between modules must be clearly specified.• Supports independent concurrent development.• Increases the likelihood of successful

integration.

Page 13: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 13

Modularization in a calculatorModularization in a calculator

• Each module does not need to know implementation details of the other.• User controls could be a GUI or a

hardware device.

• Logic could be hardware or software.

Page 14: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 14

Method signatures as an Method signatures as an interfaceinterface

// Return the value to be displayed. public int getDisplayValue(); 

// Call when a digit button is pressed.public void numberPressed(int number); 

// Call when a plus operator is pressed.public void plus(); 

// Call when a minus operator is pressed.public void minus(); 

// Call to complete a calculation.public void equals(); 

// Call to reset the calculator.public void clear();

Page 15: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 15

DebuggingDebugging

• It is important to develop code-reading skills.• Debugging will often be performed on others’

code.

• Techniques and tools exist to support the debugging process.

• Explore through the calculator-engine project.

Page 16: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 16

Manual walkthroughsManual walkthroughs

• Relatively underused.• A low-tech approach.• More powerful than appreciated.

• Get away from the computer!

• ‘Run’ a program by hand.

• High-level (Step) or low-level (Step into) views.

Page 17: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 17

Tabulating object stateTabulating object state

• An object’s behavior is usually determined by its state.

• Incorrect behavior is often the result of incorrect state.

• Tabulate the values of all fields.

• Document state changes after each method call.

Page 18: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 18

Verbal walkthroughsVerbal walkthroughs

• Explain to someone else what the code is doing.• They might spot the error.• The process of explaining might help you to

spot it for yourself.

• Group-based processes exist for conducting formal walkthroughs or inspections.

Page 19: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 19

Print statementsPrint statements

• The most popular technique.

• No special tools required.

• All programming languages support them.

• Only effective if the right methods are documented.

• Output may be voluminous!

• Turning off and on requires forethought.

Page 20: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 20

DebuggersDebuggers

• Debuggers are both language- and environment-specific.• BlueJ has an integrated debugger.

• Support breakpoints.

• Step and Step-into controlled execution.

• Call sequence (stack).

• Object state.

Page 21: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 21

ReviewReview

• Errors are a fact of life in programs.

• Good software engineering techniques can reduce their occurrence.

• Testing and debugging skills are essential.

• Make testing a habit.

• Automate testing where possible.

• Practise a range of debugging skills.

Page 22: Well-behaved objects Improving your coding skills 1.0

Designing classesDesigning classes

How to write classes in a way that they are easily understandable, maintainable

and reusable

Page 23: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 23

Main concepts to be coveredMain concepts to be covered

• Responsibility-driven design

• Coupling

• Cohesion

• Refactoring

• Static methods

Page 24: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 24

Software changesSoftware changes

• Software is not like a novel that is written once and then remains unchanged.

• Software is extended, corrected, maintained, ported, adapted…

• The work is done by different people over time (often decades).

Page 25: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 25

Change or dieChange or die

• There are only two options for software:• Either it is continuously maintained• or it dies.

• Software that cannot be maintained will be thrown away.

Page 26: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 26

World of ZuulWorld of Zuul

Page 27: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 27

Code qualityCode quality

Two important concepts for quality of code:

• Coupling

• Cohesion

Page 28: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 28

CouplingCoupling

• Coupling refers to links between separate units of a program.

• If two classes depend closely on many details of each other, we say they are tightly coupled.

• We aim for loose coupling.

Page 29: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 29

Loose couplingLoose coupling

Loose coupling makes it possible to:

• understand one class without reading others;

• change one class without affecting others.

• Thus: improves maintainability.

Page 30: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 30

CohesionCohesion• Cohesion refers to the the number and

diversity of tasks that a single unit is responsible for.

• If each unit is responsible for one single logical task, we say it has high cohesion.

• Cohesion applies to classes and methods.

• We aim for high cohesion.

Page 31: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 31

High cohesionHigh cohesion

High cohesion makes it easier to:

• understand what a class or method does;

• use descriptive names;

• reuse classes or methods.

Page 32: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 32

Cohesion of methodsCohesion of methods

• A method should be responsible for one and only one well defined task.

Page 33: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 33

Cohesion of classesCohesion of classes

• Classes should represent one single, well defined entity.

Page 34: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 34

Code duplicationCode duplication

Code duplication

• is an indicator of bad design,

• makes maintenance harder,

• can lead to introduction of errors during maintenance.

Page 35: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 35

Responsibility-driven designResponsibility-driven design

• Question: where should we add a new method (which class)?

• Each class should be responsible for manipulating its own data.

• The class that owns the data should be responsible for processing it.

• RDD leads to low coupling.

Page 36: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 36

Localizing changeLocalizing change

• One aim of reducing coupling and responsibility-driven design is to localize change.

• When a change is needed, as few classes as possible should be affected.

Page 37: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 37

Thinking aheadThinking ahead

• When designing a class, we try to think what changes are likely to be made in the future.

• We aim to make those changes easy.

Page 38: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 38

RefactoringRefactoring

• When classes are maintained, often code is added.

• Classes and methods tend to become longer.

• Every now and then, classes and methods should be refactored to maintain cohesion and low coupling.

Page 39: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 39

Refactoring and testingRefactoring and testing

• When refactoring code, separate the refactoring from making other changes.

• First do the refactoring only, without changing the functionality.

• Test before and after refactoring to ensure that nothing was broken.

Page 40: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 40

Design questionsDesign questions

Common questions:

• How long should a class be?

• How long should a method be?

• Can now be answered in terms of cohesion and coupling.

Page 41: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 41

Design guidelinesDesign guidelines

• A method is too long if it does more then one logical task.

• A class is too complex if it represents more than one logical entity.

• Note: these are guidelines - they still leave much open to the designer.

Page 42: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 42

ReviewReview

• Programs are continuously changed.

• It is important to make this change possible.

• Quality of code requires much more than just performing correct at one time.

• Code must be understandable and maintainable.

Page 43: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 43

ReviewReview

• Good quality code avoids duplication, displays high cohesion, low coupling.

• Coding style (commenting, naming, layout, etc.) is also important.

• There is a big difference in the amount of work required to change poorly structured and well structured code.

Page 44: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 44

Static MethodsStatic Methods

• Previous we discussed class and object level.

• We introduced class variables

• static keyword

• A similar thing can be done for methods providing services for the class instead an object

Page 45: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 45

Static Methods (2)Static Methods (2)

public class StaticTest {static count = 0;

public StaticTest {count++;

}

public static int giveCount() {return count;

}

…}

Page 46: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 46

Executing without BlueJExecuting without BlueJ

• If we want to start a Java application without BlueJ, we need to use a class method.

• Java uses a class method with a specific signature:• public static void main(String args[])• args contains the command line options for

your application

Page 47: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 47

Executing without BlueJ (2)Executing without BlueJ (2)

• Compiling• javac className.java

• Running• java className • java className command line options• but className should contain a main method

• BlueJ write its own main whenever you ask it to execute something.

Page 48: Well-behaved objects Improving your coding skills 1.0

18/11/2004 Lecture 6: Wel-behaved Objects 48

ConceptsConcepts

• testing• positive and negative

testing• debugging• unit testing• regression testing• walkthroughs

• responsibility-driven design

• coupling• cohesion• refactoring• static • class methods• main method