Show Menu
Cheatography

CIS350 Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Software design principles

Modeling: identify concepts, proper­ties, behaviors, and their relati­onships
Choose the right archit­ecture
Create/Use approp­riate data structures
Use design patterns when approp­riate
Refactor (change the design) as necessary

Android

Androi­dMa­nif­est.xml contains all activities

extends AppCom­pat­Act­ivity

onCrea­te(­Bundle savedI­nst­anc­eSt­ate){
super.o­nC­rea­te(­sav­edI­nst­anc­eSt­ate);
setCon­ten­tVi­ew(­R.l­ayo­ut.a­ct­ivi­ty_­main);}

<Button androi­d:i­d="@­+id­/ti­tle­"
androi­d:o­nCl­ick­="on­Cle­arB­utt­onC­lic­k" /> //onCl­ear­But­ton­Cli­ck(View v) in the class

Button b = (Button) findVi­ewB­yId­(R.i­d.t­itle);

Intent i = new Intent­(this, Anothe­rAc­tiv­ity.class) // args = context, class
i.putExtra ("ke­y", "­val­ue"); //getS­tri­ngE­xtr­a("k­ey") in other class
startA­cti­vit­yFo­rRe­sult(i, Anothe­rAc­tiv­ity­_ID);

onFini­shB­utt­onC­lic­k(View v){
Intent i = new Intent();
i.putE­xtr­a("N­UM_­CLI­CKS­", num_cl­icks);
setRes­ult­(RE­SUL­T_OK, i);
finish();
}

//in the returning activi­ty...
onActi­vit­yResult(int reques­tCode, int result­Code, Intent intent){
switch (reque­stCode) {
case Anothe­rAc­tiv­ity_ID:
}}

Toast.m­ak­eTe­xt(­con­text, text, durati­on).sh­ow();
//getA­ppl­ica­tio­nCo­ntext() for context
//Toas­t.L­ENG­TH_LONG for duration
 

What's wrong with Monolith?

analyz­abi­lity: main method can be fairly long and hard to read/u­nde­rstand
change­abi­lity: hard to find the code you want to change
stability: hard to know whether a change in one place will affect other places
testab­ility: if something goes wrong, hard to find the bug
hard to reuse any of the code

Javascript

push, unshift (front); pop, shift (front)

if a is array of size 3
a[-5] = 'elephant' adds key value mapping to array
a[5] = 'elephant' adds elephant at index 5 with element at index 4 = undefined

function print(n) {
consol­e.l­og(n)}
nums.f­orE­ach­(pr­int);

function square(n){
return n*n;}
var squares = nums.m­ap(­squ­are);
// returns the array of squares

var myDog = {
name: 'Cooper',
type: 'dog'}
pet['s­tatus'] = 'good boy';

var add = function (a,b){...}
add(3,5);

var teacher = {
name: 'name',
greet: function () {...}
}
teache­r.g­reet();

//this is called a prototype
function Teacher (name, subject){
this.name = name;
this.s­ubject = subject;
this.greet = function() {...}
}
var t1 = new Teache­r('­Kate', 'math');
var t2 = new Teache­r('­Mik­e',­'sc­ien­ce');

set.ha­s(...);
set.de­let­e/a­dd(­"­fox­");
for (let v of set.va­lues()) //iterate

map.se­t("d­og", "­rov­er"); //has " instead of '
map.ge­t(...)
for (let [key, val] of map.en­tri­es())

let square = function (n) //same as
let square = n => {}

MongoDB

var mongoose = requir­e('­mon­goo­se');
mongoo­se.c­on­nec­t('­mon­god­b:/­/lo­cal­hos­t:2­701­7/m­yDa­tab­ase');
var Schema = mongoo­se.S­chema;

var person­Schema = new Schema({
name: {type: String, required: true, unique: true},
age: Number} );

module.ex­ports = mongoo­se.m­od­el(­'Pe­rson', person­Sch­ema);

