Software Engineering and Architecture

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Advertisements

Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Annoucements  Next labs 9 and 10 are paired for everyone. So don’t miss the lab.  There is a review session for the quiz on Monday, November 4, at 8:00.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
Encapsulation CMSC 202. Types of Programmers Class programmers – Developers of new classes – Goal: Expose the minimum interface necessary to use a new.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Packages. Package A package is a set of related classes Syntax to put a class into a package: package ; public class { …} Two rules:  A package declaration.
1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Computers and Scientific Thinking David Reed, Creighton University Functions and Libraries 1.
Agenda Object Oriented Programming Reading: Chapter 14.
CPS120: Introduction to Computer Science Functions.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Designing Classes. Software changes Software is not like a novel that is written once and then remains unchanged. Software is extended, corrected, maintained,
Henrik Bærbak Christensen1 Test Driven Development “TDD” Summary.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
HotCiv Project Starting up!. Henrik Bærbak Christensen2 HotCiv = Agile development Iterations of –product development –learning increments.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
Catalog of Refactoring (6) Making Method Calls Simpler.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Algorithms and Problem Solving
Classes and Objects: Encapsulation
More Sophisticated Behavior
Chapter 6 CS 3370 – C++ Functions.
Phil Tayco Slide version 1.0 Created Sep 18, 2017
HotCiv Project Starting up!.
Modern JavaScript Develop And Design
Lecture 9-2: Interacting with the Superclass (super);
Chapter 4: Writing Classes
Microsoft Access Illustrated
Functions.
Classes and Objects 2nd Lecture
Lecture 9 Concepts of Programming Languages
Design by Contract Fall 2016 Version.
Recursion 12-Nov-18.
Classes and Objects: Encapsulation
Phil Tayco Slide version 1.0 Created Oct 2, 2017
Classes and Objects Encapsulation
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
Chapter 4: Writing classes
slides created by Ethan Apter
Recursion 2-Dec-18.
Recursion 2-Dec-18.
Fall 2018 CISC124 12/3/2018 CISC124 or talk to your grader with questions about assignment grading. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod.
Recursion 29-Dec-18.
slides created by Ethan Apter
Algorithms and Problem Solving
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Widely used guidance for writing better code.
Recursion 23-Apr-19.
CS Week 2 Jim Williams, PhD.
slides created by Ethan Apter and Marty Stepp
CMPE212 – Reminders Assignment 2 due next Friday.
CMSC 202 Encapsulation Version 9/10.
Software Engineering and Architecture
Software Engineering and Architecture
Software Engineering and Architecture
Lecture 3 More on Flow Control, More on Functions,
Software Engineering and Architecture
Software Engineering and Architecture
Software Engineering and Architecture
Software Engineering and Architecture
Lecture 9 Concepts of Programming Languages
Software Engineering and Architecture
Presentation transcript:

Software Engineering and Architecture Writing Clean Code

Henrik Bærbak Christensen Motivation Code is written to Be compiled, deployed, and executed by users in order to serve their need So we make money, get salaries and can buy presents for our woman (or man, or children, or cats, or whatever…) Be maintained – that is read by humans and modified CS@AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Motivation Functionally correct code can be next to impossible to maintain… Morale: Write Clean Code CS@AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Analyzability Basically: can I understand the code fast? Is my understanding correct? CS@AU Henrik Bærbak Christensen

Henrik Bærbak Christensen How to ? Clean Code? Not an exact science !!! Kent Beck: ”Code smell” WTF measure Our Take at it Uncle Bob ”Clean Code” On Functions and Comments Uncle Henrik My own prejudices and tastes  CS@AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Exercise We will be looking at Game.moveUnit(from, to) For AlphaCiv With a bit of combat Testcases based on AlphaCiv world layout Source: Group N in 2016, final prod. Code + some other uncleaniness I wrote my own test code CS@AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Context moveUnit(Position from, Position to) Choice of data structures in GameImpl [Meta: Git rep ‘frsproject’, sub project ‘clean-code-lecture’ on master branch] CS@AU Henrik Bærbak Christensen

TDD TestList => Test Cases My own work I created a set of test cases for testing the group’s codebase. Generally, their code was functionally correct. Etc… CS@AU Henrik Bærbak Christensen

The Method 5 minutes study [Meta: Version 4527 of code base / my-solutions/dsoftark/tdd-own-solution] Functionally correct AlphaCiv+ (except details in combat) CS@AU Henrik Bærbak Christensen

A Classification Template Clean Code Properties A Classification Template

