Erik Jõgi. twitter.com/erikjogi twitter.com/codeborne

Similar documents
Software Engineering I (02161)

Software Engineering I (02161)

Core Java Contents. Duration: 25 Hours (1 Month)

Kerievsky_book.fm Page 355 Thursday, July 8, :12 PM. Index

Inheritance. EEC 521: Software Engineering. Dealing with Change. Polymorphism. Software Design. Changing requirements Code needs to be flexible

Java Collections Framework reloaded

Complete Java Contents

CHAPTER 6: CREATIONAL DESIGN PATTERNS

Introduction to Testing and Maintainable code

Advanced Object Oriented PHP

XmlHttpRequest asemel võib olla vajalik objekt XDomainRequest

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

Unit Testing and JUnit

Refactoring. Why? When? What?

Puudub protseduur. Protseduuri nimi võib olla valesti kirjutatud. Protseduuri (või funktsiooni) poole pöördumisel on vähem argumente kui vaja.

Pieter van den Hombergh Richard van den Ham. February 8, 2018

Software Engineering I (02161)

Software Engineering I (02161)

5/23/2015. Core Java Syllabus. VikRam ShaRma

Software Engineering I (02161)

Design Patterns. CSC207 Fall 2017

The following topics will be covered in this course (not necessarily in this order).

Application Development in JAVA. Data Types, Variable, Comments & Operators. Part I: Core Java (J2SE) Getting Started

Software Engineering I (02161)

Chapter 4 Java I/O. X i a n g Z h a n g j a v a c o s q q. c o m

Core Java Syllabus. Overview

Design Patterns. Gunnar Gotshalks A4-1

Design Patterns. CSC207 Winter 2017

As a programmer, you know how easy it can be to get lost in the details

Design Patterns For Object Oriented Software Development Acm Press

Test-Driven Development (TDD)

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Design Patterns. CSC207 Fall 2017

The GoF Design Patterns Reference

JAVA. 1. Introduction to JAVA

DESIGN PATTERN - INTERVIEW QUESTIONS

Software Design and Analysis CSCI 2040

Design Patterns (DP) In the beginning. It s not a course about DP (just a little) A lot of good design and efficient implementation is based on DP

Collections. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

Outline. Logistics. Logistics. Principles of Software (CSCI 2600) Spring Logistics csci2600/

Junit. Presentation & Tools (Eclipse, Maven, Mockito, Spring)

Tutorial #11 SFWR ENG / COMP SCI 2S03. Interfaces and Java Collections. Week of November 17, 2014

Recitation 13. Software Engineering Practices and Introduction to Design Patterns

JAVA. Duration: 2 Months

Design Patterns. An introduction

MSDE Upgrade platvormile SQL 2005 Server Express SP4

Java Collections Framework

Java Data Structures Collections Framework BY ASIF AHMED CSI-211 (OBJECT ORIENTED PROGRAMMING)

SQL Server 2005 Expressi paigaldamine

Cocoa Design Patterns. Erik M. Buck October 17, 2009

Software Engineering I (02161)

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. June 8, 2017

Analysis of the Test Driven Development by Example

40) Class can be inherited and instantiated with the package 41) Can be accessible anywhere in the package and only up to sub classes outside the

A Rapid Overview of UML

Lecture 13: Design Patterns

Software Engineering I (02161)

SOLID DESIGN PATTERNS FOR MERE MORTALS

CONTAİNERS COLLECTİONS

Pattern Resources. Lecture 25: Design Patterns. What are Patterns? Design Patterns. Pattern Languages of Programming. The Portland Pattern Repository

Choice, Choices, Choices: Fat versus Thin Clients

Implementation. (Mapping to Java) Jörg Kienzle & Alfred Strohmeier. COMP-533 Implementation

CSC7203 : Advanced Object Oriented Development. J Paul Gibson, D311. Design Patterns

Java Collections. Readings and References. Collections Framework. Java 2 Collections. CSE 403, Spring 2004 Software Engineering

Collections Questions

Outline. Design Patterns. Observer Pattern. Definitions & Classifications

Enterprise Java Development using JPA, Hibernate and Spring. Srini Penchikala Detroit JUG Developer Day Conference November 14, 2009

WD My Net N600 juhend:

Software Engineering - I An Introduction to Software Construction Techniques for Industrial Strength Software

Clean Code * * Or why is more important how we write code rather what we write. Assoc. prof. Catalin Boja, Asist. Bogdan Iancu, Lect.

Using Design Patterns in Java Application Development

