Abstraction
Lab: Building Blocks of Programming
Video Runtime: 11:45
You need to embrace abstraction in your programming techniques. Abstraction is one of those big scary words. It removes and hides complexity, allows you to build code in a modular, purposeful fashion, and code to only what is needed.
Your key takeaways are:
- Abstraction promotes quality code
- Skinny, purposeful code
- Promotes reuse and modularity
- Hides complexity
- Improves maintainability
Study Notes
What is abstraction?
- Purposeful methodology – everything in your code should have a specific purpose and function. Everything in that code should support only that task. Being purposeful is an earmark of quality coding.
- Remove or hide complexity – you only deal with what’s important to you, and hide away the finer details in a “black box.” If you want to know what’s in the black box, you can always go look inside.
- Code to just what is needed – when you get into your car and turn the key, you don’t need to know how the starter, ignition, and other components of the engine work in order to start the car.
This is an example of abstraction. You only need to know how to interface with the car:
- Insert the key
- Turn the key
Hopefully, your car starts. You didn’t have to tell the engine how to operate, or know the steps it goes through. You know how to interface with the car and operate the key. The car handles the finer details of the functions involved in firing up the engine and other systems.
Subroutines
Often we see very long functions or big files containing unneeded complexity, rather than breaking things up and having each function perform only its single purpose.
Objects are one way to break things up and abstract away complexity. Another way to is to use subroutines. Without subroutines, everything would be in one giant file.
Can you imagine if all of WordPress core code existed only in one file?
Subroutines promote reuse. If you can reuse something, you can create modules, and modularity is another indication of quality code. Once coded, these modules can be used over and over again. Not just in your current project, but also in any other projects.
Quality Code
- Skinny and lean – if your code has a lot of lines in it, you can probably refactor it and abstract away some of the functionality
- Purposeful – single responsibility
- Modular – you can take it and move it
- Resuable – helps get rid of redundancies
- Improves maintainability – it’s easier to look at the code and know right away what it’s doing
Abstraction in Action
function _qa() { | |
$('.qa-question').on('click', function() { | |
var $answer = $(this).next('.qa-answer'); | |
if ( $(this).hasClass('opened') ) { | |
_closeQA( $answer, $(this) ); | |
} else { | |
_openQA( $answer, $(this) ); | |
} | |
$answer = null; | |
}); | |
} | |
function _openQA( $answer, $container ) { | |
if ( $answer.hasClass('closed') ) { | |
$answer.removeClass('closed'); | |
} | |
$container.addClass('opened'); | |
$answer.slideDown(); | |
} |
Whoever says that coding is hard, just smack them.
Episodes
Total Lab Runtime: 02:17:55
- 1 Lab Introductionfree 06:21
- 2 User-Friendly Expression of Informationfree 22:42
- 3 Fundamentals of Syntaxfree 08:57
- 4 Decision Branching - if/then/elsefree 10:39
- 5 Decision Branching – switch/casefree 07:47
- 6 Basics of Iterationfree 14:14
- 7 Iteration in Practicefree 12:51
- 8 Order of Executionfree 10:55
- 9 Abstractionfree 11:45
- 10 Scoping – Who Can Communicate With Whomfree 31:44