Advanced Programming in Java

Slides:



Advertisements
Similar presentations
A practical guide John E. Boal TestDrivenDeveloper.com.
Advertisements

Test-Driven Development and Refactoring CPSC 315 – Programming Studio.
A Brief Introduction to Test- Driven Development Shawn M. Jones.
Refactoring By: Brian Smith. What is Refactoring? Definition: a change to the internal structure of software to make it easier to understand and cheaper.
Test-Driven Development and Refactoring Project 3 Lecture 1 CPSC 315 – Programming Studio Fall 2009.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
XP and Refactoring David Talby. Development Methodologies The Software Crisis – 84% of software projects are not on time – 31% of software projects never.
REFACTORING Improving the Design of Existing Code Atakan Şimşek e
TDD Test-Driven Development. JUnit 4.0 To use annotations need to import org.junit.Test To use assertion need to import org.junit.Assert.* No need to.
13-Jul-15 Refactoring II. Books Design Patterns is the classic book by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides Basically a catalog.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Refactoring Lecture 5 CIS 6101 Software Processes and Metrics.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Unit Testing.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
1 v1.6 08/02/2006 Overview of Eclipse Lectures 1.Overview 2.Installing and Running 3.Building and Running Java Classes 4.Refactoring 5.Debugging 6.Testing.
Advanced Programing practices
Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some.
1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals.
Software Refactoring Part I: Introduction Bartosz Walter Advanced Object-Oriented Design Lecture 5.
Sadegh Aliakbary Sharif University of Technology Spring 2012.
CS305j Introduction to Computing Inheritance and Polymorphism 1 Topic 26 Introduction to Inheritance and Polymorphism "One purpose of CRC cards [a design.
Refactoring1 Refactoring DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY February 6, 2009.
Refactoring An Automated Tool for the Tiger Language Leslie A Hensley
Refactoring1 Improving the structure of existing code.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
JUnit Adam Heath. What is JUnit?  JUnit is a unit testing framework for the Java programming language  It allows developers to swiftly and easily test.
1 CSC/ECE 517 Fall 2010 Lec. 3 Overview of Eclipse Lectures Lecture 2 “Lecture 0” Lecture 3 1.Overview 2.Installing and Running 3.Building and Running.
Chapter 21 Test-Driven Development 1CS6359 Fall 2011 John Cole.
Refactoring 101 William C. Wake
Refactoring 2. Admin Blackboard Quiz Acknowledgements Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of.
By Rick Mercer with help from Kent Beck and Scott Ambler Java Review via Test Driven Development (TDD)
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Salman Marvasti Sharif University of Technology Winter 2015.
Refactoring - 1 CS494: Intro. to Refactoring Readings: –Refactoring for everyone: How and why to use Eclipse's automated refactoring features. By David.
Scalatest. 2 Test-Driven Development (TDD) TDD is a technique in which you write the tests before you write the code you want to test This seems backward,
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
Refactoring Mehdi Einali Advanced Programming in Java 1.
2-1 By Rick Mercer with help from Kent Beck and Scott Ambler Java Review via Test Driven Development.
Refactoring1 Improving the structure of existing code.
Refactoring Agile Development Project. Lecture roadmap Refactoring Some issues to address when coding.
Pertemuan 12 Refactoring Mata kuliah: T0144 – Advanced Topics in Software Engineering Tahun: 2010.
Refactoring. 2 Process of changing a software system in such a way that it does not alter the external behavior of the code, yet improves its internal.
Test Driven Development Introduction Issued date: 8/29/2007 Author: Nguyen Phuc Hai.
Refactoring. DCS – SWC 2 Refactoring ”A change made to the internal structure of software to make it easier to understand and cheaper to modify without.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Principles and examples
Module Road Map Refactoring Why Refactoring? Examples
Advanced Programming in Java
Advanced Programming in Java
Chapter 4: Writing Classes
Refactoring II 21-Sep-18.
Advanced Programming Behnam Hatami Fall 2017.
بازآرایی برنامه Code Refactoring
Overview of Eclipse Lectures
Implementing Non-Static Features
Improving the structure of existing code
Advanced Programming Behnam Hatami Fall 2017.
Advanced Programming Behnam Hatami Fall 2017.
Refactoring II 5-Feb-19.
Refactoring.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Advanced Programing practices
Refactoring.
Presentation transcript:

Advanced Programming in Java Salman Marvasti Sharif University of Technology Fall 2015

