3 posts tagged with lesson
joomla development getting started

May 10, 2014
Joomla Development Tutorial Series

Introduction to Joomla Development. This Joomla development tutorial short video serves only to introduce the course and the material to be covered in future videos. We’ll start easy.

Below is the initial video for a series I’ve begun on Joomla CMS third party development. Depending on the interest I receive I’m going to continue demonstrating how to develop code for the Joomla CMS. I’ve decided to start with plugins, then modules, and finally a component.

I can’t stress enough, if you like these – I need to know. Otherwise I will assume everyone is already an expert and there’s no need to continue the series!

This video is very short and only serves as course introduction.

Joomla Development 101: Introduction

Joomla Development 101: Lesson 1 from dbhurley on Vimeo.

Associated Slides

April 25, 2014
Outside the Box

image

I’m sitting at my desk after pulling another all-nighter working on a last-minute deadline for a project. And before you ask let me tell you, I don’t like it. 

Late Nights

I’m not opposed to hard work, in fact, many would say I work a bit too much. And I love working late at night and early (wicked early) in the morning. There’s something relaxing and exciting about being up when the rest of the house is dark and quiet. It’s peaceful…well, it’s peaceful if there’s not a last minute deadline that you’re cramming to finish your work for. 

As I was wracking my brain with a problem I couldn’t seem to solve and had spent several hours working on it I realized something. It’s not about working harder, but working smarter. I know, I know it’s a very well-known saying and most of the time I do a great job of getting the right amount of sleep and thinking before writing, but sometimes I don’t realize until it’s too late. So what exactly does it mean to work smarter? 

Working smarter means being willing to think outside the box.

When presented with a problem don’t blindly accept a given solution; especially if you find yourself spending hours and hours running in circles without a getting the work done. Thinking outside the box means finding alternative solutions to the problem. If something’s not working and you’ve exhausted your resources, take a minute (doesn’t have to be long), take a deep breath, and take a fresh look at the problem. Look for alternate solutions which you’ve not explored. It may mean a short term expenditure of time, but long-term you could see massive time savings. 

Working smarter also means prioritizing tasks.

Don’t get stuck fighting with a minor problem when you could make great strides forward by focusing on a different part of the job. When you pick your tasks correctly you’ll find you personally feel better because you’ll make progress. Yes, it’s a mind game you’re playing with yourself. But it’s ok, the result is positive. I know it helps me to make progress on things in any form. So moving to a different task which I can quickly complete helps keep me motivated and energized.

Working smarter means sharing responsibility.

Don’t do it all yourself. If you’re anything like me then you struggle constantly with the feeling that you should be the one doing the work because you know it best. But trust me, from personal experience I’ve learned a couple of things. First, I don’t know best all the time. Second, you should take advantage of the skills of others and allow them to succeed at those things they know how to do. Many hands make light work and working as team will accomplish much more than you could ever get done by yourself.

So, where does this article come from? It comes from a long night of hard work where I look back and start to wonder if I’ve been working harder or working smarter. I’m pretty sure this time around I’ve worked too hard and not smart enough. I spent some time doing my research today and have found better ways I could have worked. So this is a lecture to myself as much as it is advice for anyone else. I’ll leave you this time with a quote from a somewhat funny old television show quote.

I’m pullin’ for ya. We’re all in this together.
Red, The RedGreen Show

April 2, 2014
Lessons in Coding: Don’t Repeat Yourself

image

I’m a parent. That tends to sound a bit like a confession. But most people who know me also know that I love kids. I have three children right now, and hope to add more in the near future.

I’ve learned many things from being a parent. I learned there is a strange phenomenon with children where they cannot hear something they are told unless I tell them multiple times. It’s interesting because it doesn’t seem to be all the time, only certain times, and usually only those times when they need to do something they don’t particularly want to do. And I really get tired of repeating myself. It gets quite annoying after a while.

Ok, so now everyone’s wondering, what does this have to do with coding and best development practices. Today I want to talk about the principle of Don’t Repeat Yourself.

Definition

Wikipedia defines the Don’t Repeat Yourself Principle as follows:

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

Let’s take that definition and break it down into some easy-to-understand pieces. And then let’s apply it to code.

Every piece of knowledge: In development that could be a class, a function, or merely even a block of code within a function.
single, unambiguous, authoritative representation: code should exist only once and it should be clearly written.
within a system: inside your component, module, plugin, application etc.

If we take those pieces and re-write the definition in our new terms it would look more like this:

