Today Programming Style and Your Brain And Then There Was JavaScript Function the Ultimate The Metamorphosis of Ajax ES5: The New Parts.

Slides:



Advertisements
Similar presentations
The JavaScript Programming Language
Advertisements

Introduction to Programming
JavaScript: A Language of Many Contrasts
Introducing JavaScript
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Internal Documentation Conventions. Kinds of comments javadoc (“doc”) comments describe the user interface: –What the classes, interfaces, fields and.
I just met you, and 'this' is crazy, but here's my NaN, so call(me) maybe? JavaScript is so weird.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 Changing Control.
JSON, ADsafe, and Misty Douglas Crockford Yahoo!.
14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,
Introduction to the C# Programming Language for the VB Programmer.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
16-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
The Programming Discipline Professor Stephen K. Kwan 2010 Things you need to know (learn) for developing large computer programs.
25-Jun-15 JavaScript Language Fundamentals II. 2 Exception handling, I Exception handling in JavaScript is almost the same as in Java throw expression.
28-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
XP Tutorial 1 New Perspectives on JavaScript, Comprehensive1 Introducing JavaScript Hiding Addresses from Spammers.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
Algorithm Programming Coding Advices Bar-Ilan University תשס " ו by Moshe Fresko.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
CSE 190M Extra Session #5 JavaScript scope and closures slides created by Marty Stepp
Taking JavaScript Seriously IS NOT THE WORST IDEA.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
1 JavaScript in Context. Server-Side Programming.
REPETITION CITS1001. Scope of this lecture Repetition for loops while loops 2.
Control Structures. Important Semantic Difference In all of these loops we are going to discuss, the braces are ALWAYS REQUIRED. Even if your loop/block.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Fall Week 4 CSCI-141 Scott C. Johnson.  Computers can process text as well as numbers ◦ Example: a news agency might want to find all the articles.
CPS120: Introduction to Computer Science Decision Making in Programs.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
USING UNITY JAVASCRIPT. CONVENTIONS AND SYNTAX IN JAVASCRIPT Case Sensitivity All keywords like var or function must be in lowercase. All variable names,
David Stotts Computer Science Department UNC Chapel Hill.
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
CSE 341 Lecture 29 a JavaScript, the bad parts slides created by Marty Stepp see also: JavaScript: The Good Parts, by.
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
JavaScript, Fourth Edition
1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i
Function the Ultimate Act III. Function Method Class Constructor Module.
JavaScript scope and closures
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
Expressions and Data Types Professor Robin Burke.
Week 3 - Friday.  What did we talk about last time?  Preprocessor directives  Other C features  sizeof, const  ASCII table  printf() format strings.
CS100Lecture 21 Announcements For homework due Thursday, work alone -- do not work in pairs New class location: Olin 155 Office hour oops! Lyn: MW, 11:15-12:15.
JavaScript and AJAX 2nd Edition Tutorial 1 Programming with JavaScript.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
JavaScript Variables. Definition A variable is a "container" for information you want to store. A variable's value can change during the script.
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
Today… Continue Building Expressions: –Literals –Constants –Casting –Array Declaration –Operators and Operator Precedence –Keywords Winter 2016CMPE212.
© 2010 Robert K. Moniot1 Chapter 6 Introduction to JavaScript.
Edited by Original material by Eric Grimson
JavaScript Syntax and Semantics
Statement atoms The 'atomic' components of a statement are: delimiters (indents, semicolons, etc.); keywords (built into the language); identifiers (names.
JavaScript.
Context.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Chapter 15 Debugging.
Testing and Repetition
CSE 341 Lecture 27 JavaScript scope and closures
Javascript Chapter 19 and 20 5/3/2019.
CSC215 Lecture Control Flow.
Presentation transcript:

Today Programming Style and Your Brain And Then There Was JavaScript Function the Ultimate The Metamorphosis of Ajax ES5: The New Parts

Programming Style & Your Brain Douglas Crockford

Head. Gut. Two Systems.

Visual Processing. An analogy.

Advertising.

Tobacco.

Computer Programs. The most complicated things people make.

Artificial Intelligence.

Programming Language.

Perfection.

Hunters and Gatherers.

Programming uses Head and Gut. Tradeoffs.

JavaScript. Good Parts. Bad Parts.

JSLint. JSLint defines a professional subset of JavaScript.

WARNING! JSLint will hurt your feelings.

Left or Right? block {.... } block {.... }

Left or Right? block {.... } Be consistent. block {.... } Everyone should do it like I do.

Left or Right? return { ok: false }; SILENT ERROR! return { ok: true }; Works well in JavaScript.

return { ok: false };

return; // semicolon insertion { ok: false };

return; { // block ok: false };

return; { ok: false // label };

return; { // useless ok: false // expression }; // statement

return; { ok: false; // semicolon }; // insertion

return; { ok: false; }; // empty statement

return; { // unreachable statement ok: false; }

return { ok: false }; return; { ok: false; }

Prefer forms that are error resistant.

switch statement. The fallthrough hazard.

“That hardly ever happens” is another way of saying “It happens”.

A good style can help produce better programs. Style should not be about personal preference and self- expression.

THEROMANSWROTELATIN ALLINUPPERCASEWITH NOWORDBREAKS ORPUNCTUATION

Medieval copyists introduced lowercase, word breaks, and punctuation. These innovations helped reduce the error rate.

Good use of style can help reduce the occurrence of errors.

The Elements of Style William Strunk

Programs must communicate clearly to people.

Use elements of good composition where applicable. For example, use a space after a comma, not before.

Use spaces to disambiguate parens. No space between a function name and (. One space between all other names and (. Wrong: foo (bar); return(a+b); if(a=== 0) {…} function foo (b) {…} function(x) {…}

Immediately Invocable Function Expressions function () {... }(); // Syntax error!

Immediately Invocable Function Expressions (function () {... })();

Immediately Invocable Function Expressions (function () {... })() Dog balls

Immediately Invocable Function Expressions (function () {... }()); // Neatness counts.

The Heartbreak of Automatic Semicolon Insertion x = y // <-- Missing semicolon (function () {... }()); Never rely on automatic semicolon insertion!

with statement. with (o) { foo = koda; }  o.foo = koda;  o.foo = o.koda;  foo = koda;  foo = o.koda;

with statement. with (o) { foo = koda; }  o.foo = koda;  o.foo = o.koda;  foo = koda;  foo = o.koda; I am not saying that it isn’t useful. I am saying that there is never a case where it isn’t confusing.

Confusion must be avoided.

Transitivity? What's That? 0 == '' // true 0 == '0' // true '' == '0' // false false == 'false' // false false == '0' // true " \t\r\n " == 0 // true Always use ===, never ==.

If there is a feature of a language that is sometimes problematic, and if it can be replaced with another feature that is more reliable, then always use the more reliable feature.

Multiline string literals var long_line_1 = "This is a \ long line"; // ok var long_line_2 = "This is a \ long line"; // syntax error

Avoid forms that are difficult to distinguish from common errors.

if (a = b) {…} a = b; if (a) {…} if (a === b) {…}

Make your programs look like what they do.

Scope. Block scope v function scope.

var statement. It gets split into two parts: The declaration part gets hoisted to the top of the function, initializing with undefined. The initialization part turns into an ordinary assignment. So function foo() {... var myVar = 0, myOtherVar; Expands into function foo() { var myVar = undefined, myOtherVar = undefined;... myVar = 0;

Declare all variables at the top of the function. Declare all functions before you call them.

for (var i …) {…} Variable i is not scoped to the loop.

Write in the language you are writing in.

Let there be let.

Global variables. Global variables are evil. Avoid global variables. When using global variables, be explicit. UPPER_CASE Global variables should be as rare as hens teeth and stick out like a sore thumb.

new prefix Forgetting new causes a constructor to clobber global variables without warning. Fixed in ES5/strict.

Constructor functions should be named with InitialCaps. Nothing else should be named with InitialCaps.

var a = b = 0; var a = 0, b = 0; b = 0; var a = b;

Write in a way that clearly communicates your intent.

if (a) b(); c();

if (a) {b(); c();} if (a) {b();} c();

As our processes become more agile, our coding must be more resilient.

++

x += 1

++ x += 1 x++

++ x += 1 x++ ++x

++x; x += 2;

For no cost, by adopting a more rigorous style, many classes of errors can be automatically avoided.

Bad stylists Under educated. Old school. Thrill seeker. Exhibitionist.

“That was intentional.” “I know what I’m doing.”

Performance. Performance specific code is usually crufty. Clean code is easier to reason about. Premature optimization is the root of all evil. D ONALD K NUTH Most of the code has a negligible impact on performance. Only optimize the code that is taking the time. Algorithm replacement is vastly more effective than code fiddling.

Programming is the most complicated thing that humans do. Computer programs must be perfect. Humans are not good at perfect.

Designing a programming style demands discipline. It is not selecting features because they are liked, or pretty, or familiar.

The Abyss

The JSLint style was driven by the need to automatically detect defects. Forms that can hide defects are considered defective.

Language Subsetting. Only a madman would use all of C++.

There will be bugs. Do what you can to move the odds to your favor.

Good style is good for your gut.

;