Review Java Programming Language Principles of Object Oriented Programming Characteristics of objects Encapsulation Objects in memory References Heap Stack Parameter Passing Sharif University of Technology

Review (2) Initialization and Cleanup Access specifiers Constructor finalize() Order of initialization Initialization blocks Access specifiers Public Private Package access Sharif University of Technology

Review (3) Package Static The this reference Method overloading toString() equals() Polymorphism Overriding , Upcasting Interface Sharif University of Technology

Agenda Software Quality Characteristic of a good software Test Unit Testing Refactoring Sharif University of Technology

Quality of Product The producer should ensure about the quality of the products Quality Control Any business, any product Sharif University of Technology

A Cook Sharif University of Technology

In surgery Sharif University of Technology

A Car Maker Sharif University of Technology

Quality Control Quality should be tested A product is not finalized, before the test Different kinds of test, check different kinds of quality Sharif University of Technology

Software Quality We are programmers Programmers produce software What are characteristics of a good software? Many parameters. E.g. Conformance to requirements Performance Time Memory Maintainability Changeability Different kinds of test, check different kinds of quality Sharif University of Technology

Test in Other Industries Test side effects A damage to the product Test of a building Test of a car Test of a part of a product Sharif University of Technology

Test Side Effects Sharif University of Technology

What to do with Test Side Effects? Testing a sample of the product Simulation Mathematical analysis In software testing Along with all of these techniques And we can also test the software itself! (Usually) no damage to the software Sharif University of Technology

Test Target System Test Unit Test Test the system as a whole For performance, correctness and conformance. Unit Test Test the units and modules Test of a component Test of a class Test of a method Sharif University of Technology

How to Test Software Manual Test Test Tools Test Code Test Teams Try it! Test Tools Performance Test Profiling JProfiler, TPTP Load Test Jmeter Test Code Unit Tests Test Teams Sharif University of Technology

Test Code Business Code Test Code The code, written for implementation of a requirement Test Code The code, written for test of an implementation Sharif University of Technology

Unit Testing A process for the programmer Test of units of software Not a test team procedure For improving the code quality Reduces bugs Test of units of software before the software is completed Unit: method, class Sharif University of Technology

Classical Unit Testing Writing main() method Some printlns Drawbacks? Sharif University of Technology

Drawbacks Test code coupled with business code In the same class Written tests are discarded One test at a time The programmer executes the tests himself Test execution is not automatic The programmer should check the result of each test himself The test is passed or failed? The test result interpretation is not automatic Sharif University of Technology

A Good Unit Test Code Repeatable Automatic Invocation Acceptance (Pass/Failure) JUnit helps you write such tests Sharif University of Technology

JUnit, First Example Sharif University of Technology

JUnit, The Green Bar Sharif University of Technology

