Advanced Programming in Java

38
Advanced Programming in Java Sadegh Aliakbary Sharif University of Technology Fall 2011

description

Advanced Programming in Java. Sadegh Aliakbary Sharif University of Technology Fall 2011. Agenda. Software Quality Characteristic of a good software Test Unit Testing Refactoring. Quality of Product. The producer should ensure about the quality of the products Quality Control - PowerPoint PPT Presentation

Transcript of Advanced Programming in Java

Advanced Programming in Java

Advanced Programming in JavaSadegh AliakbarySharif University of TechnologyFall 2011AgendaSoftware Quality Characteristic of a good softwareTestUnit TestingRefactoring

Fall 2011Sharif University of Technology2Quality of ProductThe producer should ensure about the quality of the productsQuality ControlAny business, any product

Fall 2011Sharif University of Technology3A CookFall 2011Sharif University of Technology4

Maintenance check of electronic equipment on a Navy aircraft.

Fall 2011Sharif University of Technology5

A Car MakerFall 2011Sharif University of Technology6

Quality ControlQuality should be tested

A product is not finalized, before the test

Different kinds of test, check different kinds of qualityFall 2011Sharif University of Technology7

Software QualityWe are programmersProgrammers produce softwareWhat are characteristics of a good software?Many parameters. E.g.Conformance to requirementsPerformanceTime MemoryMaintainabilityChangeabilityDifferent kinds of test, check different kinds of quality

Fall 2011Sharif University of Technology8Test TargetSystem TestTest the system as a wholeFor performance, correctness and conformance.Unit TestTest the units and modulesTest of a componentTest of a classTest of a methodFall 2011Sharif University of Technology9How to Test SoftwareManual TestTry it!Test ToolsPerformance TestProfilingJProfiler, TPTPLoad Test JmeterTest CodeUnit TestsTest TeamsFall 2011Sharif University of Technology10Test CodeBusiness CodeThe code, written for implementation of a requirement

Test CodeThe code, written for test of an implementationFall 2011Sharif University of Technology11Unit TestingA process for the programmerNot a test team procedureFor improving the code qualityReduces bugsTest of units of software before the software is completedUnit: method, class

Fall 2011Sharif University of Technology12

Classical Unit TestingWriting main() methodSome printlnsDrawbacks?Fall 2011Sharif University of Technology13DrawbacksTest code coupled with business codeIn the same classWritten tests are discardedOne test at a timeThe programmer executes the tests himselfTest execution is not automaticThe programmer should check the result of each test himselfThe test is passed or failed?The test result interpretation is not automaticFall 2011Sharif University of Technology14A Good Unit Test CodeRepeatableAutomaticInvocationAcceptance (Pass/Failure)

JUnit helps you write such testsFall 2011Sharif University of Technology15JUnit, First ExampleFall 2011Sharif University of Technology16

JUnit, The Green BarFall 2011Sharif University of Technology17

public class Testing {@Testpublic void testNormal() {int[] array = {3,2,1,4};int[] sorted = {1,2,3,4};Business.sort(array);for (int i = 0; i < sorted.length; i++) {Assert.assertEquals(sorted[i], array[i]);}}@Testpublic void testEmptyArray() {int[] array = {};try{Business.sort(array);}catch(Exception e){Assert.fail();}Assert.assertNotNull(array);Assert.assertEquals(array.length, 0);}}

Fall 2011Sharif University of Technology18AssertionsassertNull(x)assertNotNull(x)assertTrue(boolean x)assertFalse(boolean x)assertEquals(x, y)Uses x.equals(y)assertSame(x, y) Uses x ==yassertNotSamefail()

Fall 2011Sharif University of Technology19Annotations@Test@Before@After@BeforeClass@AfterClassFall 2011Sharif University of Technology20Fall 2011Sharif University of Technology21

A Good Unit Test isAutomatedThroughRepeatableIndependenceProfessional

Fall 2011Sharif University of Technology22Test Driven DevelopmentTest First DevelopmentBefore writing a code, write the tests!

Fall 2011Sharif University of Technology23TDDFall 2011Sharif University of Technology24

RefactoringRefactoringA disciplined way to restructure codeIn order to improve code qualityWithout changing its behavior

a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior.Fall 2011Sharif University of Technology26Martin FowlerRefactoringRefactoring is the process of changing a software system In such a way that it does not alter the external behavior of the code But improves its internal structureIt is a disciplined way to clean up code It minimizes the chances of introducing bugsWhen you refactor, you are improving the design of the code after it has been written.

Fall 2011Sharif University of Technology27Martin Fowler

RefactoringBy continuously improving the design of code, we make it easier and easier to work withFall 2011Sharif University of Technology28Joshua Kerievsky, Refactoring to PatternsThe Two HatsKent Beck's metaphor of two hatsDivide your time between two distinct activitiesadding functionrefactoringFall 2011Sharif University of Technology29Why Should I Refactor?Refactoring Improves the Design of SoftwareRefactoring Makes Software Easier to UnderstandRefactoring Helps You Find BugsRefactoring Helps You Program Faster

Refactoring makes your code more maintainableFall 2011Sharif University of Technology30When Should You Refactor?The Rule of Three:Refactor When You Add FunctionRefactor When You Need to Fix a BugRefactor As You Do a Code ReviewFall 2011Sharif University of Technology31Bad SmellA bad smell in codeAny symptom in the source code that possibly indicates a deeper problem.The term is coined by Kent Beck.Fall 2011Sharif University of Technology32

Bad SmellsIf it stinks, change it!Kent Beck and Martin Fowler.Bad smells in codeBad smells are source of problemsRemove bad smellsHow?By RefactoringFall 2011Sharif University of Technology33Bad SmellsDuplicated CodeLong MethodLarge ClassLong Parameter ListFall 2011Sharif University of Technology34Refactoring TechniquesExtract MethodMove MethodVariableClassExtract ClassRenameMethodVariableClassPull UpPush DownFall 2011Sharif University of Technology35IDE SupportRefactoring techniques are widely supported by IDEs

Practice it in EclipseFall 2011Sharif University of Technology36

ReferenceRefactoring: improving the design of existing code, Martin Fowler, Kent Beck,John Brant, William Opdyke, Don Roberts(1999)Fall 2011Sharif University of Technology37Fall 2011Sharif University of Technology38