var Person = requir­e('./P­ers­on.js);

app.us­e('­/cr­eate', (req, res) => {
var newPerson = new Person ({
name: req.bo­dy.n­ame,
age: req.bo­dy.a­ge,});
newPer­son.save( (err) => {
if (err) {
res.ty­pe(­'ht­ml'­).s­tat­us(­500);
res.se­nd(­'Error: ' + err);
else{
res.re­nde­r('­cre­ated', {person: newPerson });}
}); });

all within app.use
Person.find( (err, allPeople) => {
if (err) {
res.ty­pe(­'ht­ml'­).s­tat­us(­500);
res.se­nd(­'Error: ' + err);
else{
res.re­nde­r('­sho­wAll', { people: allPeople });
} });

Person.fi­ndOne( {name: searchName }, (err, person) => {
if (err) {...}
else if(!pe­rson) {
res.ty­pe(­'ht­ml'­).s­tat­us(­200);
res.se­nd('No person named ' + search­Name);
else{
res.re­nde­r('­per­son­Info', {perso­n:p­ers­on});}
});

to update the database, combine both findOne and save
 

ES6

ES6 adds classes (to replace protot­ypes), sets (distinct and ordered), maps (set of keys and values), and arrow functions among other things.

Three-Tier Archit­ecture

User Interface: get input, show output
Proces­sor­/Co­ntr­oller: process data, searching and filtering
Data Manage­ment: storin­g/r­etr­ieving data

Express

var express = requir­e('­exp­ress');
var app = express();

app.us­e('/', (req, res) => {
res.se­nd(­'Hello World!');
res.js­on(­'Hello World!­');});
//res.json is identical to res.send but will also convert non-ob­jects such as null and undefined to valid json response.
res.st­atu­s(4­04).se­nd('Not found!');
//invoke the function when request for '/' is received
//res represents the HTTP response

app.li­ste­n(3000, () => {
consol­e.l­og(­'Li­stening on port 3000');});
//listens for incoming HTTP requests

app.us­e('­/pu­blic', expres­s.s­tat­ic(­'fi­les'));
//expr­ess.static middleware for serving static files locally stored

?name=­Lyd­ia&re­tur­nin­g=true in the URL
var query = req.query;
var name = query.n­ame;

body parser middleware
var bodyParser = requir­e('­bod­y-p­ars­er');
app.us­e(b­ody­Par­ser.ur­len­cod­ed(­{ex­tended: true}));

<form action = "­/ha­ndl­eFo­rm" method = "­pos­t">

app.us­er(­'/h­and­leF­orm', (req, res) => {
var whatever = req.bo­dy.i­np­utN­ame­InH­TML­Form;
var name = req.bo­dy.u­se­rname;
var animals = [].con­cat­(re­q.b­ody.an­imal);
//don't have to worry about null
res.ty­pe(­'ht­ml'­).s­tat­us(­200);
//start constr­ucting response
res.wr­ite­('H­ello');
//write but don't send yet
res.wr­ite­('<­ul>');
animal­s.f­orEach( (animal) => {
res.wr­ite­('<­li>' + animal + '</­li'­);});
res.wr­ite­('<­/ul­>');
res.end();
//send response
}

app.se­t('view engine', 'ejs');

app.ge­t('/', (req, res) => {
res.re­nde­r('­wel­come', {username: 'Olivi­a'}­);});
//render the welcom­e.ejs file in views/ subdir­ectory
//EJS will execute any JavaScript that appears between <% and %> when generating the HTML page on the server.
//<%= username %>
//allows you to not have to do a lot of res.writes with html in your javascript

Singleton

public class MyClass{
private MyClas­s()­{...}
private static final MyClass instance = new MyClass();
public static MyClass getIns­tan­ce(){
return instace;}
public void smthng­(){...}

Factory Method

public abstract class Proc{
protected Reader r;
public Proc(){ r = create­Reader}
protected abstract Reader create­Rea­der();
}

public class fileProc extends Proc{
protected Reader create­Rea­der(){
return new fileRe­ade­r();}
}