Classification Scheme An attempt at systematics… Method Name:   Wanted property is OK Example/argument Small Do One Thing One Level of Abstraction Use Descriptive Names Keep Number of Arguments Low Avoid Flag Arguments Have No Side Effects Command Query Separation Prefer Exceptions to Error Codes Don't Repeat Yourself Do the Same Thing, the Same Way Name Boolean Expressions Bail out Fast Arguments in Argument Lists CS@AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Bob’s Properties Small Make it do 1-5 logical steps / ‘functional nuggets’ Do One Thing Do one thing, do it well, do it only! Keep focus! One Level of Abstraction The dean does not edit spelling errors in my slides! Use Descriptive Names Tell the one thing it does! Do not tell anything else Keep Number of Arguments low 0-1-2 maybe three arguments. No more. If more, your method does more than one thing! Eksempel: POS systems’ streg kode scanner kalder: ‘processItem(barcode)’ item = barcodeMap.get(barcode); addToReceipt(item); updateTotal(item.cost()); decrementInventory(item); CS@AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Bob’s Properties Avoid Flag Arguments produceWebPage(true, true, false); Huh??? Boolean arguments says ”do more than one thing!” Right? Have No Side Effects Do not do hidden things / hidden state changes It will fool the client; and will hide weird bugs Ex: init a session; modify the object passed as argument If it does, the descriptive name, should reflect it! CS@AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Bob’s Properties Command Query Separation Setters and Getters Accessors and Mutators Query: no state change!!! Return a value. Command: no return value! Change the state Prefer Exceptions to Error Codes Not ‘int addPayment(int amount)’ that returns error code 17 in case it is an illegal coin Don’t Repeat Yourself Avoid Duplicated Code To avoid multiple maintenance problem CS@AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Don’t Repeat Yourself That is Why? Multiple maintenance problem !!! Root of all Evil Avoid Duplication CS@AU Henrik Bærbak Christensen

Additions by Uncle Henrik Clean Code Additions by Uncle Henrik CS@AU Henrik Bærbak Christensen

Arguments in Argument Lists One symptom on duplicated code is the use of ‘arguments’ in the method/class names addOneToX(int x); addTwoToX(int x); addThreeToX(int x); ??? An argument appears as part of the method name CS@AU Henrik Bærbak Christensen

Do the Same Thing, the Same Way Akin to Uncle Bob’s Do One Thing but not quite… Why? Analyzability WarStory DSE ‘string copy’ Do the same thing, the same way In the C library all functions that operate on a strings (copy, compare, …) have parameters (destination, source) in that order. My collegue had reversed it in his special copy function. CS@AU Henrik Bærbak Christensen

Name Boolean Expressions Boolean expressions are difficult to read! One big ball of mud of && between boolean computations  CS@AU Henrik Bærbak Christensen

Name Boolean Expressions Name each subexpression so we can read what it is! Give each boolean expression a name Yeah, should have been ‘LessThanOrEqualOne’  CS@AU Henrik Bærbak Christensen

And Help from My Friends CS@AU Henrik Bærbak Christensen

Bail out fast When lots of conditions must be checked, I often see deep nesting of if Empirical studies show that humans cannot cope well with nesting levels deeper than 1-2 ! You can write it, but cannot maintain it CS@AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Bail out fast Flatten it, by bailing out as soon as an answer can be computed Bail out fast CS@AU Henrik Bærbak Christensen

Agile Method Development Like any other creative process, you do not do it in one brilliant step! Kent Beck: Clean code that works but not in that order Make it work, then make it clean Commit horrible sins, and then clean up the mess CS@AU Henrik Bærbak Christensen

Analysis

Henrik Bærbak Christensen Method Name: moveUnit()   Wanted property Is OK Small No Do One Thing One Level of Abstraction Use Descriptive Names Keep Number of Arguments Low Yes Avoid Flag Arguments Have No Side Effects Command Query Separation Prefer Exceptions to Error Codes n/a Don't Repeat Yourself Do SameThing, the Same Way Simple Boolean Expressions Bail out Fast Arguments in Argument Lists It is not small. It does several things (validation, actual movement in datastructures, combat resolving). It has many levels of abstraction (direct data manipulation and up). Ats+dfs are not descriptive names. It repeats itself in the actual unit movement code. A lot of boolean expressions are expressed directly and concatenated using AND. It does not bail out fast (it should say ‘if these conditions are not met, return false’). It has the player color argument encoded in the moveRedUnit method name. On the positive side, argument count is low, no flag arguments, no side effects (except the required one), it is a method that is a mutator (command separation), and there is no need for exceptions. CS@AU Henrik Bærbak Christensen

Refactoring Session Online Coding Session

Henrik Bærbak Christensen Let us clean up… CS@AU Henrik Bærbak Christensen

45 Minutes Later Disclaimer: Issue with lost unit if attack fails  CS@AU Henrik Bærbak Christensen

Henrik Bærbak Christensen Next level methods CS@AU Henrik Bærbak Christensen

(Videos) 45 Minutes Later CS@AU Henrik Bærbak Christensen

Next Level Methods Note. I can still spot some clean up potential ! ( ! notXXX is hard to read) CS@AU Henrik Bærbak Christensen

Afterthoughts

Henrik Bærbak Christensen The Two Codebases TDD produce two large codebases The production code The test code The properties of clean code apply to both You want to be able to read test code also! (Analyzability) Except one property: Arguments in Argument Lists In test code, you often ‘hardwire parameters’ / no abstraction Example: ”private void moveRedArcherToPosition45()” Encapsulates the moves of particular unit to particular position in order to do testing! CS@AU Henrik Bærbak Christensen