public class Testing { @Test public 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]); } public void testEmptyArray() { int[] array = {}; try{ }catch(Exception e){ Assert.fail(); Assert.assertNotNull(array); Assert.assertEquals(array.length, 0); Sharif University of Technology

Assertions assertNull(x) assertNotNull(x) assertTrue(boolean x) assertFalse(boolean x) assertEquals(x, y) Uses x.equals(y) assertSame(x, y) Uses x ==y assertNotSame fail() Sharif University of Technology

Annotations @Test @Before @After @BeforeClass @AfterClass Sharif University of Technology

Sharif University of Technology

A Good Unit Test is Automated Through Repeatable Independence Professional Sharif University of Technology

Test Driven Development Test First Development Before writing a code, write the tests! Sharif University of Technology

TDD Sharif University of Technology

Refactoring

Refactoring A disciplined way to restructure code In order to improve code quality Without 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. Martin Fowler Sharif University of Technology

Refactoring Refactoring 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 structure It is a disciplined way to clean up code It minimizes the chances of introducing bugs When you refactor, you are improving the design of the code after it has been written. Martin Fowler Sharif University of Technology

Refactoring By continuously improving the design of code, we make it easier and easier to work with Joshua Kerievsky, Refactoring to Patterns Sharif University of Technology

Example Duplicate Code What are the drawbacks? What is the solution? Refactoring: Finding a “Bad Smell” Changing the code to remove the bad smell Some well-known bad smells are reported Sharif University of Technology

Bad Smell A bad smell in code Any symptom in the source code that possibly indicates a deeper problem. The term is coined by Kent Beck. Sharif University of Technology

Bad Smells If it stinks, change it! Bad smells in code Kent Beck and Martin Fowler. Bad smells in code Bad smells are source of problems Remove bad smells How? By Refactoring Sharif University of Technology

Bad Smells Duplicated Code Long Method Large Class Long Parameter List … Sharif University of Technology

Refactoring Techniques Extract Method Move Method Variable Class Extract Class Rename Pull Up Push Down Sharif University of Technology

IDE Support Refactoring techniques are widely supported by IDEs Practice it in Eclipse Sharif University of Technology

The Two Hats Kent Beck's metaphor of two hats Divide your time between two distinct activities adding function refactoring Sharif University of Technology

Why Should I Refactor? Refactoring Improves the Design of Software Refactoring Makes Software Easier to Understand Refactoring Helps You Find Bugs Refactoring Helps You Program Faster Refactoring makes your code more maintainable Sharif University of Technology

When Should You Refactor? The Rule of Three: Refactor When You Add Function Refactor When You Need to Fix a Bug Refactor As You Do a Code Review Sharif University of Technology

Find bad smells! Refactor the Code! Scanner s = new Scanner(System.in); System.out.println("Rectangle Info."); System.out.print("Enter the width: "); int a1 = s.nextInt(); System.out.print("Enter the length: "); int a2 = s.nextInt(); int b1 = s.nextInt(); int b2 = s.nextInt(); int x = a1*a2; int y = b1*b2; if(x == y) System.out.println("Equal"); Find bad smells! Refactor the Code! Sharif University of Technology

Scanner scanner = new Scanner(System. in); System. out Scanner scanner = new Scanner(System.in); System.out.println("Rectangle Info."); System.out.print("Enter the width: "); int width1 = scanner.nextInt(); System.out.print("Enter the length: "); int length1 = scanner.nextInt(); int width2 = scanner.nextInt(); int length2 = scanner.nextInt(); int area1 = width1*length1; int area2 = width2*length2; if(area1 == area2) System.out.println("Equal"); Rename… Sharif University of Technology

class Rectangle{ private int length , width; public int getLength() { return length; } public void setLength(int length) { this.length = length; public int getWidth() { return width; public void setWidth(int width) { this.width = width; public Rectangle(int length, int width) { Extract Class… Sharif University of Technology

Scanner scanner = new Scanner(System. in); System. out Scanner scanner = new Scanner(System.in); System.out.println("Rectangle Info."); System.out.print("Enter the width: "); int width = scanner.nextInt(); System.out.print("Enter the length: "); int length = scanner.nextInt(); Rectangle rectangle1 = new Rectangle(length, width); width = scanner.nextInt(); length = scanner.nextInt(); Rectangle rectangle2 = new Rectangle(length, width); int area1 = rectangle1.getWidth()*rectangle1.getLength(); int area2 = rectangle2.getWidth()*rectangle2.getLength(); if(area1 == area2) System.out.println("Equal"); Sharif University of Technology

int area1 = rectangle1.area(); int area2 = rectangle2.area(); class Rectangle{ ... public int area(){ return length * width; } … int area1 = rectangle1.area(); int area2 = rectangle2.area(); Extract Method… Sharif University of Technology

private static Rectangle readRectangle(Scanner scanner) { int width; int length; System.out.println("Rectangle Info."); System.out.print("Enter the width: "); width = scanner.nextInt(); System.out.print("Enter the length: "); length = scanner.nextInt(); Rectangle rectangle2 = new Rectangle(length, width); return rectangle2; } Extract Method… Sharif University of Technology

Refactored Code Scanner scanner = new Scanner(System.in); Rectangle rectangle1 = Rectangle.readRectangle(scanner); Rectangle rectangle2 = Rectangle.readRectangle(scanner); int area1 = rectangle1.area(); int area2 = rectangle2.area(); if(area1 == area2) System.out.println("Equal"); Sharif University of Technology

Reference Refactoring: improving the design of existing code, Martin Fowler, Kent Beck, John Brant, William Opdyke, Don Roberts (1999) Sharif University of Technology

Additional Info for Bulk Test and Bulk Data Operations – ONLY JAVA 1.8 List<String> names = Arrays.asList("Smith", "Adams", "Crawford"); List<Person> people = peopleDAO.find("London");   // Using anyMatch and method reference List<Person> anyMatch = people.stream().filter(p -> (names.stream().anyMatch(p.name::contains))). collect(Collectors.toList());   // Using reduce List<Person> reduced = people.stream(). filter(p -> names.stream().reduce(false, (Boolean b, String keyword) -> b ||  p.name.contains(keyword), (l, r) -> l | r)).collect(Collectors.toList()); Sharif University of Technology