Abstract Your Code!

139
mitcho (Michael 貴 Erlewine) http://mitcho.com May 1, 2010 WordCamp San Francisco Abstract Your Code! CC-BY flickr.com/photos/tupwanders/79745937/

description

 

Transcript of Abstract Your Code!

Page 1: Abstract Your Code!

mitcho (Michael 芳貴 Erlewine)http://mitcho.com

May 1, 2010WordCamp San Francisco

AbstractYourCode!

CC-BY flickr.com/photos/tupwanders/79745937/

Page 2: Abstract Your Code!

Introduction

Page 3: Abstract Your Code!

Introduction

• Hi, I’m mitcho.

Page 4: Abstract Your Code!

Introduction

• Hi, I’m mitcho.• Linguist, coder, teacher.

Page 5: Abstract Your Code!

Introduction

• Hi, I’m mitcho.• Linguist, coder, teacher.• http://mitcho.com; @mitchoyoshitaka

Page 6: Abstract Your Code!

Introduction

• Hi, I’m mitcho.• Linguist, coder, teacher.• http://mitcho.com; @mitchoyoshitaka• Linguistics PhD student at MIT

Page 7: Abstract Your Code!

Introduction

• Hi, I’m mitcho.• Linguist, coder, teacher.• http://mitcho.com; @mitchoyoshitaka• Linguistics PhD student at MIT• Gameloft, Mozilla, Automattic

Page 8: Abstract Your Code!

Yet Another Related Posts Plugin (YARPP) YARP

P!

Page 9: Abstract Your Code!

• smart “related posts” listings with lots of customization options

Yet Another Related Posts Plugin (YARPP) YARP

P!

Page 10: Abstract Your Code!

• smart “related posts” listings with lots of customization options• mitcho.com/code/yarpp or search for “YARPP”; @yarpp

Yet Another Related Posts Plugin (YARPP) YARP

P!

Page 11: Abstract Your Code!

• smart “related posts” listings with lots of customization options• mitcho.com/code/yarpp or search for “YARPP”; @yarpp• just under 500k downloads

Yet Another Related Posts Plugin (YARPP) YARP

P!

Page 12: Abstract Your Code!

HookPress

Page 13: Abstract Your Code!

HookPress

• opens up the WordPress hook system to remote scripts

Page 14: Abstract Your Code!

HookPress

• opens up the WordPress hook system to remote scripts• extend WordPress with other languages

Page 15: Abstract Your Code!

HookPress

• opens up the WordPress hook system to remote scripts• extend WordPress with other languages• now implemented on WordPress.com!

Page 16: Abstract Your Code!

HookPress

• opens up the WordPress hook system to remote scripts• extend WordPress with other languages• now implemented on WordPress.com!• mitcho.com/code/hookpress; @hookpress

Page 17: Abstract Your Code!

So, you’re a programmer...

Page 18: Abstract Your Code!

You customize WordPress for a project.

Page 19: Abstract Your Code!

what if...

Page 21: Abstract Your Code!

• they move servers and your custom code breaks...

CC-BY-NC-SA flickr.com/photos/slworking/1704538333

Page 22: Abstract Your Code!

• they move servers and your custom code breaks...• they want to reuse some (not all) of the functionality on another site...

CC-BY-NC-SA flickr.com/photos/slworking/1704538333

Page 23: Abstract Your Code!

• they move servers and your custom code breaks...• they want to reuse some (not all) of the functionality on another site...• they want modifications to your code, but you’re busy, and the new coder can’t understand your code...

CC-BY-NC-SA flickr.com/photos/slworking/1704538333

Page 24: Abstract Your Code!

• they move servers and your custom code breaks...• they want to reuse some (not all) of the functionality on another site...• they want modifications to your code, but you’re busy, and the new coder can’t understand your code...disaster!

CC-BY-NC-SA flickr.com/photos/slworking/1704538333

Page 25: Abstract Your Code!
Page 26: Abstract Your Code!

Abstraction!

Page 27: Abstract Your Code!

In the development of the understanding of complex phenomena, the most powerful tool available to the human intellect is abstraction. Abstraction arises from the recognition of similarities between certain objects, situations, or processes in the real world and the decision to concentrate on these similarities and to ignore, for the time being, their differences.

— C.A.R. Hoare

Page 28: Abstract Your Code!

Abstraction!

Page 29: Abstract Your Code!

Abstraction!=

thinking about the more general problem

Page 30: Abstract Your Code!

result=

Page 31: Abstract Your Code!

result=

code that you and others can reuse

Page 32: Abstract Your Code!

How?

Page 33: Abstract Your Code!

Think abstractly

Page 34: Abstract Your Code!

