44
Advanced Programming in Java Sadegh Aliakbary Sharif University of Technology Spring 2012

Sadegh Aliakbary Sharif University of Technology Spring 2012

Embed Size (px)

Citation preview

Advanced Programming in Java

Advanced Programming in JavaSadegh AliakbarySharif University of TechnologySpring 2012ReviewJava Programming LanguagePrinciples of Object Oriented ProgrammingCharacteristics of objectsEncapsulationObjects in memoryReferencesHeapStackParameter PassingSpring 2012Sharif University of Technology2Review (2)Initialization and CleanupConstructorfinalize()Order of initializationInitialization blocksAccess specifiersPublicPrivatePackage accessSpring 2012Sharif University of Technology3Review (3)PackageStaticThe this referenceMethod overloadingtoString()equals()RefactoringBad smellsRefactoring techniquesSpring 2012Sharif University of Technology4AgendaSoftware Quality Characteristic of a good softwareTestUnit TestingRefactoring

Spring 2012Sharif University of Technology5Quality of ProductThe producer should ensure about the quality of the productsQuality ControlAny business, any product

Spring 2012Sharif University of Technology6A CookSpring 2012Sharif University of Technology7

In surgerySpring 2012Sharif University of Technology8

A Car MakerSpring 2012Sharif University of Technology9

Quality ControlQuality should be tested

A product is not finalized, before the test

Different kinds of test, check different kinds of qualitySpring 2012Sharif University of Technology10

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

Spring 2012Sharif University of Technology11Test in Other IndustriesTest side effectsA damage to the productTest of a buildingTest of a carTest of a part of a product

Spring 2012Sharif University of Technology12Test Side EffectsSpring 2012Sharif University of Technology13

What to do with Test Side Effects?Testing a sample of the productSimulationMathematical analysis

In software testingAlong with all of these techniques And we can also test the software itself!(Usually) no damage to the software Spring 2012Sharif University of Technology14Test 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 methodSpring 2012Sharif University of Technology15How to Test SoftwareManual TestTry it!Test ToolsPerformance TestProfilingJProfiler, TPTPLoad Test JmeterTest CodeUnit TestsTest TeamsSpring 2012Sharif University of Technology16Test CodeBusiness CodeThe code, written for implementation of a requirement

Test CodeThe code, written for test of an implementationSpring 2012Sharif University of Technology17Unit 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

Spring 2012Sharif University of Technology18

Classical Unit TestingWriting main() methodSome printlnsDrawbacks?Spring 2012Sharif University of Technology19DrawbacksTest 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 automaticSpring 2012Sharif University of Technology20A Good Unit Test CodeRepeatableAutomaticInvocationAcceptance (Pass/Failure)

JUnit helps you write such testsSpring 2012Sharif University of Technology21JUnit, First ExampleSpring 2012Sharif University of Technology22

JUnit, The Green BarSpring 2012Sharif University of Technology23

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);}}

Spring 2012Sharif University of Technology24AssertionsassertNull(x)assertNotNull(x)assertTrue(boolean x)assertFalse(boolean x)assertEquals(x, y)Uses x.equals(y)assertSame(x, y) Uses x ==yassertNotSamefail()

Spring 2012Sharif University of Technology25Annotations@Test@Before@After@BeforeClass@AfterClassSpring 2012Sharif University of Technology26Spring 2012Sharif University of Technology27

A Good Unit Test isAutomatedThroughRepeatableIndependenceProfessional

Spring 2012Sharif University of Technology28Test Driven DevelopmentTest First DevelopmentBefore writing a code, write the tests!

Spring 2012Sharif University of Technology29TDDSpring 2012Sharif University of Technology30

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.Spring 2012Sharif University of Technology32Martin 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.

Spring 2012Sharif University of Technology33Martin Fowler

RefactoringBy continuously improving the design of code, we make it easier and easier to work withSpring 2012Sharif University of Technology34Joshua Kerievsky, Refactoring to PatternsThe Two HatsKent Beck's metaphor of two hatsDivide your time between two distinct activitiesadding functionrefactoringSpring 2012Sharif University of Technology35Why 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 maintainableSpring 2012Sharif University of Technology36When Should You Refactor?The Rule of Three:Refactor When You Add FunctionRefactor When You Need to Fix a BugRefactor As You Do a Code ReviewSpring 2012Sharif University of Technology37Bad SmellA bad smell in codeAny symptom in the source code that possibly indicates a deeper problem.The term is coined by Kent Beck.Spring 2012Sharif University of Technology38

Bad SmellsIf it stinks, change it!Kent Beck and Martin Fowler.Bad smells in codeBad smells are source of problemsRemove bad smellsHow?By RefactoringSpring 2012Sharif University of Technology39Bad SmellsDuplicated CodeLong MethodLarge ClassLong Parameter ListSpring 2012Sharif University of Technology40Refactoring TechniquesExtract MethodMove MethodVariableClassExtract ClassRenameMethodVariableClassPull UpPush DownSpring 2012Sharif University of Technology41IDE SupportRefactoring techniques are widely supported by IDEs

Practice it in EclipseSpring 2012Sharif University of Technology42

ReferenceRefactoring: improving the design of existing code, Martin Fowler, Kent Beck,John Brant, William Opdyke, Don Roberts(1999)Spring 2012Sharif University of Technology43Spring 2012Sharif University of Technology44