Organizing Code in Web Apps. SWE 432, Fall 2016 Design and Implementation of Software for the Web

Similar documents
Organizing Code in Web Apps. SWE 432, Fall 2017 Design and Implementation of Software for the Web

Callbacks & Promises

Tools. SWE 432, Fall Design and Implementation of Software for the Web

Frontend Frameworks. SWE 432, Fall 2016 Design and Implementation of Software for the Web

CSE 341 Lecture 27. JavaScript scope and closures. slides created by Marty Stepp

Persistence & State. SWE 432, Fall 2016 Design and Implementation of Software for the Web

FALL 2017 CS 498RK JAVASCRIPT. Fashionable and Functional!

Today. Continue our very basic intro to JavaScript. Lambda calculus

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

JavaScript for C# Programmers Kevin

JavaScript: the language of browser interactions. Claudia Hauff TI1506: Web and Database Technology

Node.js Training JavaScript. Richard richardrodger.com

Scope and Parameter Passing 1 / 19

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

Web Application Development

CSE 70 Final Exam Fall 2009

Recap: Functions as first-class values

Programming II (CS300)

JavaScript CS 4640 Programming Languages for Web Applications

Backend Development. SWE 432, Fall Web Application Development

Next: What s in a name? Programming Languages. Data model in functional PL. What s in a name? CSE 130 : Fall Lecture 13: What s in a Name?

Client Side JavaScript and AJAX

INF3110 Programming Languages Runtime Organization part II

Refactoring with Eclipse

Topic 6: Inner Classes

Exercise 1 Using Boolean variables, incorporating JavaScript code into your HTML webpage and using the document object

Ticket Machine Project(s)

Chapter 6 Introduction to Defining Classes

Programming Languages

JavaScript or: HOW I LEARNED TO STOP WORRYING AND LOVE A CLASSLESS*

JavaScript or: How I Learned to Stop Worrying and Love a Classless Loosely Typed Language

Catbook Workshop: Intro to NodeJS. Monde Duinkharjav

Racket: Modules, Contracts, Languages

Javascript. Many examples from Kyle Simpson: Scope and Closures

CIS 110: Introduction to computer programming

JavaScript CS 4640 Programming Languages for Web Applications

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Racket. CSE341: Programming Languages Lecture 14 Introduction to Racket. Getting started. Racket vs. Scheme. Example.

Mobile Application Programming. Swift Classes

Microservices. SWE 432, Fall 2017 Design and Implementation of Software for the Web

Today's Topics. CISC 458 Winter J.R. Cordy

G Programming Languages - Fall 2012

Mutable Data Types. Prof. Clarkson Fall A New Despair Mutability Strikes Back Return of Imperative Programming

React. HTML code is made up of tags. In the example below, <head> is an opening tag and </head> is the matching closing tag.

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

Semantic Analysis. Lecture 9. February 7, 2018

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

JavaScript. Training Offer for JavaScript Introduction JavaScript. JavaScript Objects

Scope, Functions, and Storage Management

Frontend II: Javascript and DOM Programming. Wednesday, January 7, 15

COMP 2406: Fundamentals of Web Applications. Fall 2013 Mid-Term Exam Solutions

Why Design by Contract! CS 619 Introduction to OO Design and Development. Design by Contract. Fall 2012

Data Visualization (DSC 530/CIS )

clean way to do separate compilation. ECS140A Programming Languages 12-1

Repetition Structures

Modular Programming. Prof. Clarkson Fall Today s music: "Giorgio By Moroder" by Daft Punk

Data Visualization (DSC 530/CIS )

What is it? CMSC 433 Programming Language Technologies and Paradigms Spring Approach 1. Disadvantage of Approach 1

BEFORE CLASS. If you haven t already installed the Firebug extension for Firefox, download it now from

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

CS 231 Data Structures and Algorithms, Fall 2016

iphone Application Programming Lecture 3: Swift Part 2

INF 102 CONCEPTS OF PROG. LANGS FUNCTIONAL COMPOSITION. Instructors: James Jones Copyright Instructors.

JavaScript: Sort of a Big Deal,

INF5750. Introduction to JavaScript and Node.js

Decision Making in C

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Linked lists Tutorial 5b

Persistence. SWE 432, Fall 2017 Design and Implementation of Software for the Web

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

CIS 110: Introduction to Computer Programming

Outline. CIS 110: Introduction to Computer Programming. Any questions? My life story. A horrible incident. The awful truth

Object-Oriented Design

Problem 1: Building and testing your own linked indexed list

Stencil: The Time for Vanilla Web Components has Arrived

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

CSCE 156 Computer Science II

