Fun with Development

(regarding variable names) If you can't pronounce it, you can't discuss it without sounding like an idiot. (...) This matters because programming is a social activity.
-- Bob Martin  (Clean Code, chapter 2)

Variable names should be

PRONOUNCEable and SEARCHable

Don’t be afraid to make a name long. A long descriptive name is better than a short
enigmatic name. A long descriptive name is better than a long descriptive comment.

Clean Code, chapter 3, section "Use descriptive names" (page 70)

Don’t be afraid to spend time choosing a name (...) Modern IDEs like Eclipse or IntelliJ make
it trivial to change names.

Naming - Avoid mental mapping

  • variable names shouldn't have to be translated
    • single-letter names for some loop counters ONLY!
    • use terms related to the problem or solution domains
    • (personal note) don't lie! (shamelessly copy code without adapting names)
  • "clarity is king" - smart programmer vs professional programmer
  • "clarity over entertainment" - "deleteItems" vs "unleashTheKraken", which is clearer?
  •  

"There can be no worse reason for using the name c than because a and b were already taken" -- Bob Martin, chapter 2

Anything that forces you to check the function signature is equivalent to a double-take. It’s a cognitive break and should be avoided.

Functions should either do something or answer something, but not both. Either your function should change the state of an object, or it should return some information about that object (command query separation)

Functions should do one thing. Error hand(l)ing is one thing

(...) if the keyword try exists in a function, it should be the very first word

(...) there should be nothing after the catch/finally blocks

public void delete(Page page) {
try {
deletePageAndAllReferences(page);
}
catch (Exception e) {
logError(e);
}
}
private void deletePageAndAllReferences(Page page) throws Exception {
deletePage(page);
registry.deleteReference(page.name);
configKeys.deleteKey(page.name.makeKey());
}
private void logError(Exception e) {
logger.log(e.getMessage());
}

Summary

  • Interesting quotes to share
  • Fun examples to learn basics and details

Make programming appealing on many levels (beginner and intermediate)

Wanna learn...

JavaScript?

JS - Code Monkey

('b' + 'a' + + 'a' + 'a').toLowerCase()

Hints


('b' + 'a' + + 'a' + 'a') // baNaNa
('b' + 'a' + + 'a') // baNaN
('ba' + + 'a') // baNaN
('a' + + 'a') // aNaN
( + 'a') // NaN

Answer: "banana"

JS - The Brown Knight

new Array(16).join('wat' - 1) + ' ??????' 

Explanation

'wat' + 1 // 'wat1'
'wat' - 1 // NaN
'wat' - 1 + 'wat' - 1 // 'NaNNaN' (string)

new Array(4) // [, , ,]
new Array(4).join('wat') // 'watwatwat' (3)

new Array(4).join('wat' + 1) // 'wat1wat1wat1'
new Array(4).join('wat' - 1) // 'NaNNaNNaN'

Answer: = 'NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN Batman'

JS - Pair of arrays glasses

[] + []
{} + {}

from Gary Bernhardt's Wat

explanation by 

Answer: "" and NaN

JS - Eye patches

[] + {}
{} + []

from Gary Bernhardt's Wat

explanation by 

Answers: "[Object object]" (string) and 0

Explanation

  •  

Wanna learn...

Ruby?

Ruby - Smoke and mirrors

# with 'a' undefined...
a = a

from Gary Bernhardt's Wat

explanation by 

Answer: Nil (error not thrown)

Try to interpret or build something 

VERY SIMPLE

 

... in a weird programming language

  • Rockstar
  • Brainf*ck
He is a developer
Done is his limit
While he is not Done
Build He up

Shout he

What's this output in Rockstar?

Build something weird and dumb...

Inspired by Sara Vieira's talk, "Build dumb sh*t"... and by Tim Holman, and ???, and myself!

Try to interpret or build something 

VERY SIMPLE

 

... in a weird programming language

Midnight takes your heart and your soul
While your heart is as high as your soul
Put your heart without your soul into your heart

Give back your heart

Desire is a lovestruck ladykiller
My world is nothing 
Fire is ice
Hate is water
Until my world is Desire,
Build my world up
If Midnight taking my world, Fire is nothing and Midnight taking my world, Hate is nothing
Shout "FizzBuzz!"
Take it to the top

If Midnight taking my world, Fire is nothing
Shout "Fizz!"
Take it to the top

If Midnight taking my world, Hate is nothing
Say "Buzz!"
Take it to the top

Whisper my world

FizzBuzz in Rockstar (up to 100)

Sources

Have fun and learn Programming

By Miguel Costa

Have fun and learn Programming

It's easier to keep learning if you're having fun doing it!

  • 399