• Produce “functionalities,” not a monolithic project

Think abstractly

Page 35: Abstract Your Code!

• Produce “functionalities,” not a monolithic project• Write each functionality as its own plugin

Think abstractly

Page 36: Abstract Your Code!

• Produce “functionalities,” not a monolithic project• Write each functionality as its own plugin• Name functions and variables by functionality, not the project

Think abstractly

Page 37: Abstract Your Code!

• Produce “functionalities,” not a monolithic project• Write each functionality as its own plugin• Name functions and variables by functionality, not the project

• Even interrelated code can be modularized

Think abstractly

Page 38: Abstract Your Code!

• Produce “functionalities,” not a monolithic project• Write each functionality as its own plugin• Name functions and variables by functionality, not the project

• Even interrelated code can be modularized• introduce new hooks and filters with do_action(), apply_filters()

Think abstractly

Page 39: Abstract Your Code!

WordPress gives you abstractions

Page 40: Abstract Your Code!

WordPress gives you abstractions

Use them.

Page 41: Abstract Your Code!

WordPress gives you abstractions

Use them.• Database query? $wpdb, not mysql_*

Page 42: Abstract Your Code!

WordPress gives you abstractions

Use them.• Database query? $wpdb, not mysql_*• Caching? Object Cache and Transients API

Page 43: Abstract Your Code!

WordPress gives you abstractions

Use them.• Database query? $wpdb, not mysql_*• Caching? Object Cache and Transients API• Options? set_option() etc. not db

Page 44: Abstract Your Code!

WordPress gives you abstractions

Use them.• Database query? $wpdb, not mysql_*• Caching? Object Cache and Transients API• Options? set_option() etc. not db• Getting posts? get_posts() etc. not SQL

Page 45: Abstract Your Code!

WordPress gives you abstractions

Use them.• Database query? $wpdb, not mysql_*• Caching? Object Cache and Transients API• Options? set_option() etc. not db• Getting posts? get_posts() etc. not SQL• Custom data? Custom taxonomy or fields

Page 46: Abstract Your Code!

WordPress gives you abstractions

Use them.• Database query? $wpdb, not mysql_*• Caching? Object Cache and Transients API• Options? set_option() etc. not db• Getting posts? get_posts() etc. not SQL• Custom data? Custom taxonomy or fields• New entities? Custom post types!

Page 47: Abstract Your Code!

WordPress gives you abstractions

Use them.• Database query? $wpdb, not mysql_*• Caching? Object Cache and Transients API• Options? set_option() etc. not db• Getting posts? get_posts() etc. not SQL• Custom data? Custom taxonomy or fields• New entities? Custom post types!• External request? wp_remote_post() not curl

Page 48: Abstract Your Code!

Don’t reinvent the wheel.

© 2008 Drew and Natalie Dee, toothpastefordinner.com

Page 49: Abstract Your Code!

Be mobile

Page 50: Abstract Your Code!

• Don’t expect to be in a particular place

Be mobile

Page 51: Abstract Your Code!

• Don’t expect to be in a particular place• Use __FILE__, WP_PLUGIN_URL, and friends

Be mobile

Page 52: Abstract Your Code!

• Don’t expect to be in a particular place• Use __FILE__, WP_PLUGIN_URL, and friends• Get info with get_bloginfo()

Be mobile

Page 53: Abstract Your Code!

• Don’t expect to be in a particular place• Use __FILE__, WP_PLUGIN_URL, and friends• Get info with get_bloginfo()• Use $wpdb so you can forget about database settings

Be mobile

Page 54: Abstract Your Code!

Be classy

Page 55: Abstract Your Code!

Be classy

• Object oriented programming is perfect for abstracting “functionality”

Page 56: Abstract Your Code!

Be classy

• Object oriented programming is perfect for abstracting “functionality”• Write your functionality as a class with some defaults. Easy for others to extend.

Page 57: Abstract Your Code!

Be classy

• Object oriented programming is perfect for abstracting “functionality”• Write your functionality as a class with some defaults. Easy for others to extend.• Makes for clean, legible code

Page 58: Abstract Your Code!

Be classy

• Object oriented programming is perfect for abstracting “functionality”• Write your functionality as a class with some defaults. Easy for others to extend.• Makes for clean, legible code• less prefix crud

Page 59: Abstract Your Code!

Be classy

• Object oriented programming is perfect for abstracting “functionality”• Write your functionality as a class with some defaults. Easy for others to extend.• Makes for clean, legible code• less prefix crud• A great example: PuSHPress

Page 60: Abstract Your Code!

Give yourself options

Page 61: Abstract Your Code!

Give yourself options