Advanced Placement Computer Science. Inheritance and Polymorphism

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

CMSC 132: Object-Oriented Programming II

CMSC 132: Object-Oriented Programming II

A Java Execution Simulator

Declarations and Access Control SCJP tips

COMP-520 GoLite Tutorial

Programming Lecture 4

SEEM4570 System Design and Implementation. Lecture 3 Events

The role of semantic analysis in a compiler

Software Design and Analysis for Engineers

3. Design by Contract

Chapter 4: Control structures. Repetition

CS-202 Introduction to Object Oriented Programming

CS 307: Software Engineering. Lecture 10: Software Design and Architecture

Boot Camp JavaScript Sioux, March 31, 2011

Fundamental Concepts and Definitions

The course is supplemented by numerous hands-on labs that help attendees reinforce their theoretical knowledge of the learned material.

Classes and Methods גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility

CSCI 161 Introduction to Computer Science

Transcription:

Organizing Code in Web Apps SWE 432, Fall 2016 Design and Implementation of Software for the Web

Today Some basics on how and why to organize code (SWE!) Closures Classes Modules For further reading: http://stackoverflow.com/questions/111102/how-dojavascript-closures-work 2

History + Motivation Wow back in my day before ES6 we didn t have your fancy modules

Spaghetti Code Brian Foote and Joe Yoder 4