More on Design. CSCI 5828: Foundations of Software Engineering Lecture 23 Kenneth M. Anderson

Applying the Observer Design Pattern

Copyright 2012, Oracle and/or its affiliates. All rights reserved.

Object Fundamentals Part Three. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 4 09/03/2009

COMP 354 TDD and Refactoring

CS251 Software Engineering Lectures 18: Intro to DP

Object Oriented Methods with UML. Introduction to Design Patterns- Lecture 8

A Quick Tour p. 1 Getting Started p. 1 Variables p. 3 Comments in Code p. 6 Named Constants p. 6 Unicode Characters p. 8 Flow of Control p.

Application Architectures, Design Patterns

TP-Link TL-WR743ND Juhend

Software Reengineering Refactoring To Patterns. Martin Pinzger Delft University of Technology

Facade and Adapter. Comp-303 : Programming Techniques Lecture 19. Alexandre Denault Computer Science McGill University Winter 2004

Goals of Lecture. Lecture 27: OO Design Patterns. Pattern Resources. Design Patterns. Cover OO Design Patterns. Pattern Languages of Programming

Wentworth Institute of Technology COMP1050 Computer Science II Spring 2017 Derbinsky. Collections & Maps. Lecture 12. Collections & Maps

Levels of Testing Testing Methods Test Driven Development JUnit. Testing. ENGI 5895: Software Design. Andrew Vardy

CLEAN CODE Why You Should Care

References: Jacquie Barker,Beginning Java Objects; Martin Fowler,UML Distilled, 9/25/ UML

Automating Regression Testing of Java Programs the JSnoopy Way

Model-View-Controller

A simple map: Hashtable

Tuesday, October 4. Announcements

Design Patterns: Part 2

Programmieren II. Polymorphism. Alexander Fraser. June 4, (Based on material from T. Bögel)

Levels of Testing Testing Methods Test Driven Development JUnit. Testing. ENGI 5895: Software Design. Andrew Vardy

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

Java Technologies. Lecture III. Valdas Rapševičius. Vilnius University Faculty of Mathematics and Informatics

1 Software Architecture

10 Java Collections; Equality, JUnit

Learn Java/J2EE Basic to Advance level by Swadeep Mohanty

Transcription:

Disain

Erik Jõgi erik@codeborne.com twitter.com/erikjogi twitter.com/codeborne

Disain? Miks?

Bad code

Clean Code A Handbook of Agile Software Craftsmanship Robert C. Martin, 2008 Uncle Bob

You know you are working on clean code when each routine you read turns out to be pretty much what you expected Ward Cunningham inventor of Wiki and Fit, co-inventor of extreme Programming

Inexpensive adaptability Robert Uncle Bob Martin

Four Elements of Simple Design 1. Passes all tests 2. No duplication 3. Expresses intent 4. Small Kent Beck Creator of Extreme Programming, author of JUnit

TDD Test Driven Design

Clean Code

Nimed muutujad, meetodid, klassid,...

Mida see meetod teeb? + Aga see?

Nimi peab aitama mõista sisu ja konteksti Stuff, Data, X, list, number, hp, a1, b2, theset, alist, somethings vs. AccountAlias, FixedPaymentCard, flaggedcustomers, maxtransactioncount, queryintervalseconds, agreementsbytype, findcustomerbyregistrationcode

Correct English Only, Please! Sõna-sõnalt tõlge ei pruugi anda õiget ingliskeelset vastet Kasutage õiged antud valdkonna termineid, uurige valdkonna ekspertidelt, mis on õiged terminid Grammatika ja õigekiri! Sõnastikud, õigekirjakontrollijad, Google Proovige, kas koodi saab lugeda nagu lauseid tekstis?

Üks termin sama mõiste jaoks Customer Client Party Counterparty state status Contract Agreement get load fetch find retrieve regcode regnumber personalcode ja kasutage teise mõiste jaoks erinevat terminit

Funktsioonid

Hea funktsioon lühike vähe trepiastmeid /indentation/ tegeleb ainult ühe asjaga (üks abstraktsiooni tase) Functions should do one thing. They should do it well. They should do it only. Robert Uncle Bob Martin

Funktsiooni argumendid Less is more mida vähem, seda parem Ideaalne funktsioon ei võta ühtegi argumenti Ühe ja kahe argumendiga on ka OK Kolm on üldjuhul juba liiga palju Üle kolme ei tohiks mingil juhul olla

