{"id":222,"date":"2016-08-07T01:01:29","date_gmt":"2016-08-07T00:01:29","guid":{"rendered":"http:\/\/blog.powered-up-games.com\/wordpress\/?p=222"},"modified":"2016-08-07T01:01:29","modified_gmt":"2016-08-07T00:01:29","slug":"css-styling-and-object-orientated-design","status":"publish","type":"post","link":"https:\/\/powered-up-games.com\/blog\/wordpress\/archives\/222","title":{"rendered":"CSS styling and object orientated design"},"content":{"rendered":"<p>Last week&#8217;s programming surgery was the most popular yet, with 5 attendees, and a diverse set of programming topics to discuss. I won&#8217;t attempt a detailed record of the conversations as my notes were too brief for that, but I will summarize a couple of interesting topics.<\/p>\n<p><!--more--><\/p>\n<p>The first was styling with CSS. Somewhat fortuitously I had just been working on some HTML on another project and so I had some of the rules freshly revised. My go to resource for this kind of thing is <a href=\"http:\/\/www.w3schools.com\/\">W3Schools<\/a>. They explain how to use HTML and CSS together and provide a live editor for trying out examples. This was particularly useful when working out the <a href=\"http:\/\/www.w3schools.com\/cssref\/sel_element_gt.asp\">element selection<\/a> feature.<\/p>\n<p>For example, to style a link only when it is the child of a &lt;div&gt; tag:<br \/>\n<code><\/p>\n<pre>\r\n&lt;div&gt;\r\n    &lt;a href=\"http:\/\/w3schools.com\/\"&gt;Example link&lt;\/a&gt;\r\n&lt;\/div&gt;\r\n<\/pre>\n<p><\/code><\/p>\n<p>You can use this CSS code:<br \/>\n<code><\/p>\n<pre>\r\ndiv &gt; a {\r\n    background-color: yellow;\r\n}\r\n<\/pre>\n<p><\/code><\/p>\n<p>The &gt; symbol says that this rule applies only to the tag on the right when it is nested inside the tag on the left.<\/p>\n<p>Another good resource for both learning and tinkering with HTML is <a href=\"https:\/\/trinket.io\/\">Trinket<\/a>. We use Trinket a lot with the code club because it allows you build examples using multiple files and <a href=\"https:\/\/trinket.io\/html\/686b6567ca\">share them<\/a>.<\/p>\n<p>My limited experience with HTML and CSS might get me through simple composing and styling issues. But for more professional help or advice in Croydon, I recommend contacting <a href=\"http:\/\/sleeptwitch.com\/\">Sleeptwitch<\/a>.<\/p>\n<p>The second topic I found interesting was a more general request for help on breaking up a growing body of purely procedural code into separate classes and objects. An idea I like to keep in mind when performing this kind of operation is <a href=\"http:\/\/www.ccs.neu.edu\/research\/demeter\/related-work\/pragmatic-programmer\/jan_03_enbug.pdf\"><em>Tell Don&#8217;t Ask<\/em><\/a>; which is an informal version of the <a href=\"http:\/\/www.ccs.neu.edu\/research\/demeter\/papers\/law-of-demeter\/oopsla88-law-of-demeter.pdf\">Law of Demeter<\/a>. In a nutshell, the idea is to ask the objects in your program to perform tasks for you rather than micro-managing them yourself.<\/p>\n<p>Here is a quick RPG style video game example. Imagine we have an object <em>Player<\/em> that we want to damage another object <em>Monster<\/em>. A purely procedural approach might be to access the internals of both classes:<\/p>\n<p><code><\/p>\n<pre>\r\nmonster.hitPoints -= player.attackDamage;\r\n<\/pre>\n<p><\/code><\/p>\n<p>The <em>Tell Don&#8217;t Ask<\/em> approach might instead instruct the <em>Player<\/em> object to do the work:<\/p>\n<p><code><\/p>\n<pre>\r\nplayer.damage(monster);\r\n<\/pre>\n<p><\/code><\/p>\n<p>Internally, the player would then forward the instruction to the monster:<br \/>\n<code><\/p>\n<pre>\r\nvoid Player::damage(Creature target)\r\n{\r\n    target.takeDamage(attackDamage);\r\n}\r\n<\/pre>\n<p><\/code><\/p>\n<p>And the monster would need to respond to that:<br \/>\n<code><\/p>\n<pre>\r\nvoid Monster::takeDamage(int damage)\r\n{\r\n    hitPoints -= damage;\r\n}\r\n<\/pre>\n<p><\/code><\/p>\n<p>Initially it seems overly complicated in examples like this, but the advantages soon begin to outweigh the disadvantages as your project scales up in size. There is also a disadvantage in that your game&#8217;s data begins to get fragmented. In simulations with large numbers of objects this can lead into performance issues, but that&#8217;s a topic for another time.<\/p>\n<p>Some good resources for turning procedural code into object oriented code are <a href=\"http:\/\/refactoring.com\/\"><em>Refactoring<\/em><\/a> by Martin Fowler and <a href=\"https:\/\/en.wikipedia.org\/wiki\/Design_Patterns\"><em>Design Patterns: Elements of Reusable Object-Oriented Software<\/em><\/a> by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, also known as the <em>Gang of Four<\/em>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last week&#8217;s programming surgery was the most popular yet, with 5 attendees, and a diverse set of programming topics to discuss. I won&#8217;t attempt a detailed record of the conversations as my notes were too brief for that, but I will summarize a couple of interesting topics.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,6,5],"tags":[58,60,59,39,61],"class_list":["post-222","post","type-post","status-publish","format-standard","hentry","category-internet","category-programming","category-video-games","tag-css","tag-lod","tag-oo","tag-programming","tag-telldontask"],"_links":{"self":[{"href":"https:\/\/powered-up-games.com\/blog\/wordpress\/wp-json\/wp\/v2\/posts\/222","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/powered-up-games.com\/blog\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/powered-up-games.com\/blog\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/powered-up-games.com\/blog\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/powered-up-games.com\/blog\/wordpress\/wp-json\/wp\/v2\/comments?post=222"}],"version-history":[{"count":13,"href":"https:\/\/powered-up-games.com\/blog\/wordpress\/wp-json\/wp\/v2\/posts\/222\/revisions"}],"predecessor-version":[{"id":235,"href":"https:\/\/powered-up-games.com\/blog\/wordpress\/wp-json\/wp\/v2\/posts\/222\/revisions\/235"}],"wp:attachment":[{"href":"https:\/\/powered-up-games.com\/blog\/wordpress\/wp-json\/wp\/v2\/media?parent=222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/powered-up-games.com\/blog\/wordpress\/wp-json\/wp\/v2\/categories?post=222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/powered-up-games.com\/blog\/wordpress\/wp-json\/wp\/v2\/tags?post=222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}