"a given object should assume as little as possible about the structure or properties of anything else"
a.k.a. "The Law of Least Knowledge"
don't let objects send messages to too many other objects
"...We are never ever ever getting
back together,
You go talk to your friends,
talk to my friends, talk to me
But we are never ever ever
ever getting back together..."
var you =
new Scumbag();
you
.yourFriends
.myFriends
.me
// returns 'Uncaught Exception: This code sucks'
#2
"Blank Space"
"Cause we're young and we're reckless
We'll take this way too far
It'll leave you breathless
Or with a nasty scar
Got a long list of ex-lovers
They'll tell you I'm insane
But I got a blank space baby
And I'll write your name"
var blank = "";
var blank += yourName;
# Ruby
print 'This is so fun!' if ''
# =>> 'This is so fun!'
// JavaScript
if ('') {
console.log('This is so fun!');
}
// =>> undefined
Web API
AJAX roulette
🎉🎊💥🎂
#3
"I Knew You
Were Trouble"
"I knew you were trouble when you walked in
So shame on me now
Flew me to places I'd never been
Now I'm lying on the cold hard ground
Oh, oh, trouble, trouble, trouble
Oh, oh, trouble, trouble, trouble"
Why is Taylor in so much trouble?
Because she didn't use a strongly-typed language.
public class Trouble{
public Trouble(String name){
System.out.println(
"I knew " + name +
" were trouble"
);
}
public static void main(String []args){
Trouble you = new Trouble(“you”);
}
}
(I am not a Java developer)
What about dynamically-typed languages?
def fun_with_types(n)
if n == 1
"Woohoo"
elsif n == 2
true
elsif n == 3
:wat
end
end
can !== should
#4
"You Belong
With Me"
"If you could see
That I'm the one
Who understands you.
Been here all along.
So, why can't you see
You belong with me,
You belong with me."