Object oriented coding can sometimes be hard to understand. When working with other people I usually use this metaphor to explain oocss.
We want to create a list of books, that sit on a shelf within a library. As html it would look something like this:
The wrong way
The common (but very wrong) way is to apply specific class names to each level, combining structural and visual styles together under a single class e.g.
The object oriented way
Separate structure/layout from style/theme. So in our example that would be:
Layout/Structure:
1) library - the bricks and mortar of the building
2) shelves - the shelves attached with hinges to the brick, x amount
3) book - the items containers with content inside them, x amount
Style/Theme:
1) library - The decoration on the walls, how it looks and is painted
2) shelves - the wood effect, shadows borders, spacing
3) books - whether the books are big blue hardbacks, or small red paperbacks
Layout
We can identify the common structural elements and give them their own name-spaced class:
So how would our html look with the structural css class names?
Theme
Then identify the common visual/styling elements and give them their own name-space class:
Now how would our html look with these style/theme classes?
Now we can reuse our classes and even move the theme around to theme just a single row if we want or even add a second theme for that row of items:
To see a real-world example check out my simple responsive template on github:
https://github.com/kmturley/alley
We want to create a list of books, that sit on a shelf within a library. As html it would look something like this:
The wrong way
The common (but very wrong) way is to apply specific class names to each level, combining structural and visual styles together under a single class e.g.
Separate structure/layout from style/theme. So in our example that would be:
Layout/Structure:
1) library - the bricks and mortar of the building
2) shelves - the shelves attached with hinges to the brick, x amount
3) book - the items containers with content inside them, x amount
Style/Theme:
1) library - The decoration on the walls, how it looks and is painted
2) shelves - the wood effect, shadows borders, spacing
3) books - whether the books are big blue hardbacks, or small red paperbacks
Layout
We can identify the common structural elements and give them their own name-spaced class:
So how would our html look with the structural css class names?
Theme
Then identify the common visual/styling elements and give them their own name-space class:
Now we can reuse our classes and even move the theme around to theme just a single row if we want or even add a second theme for that row of items:
To see a real-world example check out my simple responsive template on github:
https://github.com/kmturley/alley
No comments:
Post a Comment