Any code written and used within your component should exist in only one location and be clearly written.

That seems to make more sense. Now that we have a good working definition of what is involved in the principle of Don’t Repeat Yourself let’s look at some examples of how you would do this within your code.

Example of Bad Code

function getSingleItem($id) 
{
   // assume all variables needed
   $query = $this->db->getQuery(TRUE)
            ->select($this->db->quoteName(array('i.*', 'c.name', 'a.something')))
            ->from($this->db->quoteName('#__myitems', 'i'))
            ->join('LEFT', $this->db->quoteName('#__categories', 'c') . ' ON (' . 
              $this->db->quoteName('i.category_id') . ' = ' . $this->db->quoteName('c.category_id') . ')')
            ->join('LEFT', $this->db->quoteName('#__attributes', 'a') . ' ON (' .
              $this->db->quoteName('a.item_id') . ' = ' . $this->db->quoteName('i.item_id') . ')')
            ->where($this->db->quoteName('i.item_id') . ' = '. (int) $id);
   $this->db->setQuery($query);
   $item = $this->db->loadObject();
}

function getManyItems($ids) 
{ 
   // assume all variables needed 
   $query = $this->db->getQuery(TRUE) 
            ->select($this->db->quoteName(array('i.*', 'c.name', 'a.something'))) 
            ->from($this->db->quoteName('#myitems', 'i')) 
            ->join('LEFT', $this->db->quoteName('#categories', 'c') . ' ON (' . $this->db->quoteName('i.category_id') . ' = ' . 
              $this->db->quoteName('c.category_id') . ')') 
            ->join('LEFT', $this->db->quoteName('#__attributes', 'a') . ' ON (' . $this->db->quoteName('a.item_id') . ' = ' . 
              $this->db->quoteName('i.item_id') . ')') 
            ->where($this->db->quoteName('i.item_id') . ' IN '. implode(',', $ids)); 
    $this->db->setQuery($query); 
    $item = $this->db->loadObject(); 
}

Does that code look similar? It should. I created the second function by copying the first function and then changing a single line. (Hint: Look at the “where” clause). So what would this code look like if I re-wrote it but did so without duplicating any lines of code? Here’s one possible example.

Example of Better Code

function getSingleItem($id) 
{ 
   // assume all variables needed 
   $query = $this->getQuery(); 
   $query->where($this->db->quoteName('i.item_id') . ' = '. (int) $id); 
   $this->db->setQuery($query); 
   $item = $this->db->loadObject(); 
}

function getManyItems($ids) 
{ 
   // assume all variables needed 
   $query = $this->getQuery(); 
   $query->where($this->db->quoteName('i.item_id') . ' IN '. implode(',', $ids));
   $this->db->setQuery($query);
   $item = $this->db->loadObject();
}

function getQuery() 
{ 
   $query = $this->db->getQuery(TRUE) 
            ->select($this->db->quoteName(array('i.*', 'c.name', 'a.something'))) 
            ->from($this->db->quoteName('#myitems', 'i')) 
            ->join('LEFT', $this->db->quoteName('#categories', 'c') . ' ON (' . $this->db->quoteName('i.category_id') . ' = ' . 
              $this->db->quoteName('c.category_id') . ')') 
            ->join('LEFT', $this->db->quoteName('#__attributes', 'a') . ' ON (' . $this->db->quoteName('a.item_id') . ' = ' . 
              $this->db->quoteName('i.item_id') . ')');
   
   return $query;
}

Instead of re-writing the query function twice, once inside of each function, I have extracted the repeated code and written it as a third function which I can now call from either of the two other functions. It seems simple. In fact, many developers are probably already implementing this type of coding to make their code better.

Benefits

Why is it important to not repeat yourself? There are many benefits to writing your code only once. Sure, nobody like to waste time writing code over and over again, but even more importantly you reduce the chance for bugs to occur. Less code means less chance of failure points. I am sure we’ve all had those moments when we can’t find the problem until after wasting an hour or more looking you find a missing semi-colon. I know it’s happened to me. There are other benefits too. When you later realize you need to add another table on to the query because your client needs more information from the database you only need to update one query now!

I hope many developers are already aware of this principle of Don’t Repeat Yourself. If this is a new concept, congratulations you have now learned what it means to write DRY code. I wanted to start this series with an easy topic and though this doesn’t cover every technical detail of this principle it does focus on a strong aspect of it. In the coming releases I look forward to looking at more principles of good development.