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

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

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

FALL 2017 CS 498RK JAVASCRIPT. Fashionable and Functional!

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

Programming II (CS300)

A Java Execution Simulator

Node.js Training JavaScript. Richard richardrodger.com

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

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

CSCE 156 Computer Science II

Scope and Parameter Passing 1 / 19

Web Application Development

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

Backend Development. SWE 432, Fall Web Application Development

CIS 110: Introduction to Computer Programming

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

INF5750. Introduction to JavaScript and Node.js

CSC207 Week 3. Larry Zhang

CMSC 132: Object-Oriented Programming II

JavaScript CS 4640 Programming Languages for Web Applications

Semantic Analysis. Lecture 9. February 7, 2018

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

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

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

CSE 70 Final Exam Fall 2009

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Catbook Workshop: Intro to NodeJS. Monde Duinkharjav

Chapter 6 Introduction to Defining Classes

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

Declarations and Access Control SCJP tips

CSCE 120: Learning To Code

Programming for Engineers in Python

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

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

CIS 110: Introduction to computer programming

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

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

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?

JavaScript for C# Programmers Kevin

Object Oriented Programming

Recap: Functions as first-class values

MatchaScript: Language Reference Manual Programming Languages & Translators Spring 2017

CS558 Programming Languages

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

PIC 10A Objects/Classes

JavaScript: Sort of a Big Deal,

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

Lecture 18 CSE11 Fall 2013 Inheritance

CS 231 Data Structures and Algorithms, Fall 2016

JavaScript and MVC Frameworks FRONT-END ENGINEERING

INF3110 Programming Languages Runtime Organization part II

XML JavaScript Object Notation JSON Cookies Miscellaneous What Javascript can t do. OOP Concepts of JS

iphone Application Programming Lecture 3: Swift Part 2

CS1004: Intro to CS in Java, Spring 2005

Programming II (CS300)

Introduction to the SharePoint Framework Bob German Principal Architect - BlueMetal. An Insight company

Welcome to Design Patterns! For syllabus, course specifics, assignments, etc., please see Canvas

LEARN WITH INTRODUCTION TO TYPESCRIPT

CSE 341, Autumn 2015, Ruby Introduction Summary

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

JavaScript CS 4640 Programming Languages for Web Applications

Object Oriented Programming

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

Linked lists Tutorial 5b

Grade Syncing. Follow these instructions for syncing Top Hat grades to your Canvas course.

COMP 2355 Introduction to Systems Programming

Object-Oriented Principles and Practice / C++

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

Object Oriented Programming. I. Object Based Programming II. Object Oriented Programming

CMSC 132: Object-Oriented Programming II

Create a Java project named week9

G Programming Languages - Fall 2012

Ticket Machine Project(s)

COMP-520 GoLite Tutorial

Programming Languages

Scope, Functions, and Storage Management

CS193P - Lecture 3. iphone Application Development. Custom Classes Object Lifecycle Autorelease Properties

[2:3] Linked Lists, Stacks, Queues

Cpt S 122 Data Structures. Inheritance

Instantiation of Template class

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

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

CS1 Lecture 5 Jan. 25, 2019

And Even More and More C++ Fundamentals of Computer Science

G Programming Languages - Fall 2012

CSCI-1200 Data Structures Fall 2018 Lecture 7 Templated Classes & Vector Implementation

Chapter 5. Names, Bindings, and Scopes

Chapter 5 Names, Binding, Type Checking and Scopes

Inheritance. Transitivity

FUNCTIONS. The Anatomy of a Function Definition. In its most basic form, a function definition looks like this: function square(x) { return x * x; }

CGS 2405 Advanced Programming with C++ Course Justification

TypeScript. Types. CS144: Web Applications

ENCM 339 Fall 2017: Editing and Running Programs in the Lab

Inheritance and Interfaces

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

Garbage Collection (1)

Decorators. Userland Extensions to ES6 Classes

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

CSE 341: Programming Languages

Transcription:

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

Today HW1 assigned today. Due in 1 week Lecture: Organizing code in web apps Some basics on how and why to organize code Closures Classes Modules 2

JavaScript Files Can create a plain text file with.js extension that contains JavaScript code. Can contain whatever expressions and code you d like. We ll look at how to run these next lecture. For now, use a pastebin. 3

Combining files What happens when there are two or more places where JavaScript code is declared? Java: each has it's own scope (e.g., Class) JavaScript: code is concatenated together Can reference variables declared in a different file Sounds great! So convenient But is there a downside here? 4

Spaghetti Code Brian Foote and Joe Yoder LaToza/Bell GMU SWE 432 Fall 2016 5

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), newval = 0; switch (operator) { case '+': newval = add(lastnumber, currnumber); LaToza/Bell break; case '-': GMU SWE 432 Fall 2016 6

aka big ball of mud aka shanty town code Brian Foote and Joe Yoder LaToza/Bell GMU SWE 432 Fall 2016 7

Bad Code Smells Tons of not-very related functions in the same file No/uninformative comments Hard to understand 8

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

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 10

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 11

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 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 LaToza/Bell GMU SWE 432 Fall 2016 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 LaToza/Bell GMU SWE 432 Fall 2016 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 LaToza/Bell GMU SWE 432 Fall 2016 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! LaToza/Bell GMU SWE 432 Fall 2016 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! LaToza/Bell GMU SWE 432 Fall 2016 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); LaToza/Bell GMU SWE 432 Fall 2016 24

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

Classes ES6 introduces the class keyword Mainly just syntax 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 proflatoza = new Faculty("Thomas", "LaToza", "SWE432", "ENGR 4431 ); 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("Thomas", "LaToza", "SWE432", "ENGR 4431 ); 26

Member variables Can t declare member variables explicitly like in Java. Instead, create them implicitly by referencing this 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("Thomas", "LaToza", "SWE432", "ENGR 4431 ); 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

Demo: Classes 30

Modules 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 31

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 32

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()... 33

Classes Exercise Build a Course Class Should have following member variables: students (Array of objects with firstname and lastname) instructor (object with firstname, lastname, office #) meeting times (Array of objects with string of meeting time) classroom (String) Public functions: Constructor Add a student to the class Get an Array of meeting times for a course 34

Readings for next time Intro to Git: https://try.github.io Intro to Node Package Manager (NPM): https:// nodesource.com/blog/an-absolute-beginnersguide-to-using-npm/ 35