• Don’t hard code the project name, copyright strings, email bodies, etc.

Page 62: Abstract Your Code!

Give yourself options

• Don’t hard code the project name, copyright strings, email bodies, etc.• Bare minimum: declare these constants in a separate config file

Page 63: Abstract Your Code!

Give yourself options

• Don’t hard code the project name, copyright strings, email bodies, etc.• Bare minimum: declare these constants in a separate config file• Ideally: create an options screen

Page 64: Abstract Your Code!

Give yourself options

• Don’t hard code the project name, copyright strings, email bodies, etc.• Bare minimum: declare these constants in a separate config file• Ideally: create an options screen• Not as hard as you think

Page 65: Abstract Your Code!

Give yourself options

• Don’t hard code the project name, copyright strings, email bodies, etc.• Bare minimum: declare these constants in a separate config file• Ideally: create an options screen• Not as hard as you think• *_option() functions are your friend!

Page 66: Abstract Your Code!

Learn from the masters

Page 67: Abstract Your Code!

• http://codex.wordpress.org

Learn from the masters

Page 68: Abstract Your Code!

• http://codex.wordpress.org• There’s more there than you think.

Learn from the masters

Page 69: Abstract Your Code!

• http://codex.wordpress.org• There’s more there than you think.• Read others’ code.

Learn from the masters

Page 70: Abstract Your Code!

• http://codex.wordpress.org• There’s more there than you think.• Read others’ code.• Borrow and adapt from other plugins, themes, and the Core

Learn from the masters

Page 71: Abstract Your Code!

• http://codex.wordpress.org• There’s more there than you think.• Read others’ code.• Borrow and adapt from other plugins, themes, and the Core• “free as in freedom!”

Learn from the masters

Page 72: Abstract Your Code!

CC-BY-NC xkcd.com/292/

Good practice matters

Page 73: Abstract Your Code!

Abstraction!

Page 74: Abstract Your Code!

Results

Page 75: Abstract Your Code!

• Won’t [Less likely to] break when you move machines or upgrade WordPress

Results

Page 76: Abstract Your Code!

• Won’t [Less likely to] break when you move machines or upgrade WordPress• Easier to maintain

Results

Page 77: Abstract Your Code!

• Won’t [Less likely to] break when you move machines or upgrade WordPress• Easier to maintain• Easier (for you or others) to build on

Results

Page 78: Abstract Your Code!

• Won’t [Less likely to] break when you move machines or upgrade WordPress• Easier to maintain• Easier (for you or others) to build on• Easier to reuse elsewhere

Results

Page 79: Abstract Your Code!

but wait, there’s more...

Page 81: Abstract Your Code!

CC-BY-NC-SA flickr.com/photos/z6p6tist6/500048151/

is not a one way street.

Free software

Page 82: Abstract Your Code!

CC-BY-NC-SA flickr.com/photos/z6p6tist6/500048151/

is not a one way street.

WordPress

Page 83: Abstract Your Code!

Gedankenexperiment:

Page 84: Abstract Your Code!

• You just customized WordPress for a client

Gedankenexperiment:

Page 85: Abstract Your Code!

• You just customized WordPress for a client• Others could also benefit from this feature

Gedankenexperiment:

Page 86: Abstract Your Code!

• You just customized WordPress for a client• Others could also benefit from this feature• You and the client have already benefited from free software: WordPress

Gedankenexperiment:

Page 87: Abstract Your Code!

Consider open-sourcing your client-requested functionality.

Page 88: Abstract Your Code!
Page 89: Abstract Your Code!

Do people really do this?

Page 90: Abstract Your Code!

success stories:

Page 91: Abstract Your Code!

success stories:

My own work at MIT

Page 92: Abstract Your Code!
Page 93: Abstract Your Code!
Page 94: Abstract Your Code!

Custom Comment Typessupported by the MIT Edgerton Digital Collections

Page 95: Abstract Your Code!
Page 96: Abstract Your Code!

Licensesupported by the MIT Department of Mathematics

Page 97: Abstract Your Code!

Licensesupported by the MIT Department of Mathematics

Page 98: Abstract Your Code!

MIT Department of Mathematics:

Page 99: Abstract Your Code!

MIT Department of Mathematics:

• Shibboleth authentication

Page 100: Abstract Your Code!

MIT Department of Mathematics:

• Shibboleth authentication• now contributing to the pre-existing Shibboleth plugin

Page 101: Abstract Your Code!

MIT Department of Mathematics:

• Shibboleth authentication• now contributing to the pre-existing Shibboleth plugin• MIT customizations can be reused in other MIT projects

Page 102: Abstract Your Code!