window.onload = function () { eqctl = document.getelementbyid('eq'); currnumberctl = document.getelementbyid('currnumber'); ; var eqctl, currnumberctl, operator, operatorset = false, equalspressed = false, lastnumber = null; function add(x,y) { return x + y; function subtract(x, y) { return x - y; function multiply(x, y) { return x * y; function divide(x, y) { if (y == 0) { alert("can't divide by 0"); return 0; return x / y; function setval(val) { currnumberctl.innerhtml = val; function setequation(val) { eqctl.innerhtml = val; function clearnumbers() { lastnumber = null; equalspressed = operatorset = false; setval('0'); setequation(''); function setoperator(newoperator) { if (newoperator == '=') { equalspressed = true; calculate(); setequation(''); return; if (!equalspressed) calculate(); equalspressed = false; operator = newoperator; operatorset = true; lastnumber = parsefloat(currnumberctl.innerhtml); var eqtext = (eqctl.innerhtml == '')? lastnumber + ' ' + operator + ' ' : eqctl.innerhtml + ' ' + operator + ' '; setequation(eqtext); function numberclick(e) { var button = (e.target)? e.target : e.srcelement; if (operatorset == true currnumberctl.innerhtml == '0') { setval(''); operatorset = false; setval(currnumberctl.innerhtml + button.innerhtml); setequation(eqctl.innerhtml + button.innerhtml); function calculate() { if (!operator lastnumber == null) return; var currnumber = parsefloat(currnumberctl.innerhtml), function setoperator(newoperator) { if (newoperator == '=') { equalspressed = true; calculate(); setequation(''); return; if (!equalspressed) calculate(); equalspressed = false; operator = newoperator; operatorset = true; lastnumber = parsefloat(currnumberctl.innerhtml); var eqtext = (eqctl.innerhtml == '')? lastnumber + ' ' + operator + ' ' : eqctl.innerhtml + ' ' + operator + ' '; setequation(eqtext); function numberclick(e) { var button = (e.target)? e.target : e.srcelement; if (operatorset == true currnumberctl.innerhtml == ' setval(''); operatorset = false; setval(currnumberctl.innerhtml + button.innerhtml); setequation(eqctl.innerhtml + button.innerhtml); function calculate() { if (!operator lastnumber == null) return; var currnumber = parsefloat(currnumberctl.innerhtml), newval = 0; switch (operator) { case '+': newval = add(lastnumber, currnumber); break; newval = 0; case '-': switch (operator) { case '+': newval = add(lastnumber, currnumber); newval = subtract(lastnumber, currnumber); break; case '-': break; newval = subtract(lastnumber, currnumber); break; case '*': case '*': newval = multiply(lastnumber, currnumber); break; newval = multiply(lastnumber, currnumber); case '/': newval = divide(lastnumber, currnumber); break; break; case '/': setval(newval); lastnumber = newval; newval = divide(lastnumber, currnumber); break; setval(newval); 5

aka big ball of mud aka shanty town code Brian Foote and Joe Yoder 6

Bad Code Smells Tons of not-very related functions in the same file No/bad comments Hard to understand Lots of nested functions fs.readdir(source, function (err, files) { if (err) { console.log('error finding files: ' + err) else { files.foreach(function (filename, fileindex) { console.log(filename) gm(source + filename).size(function (err, values) { if (err) { console.log('error identifying file size: ' + err) else { console.log(filename + ' : ' + values) aspect = (values.width / values.height) widths.foreach(function (width, widthindex) { height = Math.round(width / aspect) console.log('resizing ' + filename + 'to ' + height + this.resize(width, height).write(dest + 'w' + width + if (err) console.log ).bind(this)) ) ) ); 7

Design Goals Within a component Cohesive Complete Convenient Clear Consistent Between components Low coupling 8

Cohesion and Coupling Cohesion is a property or characteristic of an individual unit Coupling is a property of a collection of units High cohesion GOOD, high coupling BAD Design for change: Reduce interdependency (coupling): You don't want a change in one unit to ripple throughout your system Group functionality (cohesion): Easier to find things, intuitive metaphor aids understanding 9

Design for Reuse Why? Don t duplicate existing functionality Avoid repeated effort How? Make it easy to extract a single component: Low coupling between components Have high cohesion within a component 10

Design for Change Why? Want to be able to add new features Want to be able to easily maintain existing software Adapt to new environments Support new configurations How? Low coupling - prevents unintended side effects High cohesion - easier to find things 11

Organizing Code How do we structure things to achieve good organization? Java Javascript Individual Pieces of Functional Components Classes Classes Entire libraries Packages Modules 12

Closures Closures are expressions that work with variables in a specific context Closures contain a function, and its needed state Closure is that function and a stack frame that is allocated when a function starts executing and not freed after the function returns 13

Closures & Stack Frames What is a stack frame? Variables created by function in its execution Maintained by environment executing code function a() { var x = 5, z = 3; b(x); function b(y) { console.log(y); a(); Contents of memory: a: x: 5 z: 3 Stack frame Function called: stack frame created 14

Closures & Stack Frames What is a stack frame? Variables created by function in its execution Maintained by environment executing code function a() { var x = 5, z = 3; b(x); function b(y) { console.log(y); a(); Contents of memory: b: y: 5 a: x: 5 z: 3 Stack frame Function called: new stack frame created 15

Closures & Stack Frames What is a stack frame? Variables created by function in its execution Maintained by environment executing code function a() { var x = 5, z = 3; b(x); function b(y) { console.log(y); a(); Contents of memory: a: x: 5 z: 3 Stack frame Function returned: stack frame popped 16

Closures Closures are expressions that work with variables in a specific context Closures contain a function, and its needed state Closure is a stack frame that is allocated when a function starts executing and not freed after the function returns That state just refers to that state by name (sees updates) var x = 1; function f() { var y = 2; return function() { console.log(x + y); y++; ; var g = f(); g(); // 1+2 is 3 g(); // 1+3 is 4 This function attaches itself to x and y so that it can continue to access them. It closes up those references 17

Closures var x = 1; function f() { var y = 2; return function() { console.log(x + y); y++; ; var g = f(); g(); // 1+2 is 3 g(); // 1+3 is 4 f() Global var x 1 var y function 2 Closure 18

Closures var x = 1; function f() { var y = 2; return function() { console.log(x + y); y++; ; var g = f(); g(); // 1+2 is 3 g(); // 1+3 is 4 Global var x 1 f() var y 3 Closure function 19

Closures var x = 1; function f() { var y = 2; return function() { console.log(x + y); y++; ; var g = f(); g(); // 1+2 is 3 g(); // 1+3 is 4 f() Global var x 1 var y 4 Closure function 20

Modules We can do it with closures! Define a function Variables/functions defined in that function are private Return an object - every member of that object is public! Remember: Closures have access to the outer function s variables even after it returns 21

Modules with Closures var facultyapi = (function(){ var faculty = [{name:"prof Bell", section: 2, {name:"prof LaToza", section:1]; return { getfaculty : function(i) { return faculty[i].name + " ("+faculty[i].section +")"; ; )(); console.log(facultyapi.getfaculty(0)); This works because inner functions have visibility to all variables of outer functions! 22

Closures gone awry var funcs = []; for (var i = 0; i < 5; i++) { funcs[i] = function() { return i; ; What is the output of funcs[0]()? >5 Why? Closures retain a pointer to their needed state! 23

Closures under control Solution: IIFE - Immediately-Invoked Function Expression function makefunction(n) { return function(){ return n; ; for (var i = 0; i < 5; i++) { funcs[i] = makefunction(i); Why does it work? Each time the anonymous function is called, it will create a new variable n, rather than reusing the same variable i Shortcut syntax: var funcs = []; for (var i = 0; i < 5; i++) { funcs[i] = (function(n) { return function() { return n; )(i); 24

Exercise: Closures var facultyapi = (function(){ var faculty = [{name:"prof Bell", section: 2, {name:"prof LaToza", section:1]; return { getfaculty : function(i) { return faculty[i].name + " ("+faculty[i].section +")"; ; )(); https://jsfiddle.net/hkcq5vpa/ https://jsfiddle.net/hkcq5vpa/1/ console.log(facultyapi.getfaculty(0)); Here s our simple closure. Add a new function to create a new faculty, then call getfaculty to view their formatted name. 25

Classes A small correction: * Lecture 4, JavaScript 26

Classes ES6 introduces the class keyword Mainly just syntax - still not like Java Classes Old New function Faculty(first, last, teaches, office) { this.firstname = first; this.lastname = last; this.teaches = teaches; this.office = office; this.fullname = function(){ return this.firstname + " " + this.lastname; var profjon = new Faculty("Jonathan", "Bell", "SWE432", "ENGR 4322"); class Faculty { constructor(first, last, teaches, office) { this.firstname = first; this.lastname = last; this.teaches = teaches; this.office = office; fullname() { return this.firstname + " " + this.lastname; var profjon = new Faculty("Jonathan", "Bell", "SWE432", "ENGR 4322"); 27

Classes - Extends extends allows an object created by a class to be linked to a super class. Can (but don t have to) add parent constructor. class Faculty { constructor(first, last, teaches, office) { this.firstname = first; this.lastname = last; this.teaches = teaches; this.office = office; fullname() { return this.firstname + " " + this.lastname; class CoolFaculty extends Faculty { fullname() { return "The really cool " + super.fullname(); 28

Classes - static static declarations in a class work like in Java class Faculty { constructor(first, last, teaches, office) { this.firstname = first; this.lastname = last; this.teaches = teaches; this.office = office; fullname() { return this.firstname + " " + this.lastname; static formatfacultyname(f) { return f.firstname + " " + f.lastname; 29

Modules (ES6) With ES6, there is finally language support for modules Module must be defined in its own JS file Modules export declarations Publicly exposes functions as part of module interface Code imports modules (and optionally only parts of them) Specify module by path to the file 30

Modules (ES6) - Export Syntax var faculty = [{name:"prof Bell", section: 2, {name:"prof LaToza", section:1]; export function getfaculty(i) { Label each declaration with //.. export export var somevar = [1,2,3]; var faculty = [{name:"prof Bell", section: 2, {name:"prof LaToza", section:1]; var somevar = [1,2,3]; function getfaculty(i) { //.. export {getfaculty, somevar; Or name all of the exports at once export {getfaculty as aliasforfunction, somevar; Can rename exports too export default function getfaculty(i){... Default export 31

Modules (ES6) - Import Syntax Import specific exports, binding them to the same name import { getfaculty, somevar from "mymodule"; getfaculty()... Import specific exports, binding them to a new name import { getfaculty as aliasforfaculty from "mymodule"; aliasforfaculty()... Import default export, binding to specified name import thething from "mymodule"; thething()... -> calls getfaculty() Import all exports, binding to specified name import * as facmodule from "mymodule"; facmodule.getfaculty()... 32

Patterns for using/creating libraries Try to reuse as much as possible! Name your module in all lower case, with hyphens Include: README.md keywords, description, and license in package.json (from npm init) Strive for high cohesion, low coupling Separate models from views How much code to put in a single module? Cascades (see jquery) 33

Cascade Pattern aka chaining Offer set of operations that mutate object and returns the this object Build an API that has single purpose operations that can be combined easily Lets us read code like a sentence Example (String): str.replace("k","r").touppercase().substr(0,4); Example (jquery): $( #wrapper").fadeout().html( Welcome").fadeIn(); 34

Demo: Modules Not yet supported by any browser! 35

Closures Exercise Work from our example before of the Faculty Closure API to create a Class API (with Closures). Private fields: Faculty API List of students (students are objects with names, section numbers, and partners [which are students]) Public functions: Add a student to the class Retrieve the name of the student s faculty https://jsfiddle.net/hkcq5vpa/1/ https://jsfiddle.net/hkcq5vpa/3/ 36

Exit-Ticket Activity Go to socrative.com and select Student Login Class: SWE432001 (Prof LaToza) or SWE432002 (Prof Bell) ID is your @gmu.edu email 1: How well did you understand today's material 2: What did you learn in today's class? For question 3: What happens when the user clicks on the 4th button on this page and why? var nodes = document.getelementsbytagname('button'); for (var i = 0; i < nodes.length; i++) { nodes[i].addeventlistener('click', function() { console.log('you clicked element #' + i); );