Abstract Your Code!
-
Upload
michael-yoshitaka-erlewine -
Category
Technology
-
view
2.348 -
download
1
description
Transcript of Abstract Your Code!
mitcho (Michael 芳貴 Erlewine)http://mitcho.com
May 1, 2010WordCamp San Francisco
AbstractYourCode!
CC-BY flickr.com/photos/tupwanders/79745937/
Introduction
Introduction
• Hi, I’m mitcho.
Introduction
• Hi, I’m mitcho.• Linguist, coder, teacher.
Introduction
• Hi, I’m mitcho.• Linguist, coder, teacher.• http://mitcho.com; @mitchoyoshitaka
Introduction
• Hi, I’m mitcho.• Linguist, coder, teacher.• http://mitcho.com; @mitchoyoshitaka• Linguistics PhD student at MIT
Introduction
• Hi, I’m mitcho.• Linguist, coder, teacher.• http://mitcho.com; @mitchoyoshitaka• Linguistics PhD student at MIT• Gameloft, Mozilla, Automattic
Yet Another Related Posts Plugin (YARPP) YARP
P!
• smart “related posts” listings with lots of customization options
Yet Another Related Posts Plugin (YARPP) YARP
P!
• 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!
• 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!
HookPress
HookPress
• opens up the WordPress hook system to remote scripts
HookPress
• opens up the WordPress hook system to remote scripts• extend WordPress with other languages
HookPress
• opens up the WordPress hook system to remote scripts• extend WordPress with other languages• now implemented on WordPress.com!
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
So, you’re a programmer...
You customize WordPress for a project.
what if...
CC-BY-NC-SA flickr.com/photos/slworking/1704538333
• they move servers and your custom code breaks...
CC-BY-NC-SA flickr.com/photos/slworking/1704538333
• 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
• 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
• 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
Abstraction!
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
Abstraction!
Abstraction!=
thinking about the more general problem
result=
result=
code that you and others can reuse
How?
Think abstractly
• Produce “functionalities,” not a monolithic project
Think abstractly
• Produce “functionalities,” not a monolithic project• Write each functionality as its own plugin
Think abstractly
• Produce “functionalities,” not a monolithic project• Write each functionality as its own plugin• Name functions and variables by functionality, not the project
Think abstractly
• 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
• 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
WordPress gives you abstractions
WordPress gives you abstractions
Use them.
WordPress gives you abstractions
Use them.• Database query? $wpdb, not mysql_*
WordPress gives you abstractions
Use them.• Database query? $wpdb, not mysql_*• Caching? Object Cache and Transients API
WordPress gives you abstractions
Use them.• Database query? $wpdb, not mysql_*• Caching? Object Cache and Transients API• Options? set_option() etc. not db
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
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
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!
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
Don’t reinvent the wheel.
© 2008 Drew and Natalie Dee, toothpastefordinner.com
Be mobile
• Don’t expect to be in a particular place
Be mobile
• Don’t expect to be in a particular place• Use __FILE__, WP_PLUGIN_URL, and friends
Be mobile
• Don’t expect to be in a particular place• Use __FILE__, WP_PLUGIN_URL, and friends• Get info with get_bloginfo()
Be mobile
• 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
Be classy
Be classy
• Object oriented programming is perfect for abstracting “functionality”
Be classy
• Object oriented programming is perfect for abstracting “functionality”• Write your functionality as a class with some defaults. Easy for others to extend.
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
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
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
Give yourself options
Give yourself options
• Don’t hard code the project name, copyright strings, email bodies, etc.
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
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
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
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!
Learn from the masters
• http://codex.wordpress.org
Learn from the masters
• http://codex.wordpress.org• There’s more there than you think.
Learn from the masters
• http://codex.wordpress.org• There’s more there than you think.• Read others’ code.
Learn from the masters
• 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
• 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
Abstraction!
Results
• Won’t [Less likely to] break when you move machines or upgrade WordPress
Results
• Won’t [Less likely to] break when you move machines or upgrade WordPress• Easier to maintain
Results
• 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
• 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
but wait, there’s more...
CC-BY-NC-SA flickr.com/photos/z6p6tist6/500048151/
CC-BY-NC-SA flickr.com/photos/z6p6tist6/500048151/
is not a one way street.
Free software
CC-BY-NC-SA flickr.com/photos/z6p6tist6/500048151/
is not a one way street.
WordPress
Gedankenexperiment:
• You just customized WordPress for a client
Gedankenexperiment:
• You just customized WordPress for a client• Others could also benefit from this feature
Gedankenexperiment:
• 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:
Consider open-sourcing your client-requested functionality.
Do people really do this?
success stories:
success stories:
My own work at MIT
Custom Comment Typessupported by the MIT Edgerton Digital Collections
Licensesupported by the MIT Department of Mathematics
Licensesupported by the MIT Department of Mathematics
MIT Department of Mathematics:
MIT Department of Mathematics:
• Shibboleth authentication
MIT Department of Mathematics:
• Shibboleth authentication• now contributing to the pre-existing Shibboleth plugin
MIT Department of Mathematics:
• Shibboleth authentication• now contributing to the pre-existing Shibboleth plugin• MIT customizations can be reused in other MIT projects
Brad Williamsco-founder, WebDevStudios.com
success stories:
Brad Williams
• NextGEN Public Uploader
Brad Williams
• NextGEN Public Uploader• Post Google Map
Brad Williams
• NextGEN Public Uploader• Post Google Map• sponsored by injersey.com
Brad Williams
Brad Williams
• no client has said no
Brad Williams
• no client has said no• if the client is on the fence, mention the ongoing support benefits
Brad Williams
• 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
• 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
John James JacobyJohn James Jacoby BuddyPress Consulting
and Custom Development
success stories:
BuddyPress Member Filterdevelopment supported by LOGOI Ministries
BuddyPress Edit Group Slugdevelopment supported by We Heart This
John James Jacoby
John James Jacoby
• educate the customer about community role and open-source
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
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
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
Tips
Tips
• bring up open-sourcing early
Tips
• bring up open-sourcing early• if they say no, respect their choice
Tips
• bring up open-sourcing early• if they say no, respect their choice• don’t pass the cost back to the client
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
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
Abstraction
Abstraction
FTW!
More FTW = less WTF
The bottom line
• Abstract your code:
The bottom line
• Abstract your code:• Client gets modular, future-proof, understandable code.
The bottom line
• Abstract your code:• Client gets modular, future-proof, understandable code.• Less headaches for you and your team.
The bottom line
• Abstract your code:• Client gets modular, future-proof, understandable code.• Less headaches for you and your team.• Open-source your customizations:
The bottom line
• 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
• 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
• 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!
Thank you!Questions?
Slides will be up on mitcho.com/blog/.
mitcho (Michael 芳貴 Erlewine)mitcho.com; @mitchoyoshitaka