Brad Williamsco-founder, WebDevStudios.com

success stories:

Page 103: Abstract Your Code!

Brad Williams

Page 104: Abstract Your Code!

• NextGEN Public Uploader

Brad Williams

Page 105: Abstract Your Code!

• NextGEN Public Uploader• Post Google Map

Brad Williams

Page 106: Abstract Your Code!

• NextGEN Public Uploader• Post Google Map• sponsored by injersey.com

Brad Williams

Page 107: Abstract Your Code!

Brad Williams

Page 108: Abstract Your Code!

• no client has said no

Brad Williams

Page 109: Abstract Your Code!

• no client has said no• if the client is on the fence, mention the ongoing support benefits

Brad Williams

Page 110: Abstract Your Code!

• no client has said no• if the client is on the fence, mention the ongoing support benefits• many clients have never thought about open-sourcing functionality, but most WordPress people want to give back

Brad Williams

Page 111: Abstract Your Code!

• no client has said no• if the client is on the fence, mention the ongoing support benefits• many clients have never thought about open-sourcing functionality, but most WordPress people want to give back• plugins have brought in new clients

Brad Williams

Page 112: Abstract Your Code!

John James JacobyJohn James Jacoby BuddyPress Consulting

and Custom Development

success stories:

Page 113: Abstract Your Code!
Page 114: Abstract Your Code!

BuddyPress Member Filterdevelopment supported by LOGOI Ministries

Page 115: Abstract Your Code!
Page 116: Abstract Your Code!

BuddyPress Edit Group Slugdevelopment supported by We Heart This

Page 117: Abstract Your Code!

John James Jacoby

Page 118: Abstract Your Code!

John James Jacoby

• educate the customer about community role and open-source

Page 119: Abstract Your Code!

John James Jacoby

• educate the customer about community role and open-source• sometimes it’s appropriate to just open-source the backend parts without UI

Page 120: Abstract Your Code!

John James Jacoby

• educate the customer about community role and open-source• sometimes it’s appropriate to just open-source the backend parts without UI• community contributions got him noticed

Page 121: Abstract Your Code!

John James Jacoby

• educate the customer about community role and open-source• sometimes it’s appropriate to just open-source the backend parts without UI• community contributions got him noticed• BuddyPress Core contributor

Page 122: Abstract Your Code!

Tips

Page 123: Abstract Your Code!

Tips

• bring up open-sourcing early

Page 124: Abstract Your Code!

Tips

• bring up open-sourcing early• if they say no, respect their choice

Page 125: Abstract Your Code!

Tips

• bring up open-sourcing early• if they say no, respect their choice• don’t pass the cost back to the client

Page 126: Abstract Your Code!

Tips

• bring up open-sourcing early• if they say no, respect their choice• don’t pass the cost back to the client• acknowledge the funding

Page 127: Abstract Your Code!

Tips

• bring up open-sourcing early• if they say no, respect their choice• don’t pass the cost back to the client• acknowledge the funding• don’t forget that you can contribute to existing projects as well

Page 128: Abstract Your Code!

Abstraction

Page 129: Abstract Your Code!

Abstraction

FTW!

Page 130: Abstract Your Code!

More FTW = less WTF

Page 131: Abstract Your Code!

The bottom line

Page 132: Abstract Your Code!

• Abstract your code:

The bottom line

Page 133: Abstract Your Code!

• Abstract your code:• Client gets modular, future-proof, understandable code.

The bottom line

Page 134: Abstract Your Code!

• Abstract your code:• Client gets modular, future-proof, understandable code.• Less headaches for you and your team.

The bottom line

Page 135: Abstract Your Code!

• Abstract your code:• Client gets modular, future-proof, understandable code.• Less headaches for you and your team.• Open-source your customizations:

The bottom line

Page 136: Abstract Your Code!

• Abstract your code:• Client gets modular, future-proof, understandable code.• Less headaches for you and your team.• Open-source your customizations:• Client can contribute to WordPress.

The bottom line

Page 137: Abstract Your Code!

• Abstract your code:• Client gets modular, future-proof, understandable code.• Less headaches for you and your team.• Open-source your customizations:• Client can contribute to WordPress.• You become famous.

The bottom line

Page 138: Abstract Your Code!

• Abstract your code:• Client gets modular, future-proof, understandable code.• Less headaches for you and your team.• Open-source your customizations:• Client can contribute to WordPress.• You become famous.

The bottom line

Win!Win!

Win!

Win!

Page 139: Abstract Your Code!

Thank you!Questions?

Slides will be up on mitcho.com/blog/.

mitcho (Michael 芳貴 Erlewine)mitcho.com; @mitchoyoshitaka