Kuidas argumente vähendada? Selle asemel, et järjest edasi anda ühte argumenti, tehke klassi tasemel väli Kui funktsioon vajab rohkem kui 2 argumenti, siis võib-olla peaks need omaette klassi tõstma? Vältige üksikuid boolean argumente (lippe) savepayment(boolean validate) vs. savepayment(), validateandsavepayment()

Vältige kõrvalmõjusid Funktsioon, mis on definitsiooni (nime) järgi read-only, ei tohi muuta olekut getamount() isconfirmed() checkpassword(string) Kui on ikkagi vaja olekut muuta, siis pange funktsioonile sobivam nimi checkpasswordandloadsettings(string)

Kommentaarid

Kommentaarid on pahad Kommentaarid valetavad: - Neid ei saa koodiga siduda - Neid ei saa automaatselt muuta - Neid ei saa testida - Nad jäävad ajast maha Tõde on ainult koodis! Ära kasuta kommentaari, kui sa saaksid kasutada funkstiooni või muutujat

Millal kommentaare kasutada? The proper use of comments is to compensate for our failure to express ourselves in code - Legal comments - Warning of consequences - TODO comments - Amplification - javadocs in public APIs

The Boy Scout Rule Leave the campground cleaner than you found it Can you imagine working on a project where code simply got better as time passed? Do you believe that any other option is professional?

Objektorienteeritud disain Agile Software Development: Principles, Patterns, and Practices Robert C. Martin, 2003

Interfaces and Abstract Classes Ignore implementation details Program to interfaces

Java Collections API Iterable Collection List: ArrayList, LinkedList Set: HashSet, LinkedHashSet SortedSet: TreeSet Map: HashMap, LinkedHashMap SortedMap: TreeMap

java.io byte based I/O InputStream & OutputStream FileInputStream, SocketInputStream, ByteArrayInputStream, GZIPInputStream character based I/O Reader & Writer FileWriter, StringWriter, OutputStreamWriter

Abstract classes Kirjelda tegevuste järjekord, jäta vabaks implementatsioon abstract class Payment { void validate() { verifyaccountstatus(); verifycustomercreditrating(); verifyreferencenumber(); } class PaymentEE extends Payment { @Override void verifyreferencenumber() { //... } @Override void verifycustomercreditrating() { //... } abstract void verifyreferencenumber(); abstract void verifycustomercreditrating(); abstract void verifyaccountstatus(); } @Override void verifyaccountstatus() { //... } }

Dependency Injection Inversion of Control The Hollywood Principle: Don t call us we ll call you Klass ei lähe ise otsima omale vajalike ressursse vaid need antakse talle ette. Selgem disain Lihtsam testida (Mock objects)

Dependency Injection class PaymentService { @Inject AccountService accountservice; void payment(account from, Account to, BigDecimal amount) { BigDecimal balance = accountservice.getbalance(from);... } } class PaymentServiceTest { @Test payment() { PaymentService paymentservice = new PaymentService(); paymentservice.accountservice = mock(accountservice.class); paymentservice.payment(...);... } }

Dependency Injection Google Guice code.google.com/p/google-guice/ Spring Framework www.springsource.org JSR-330: Dependency Injection for Java http://code.google.com/p/atinject/ Mockito code.google.com/p/mockito/ EasyMock www.easymock.org

Composition Over Inheritance Inheritance is for is-a relationships: - Apple is a Fruit - Button is a JComponent When the same interface is most logical Composition is for consists-of, contains, uses, has relationships: - SalesSystemUI uses JFrame - Customer conains an Address Composition means decoupling independence of each other's changes

Immutable Objects An immutable object is an object whose state cannot be modified after it is created - Kõige levinum Java immutable objekti näide? Lihtsustab programmeerimist - Caching - Thread-safety - Bad code - Map keys Võimaldab kompilaatoril/virtuaalmasinal koodi optimeerida

Design Patterns Elements of Reusable ObjectOriented Software [Gang of Four Book GOF] Erich Gamma Richard Helm Ralph Johnson John Vlissides 1994

Disaini mustrid Tüüplahendused objektorienteeritud disainis olulistele ja tihti esile kerkivatele probleemidele Kogenumate arendajate oskuste ja kogemuste süstematiseeritud kataloog Lihtsustab arendajate suhtlemist me kasutame Strategy mustrit

Refactoring to Patterns Joshua Kerievsky, 2004 Avoid: - over-engineering - under-engineering

A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away. Antoine de Saint-Exupery

Lõppsõna The Boy Scout Rule Kogemus, kogemus, kogemus Lugege palju teiste koodi!