Thursday, March 21, 2013

Jeremy's Programming Blog - 2 new articles

JeremyMorgan.com Newsletter

 
Sent to Insider Member blogs.sherrylinx.8442@blogger.com



Jeremy's Programming Blog - 2 new articles

The Single Responsibility Principle

There’s a lot of genius in simplicity. This applies to many things in life and definitely in software development. SRP, or Single Responsibility Principle is one of the foundations building better software. It’s a good way to keep code working as expected, and to avoid painting yourself in a corner.

Do one thing and do it well

The Single Responsibility Principle guides simplicity, and the rule itself is strikingly simple: There should never be more than one reason for a class to change. This is easier said than done of course, but the idea is if you have more than one reason to change a class, then you should break it into two classes.

This goes for methods as well, as sometimes folks tend to cram a bunch of functions into a method. The problem with that is, when part of that method changes drastically, you may have to modify the signature or output of the method. Overloading can help there, but it’s still better to separate tasks as much as possible. Creating another class or method doesn’t cost a thing.

I know OOP design usually dictates getting as much done in an object as you can, but this can lead to growing pains down the road. This is where extending and inheritance can really come in handy.

A SRP Violation

Ok one of the biggest violators I have seen is mixing presentation with data. With the MVC movement in full swing you don’t see as much of it anymore, but it still happens. Take the following class from a blog app:

1  2  3  4  5  6  7  8  9  10  11  12  
class Page {      public function drawIndex() {        $this->getArticle($id);        // some code to output HTML    }        private function getArticle($id){        // query the database for the latest article    }    }

Why is this bad? It looks pretty simple, and it’s pretty easy to use. The reason it’s bad is because it does two separate things, it gets and article from the database, then it outputs html. This is not only a violation of the Single Responsibility Principle but Separation of Concerns as well.

What happens when you change databases? This happens all the time. You have now made drastic changes to the getArticle function, and maybe even the output that comes from the database. This is bad design to have this coupled with your presentation code and even puts it at risk. Plus, let’s say you want to display the output to JSON as well. Where would that method go? Lumping it all together creates a huge mess later down the road.

Not to mention the fact that your designers and your DBA might both be dipping into the same class… is that something you really want?

The Solution

1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  
class Page {      public function drawIndex() {        $data = $model->getArticle($id);        // functions to output HTML    }  }    class Model {      public function getArticle($id){        // query the database for the latest article            return $data;    }    }

Once you break it into two classes you create a Separation of Concerns. The Page class generates web pages, and the Model gets data from your database. The page doesn’t care what the model is doing as long as it takes in data (the article id) and outputs data (the article) as expected. You can change the model to connect to any data source you want, and the Page class doesn’t care.

And the Page class is now free to output data any way it wants. If you want to create some html, you can create a method to do that, output some JSON, XML whatever, with the same data from the model. By separating them you give yourself a lot more freedom down the road to change and modify.

Just remember, if there is more than one reason to change, split it up.

When to implement the Single Responsibility Principle

In theory you want to do this from the very beginning. As you’re designing your application you should always keep this in mind. In practice it’s not always easy and sometimes you just have to get your project finished. This is where refactoring comes in.

You should always go back and refactor your code at some point because business rules change, requirements change and things get added on and subtracted. Good code refactoring can really improve your development process. Remember, you’re optimizing for humans here, and the easier it is for you or another programmer to make changes the better off you’ll be down the road.

For more information about the Single Responsibility Principle, check out Principles of Object Oriented Analysis and Design by James Martin.



How to Watch Netflix in Ubuntu Linux

One of the biggest hurdles to Linux adoption is getting stuff to work in Linux that works great in Windows. Microsoft has enjoyed a huge market share over the years, and because of that when companies develop commercial software they target Windows first, and maybe OSX. Linux is either an option for companies with a geekier application, or their product is either emulated or ported by dedicated Linux hackers. What this does is ensure you pretty much need a Windows box somewhere for some task. But this changes every day.

Netflix in Linux - A long wait

Netflix is something that Linux hackers have wanted for years. Netflix is written in Silverlight / .Net and it looks like that isn’t going to change anytime soon. The frequent requests for a Linux Native Netflix client have been ignored, as there would be a serious expenditure for them to build a different streaming system, and they’d have the problem of supporting two.

So the solution most used was to create a Virtual machine with a Windows and watch it that way. Not the best option, but at least a working one. Then along comes someone like Erich E. Hoover, Ph.D to create a more elegant and easier solution. That’s the solution I’ll be outlining here. It’s super easy, and doesn’t take as much horsepower as a dedicated Windows virtual machine either.

How to Install The Tools to Watch Netflix in Linux

Note that this is by no means an official Netflix solution, nor is it endorsed or encouraged by them. I’m using Ubuntu 13.04 Raring Ringtail for this installation, but it works in many Ubuntu versions and variants.

First, update your system:

  sudo apt-get update  

Then, add the repository:

  sudo apt-add-repository ppa:ehoover/compholio  

Now run the following:

  sudo apt-get update && sudo apt-get install netflix-desktop  

This may take a while, especially if you’re on a slow connection. You’ll see the following screen:

Agree to it (if you want to install this). After it installs a pile of stuff, you’ll see the Netflix icon on your start bar under “Sound and Video” (I’m using LXDE but it’s pretty easy to find with other Window managers).

The first time you run it, will get a window asking you to install Mono:

Then it will need to install Gecko (Firefox for watching)

Then you’re ready to go. You may get a few errors that pop up here and there but it will still work.

The Final Result

That’s it! This is all there is to installing Netflix in Linux.

My only complaint so far is not being able to put it into smaller window, but I’ll figure out how to do that and update this article soon. I noticed the Video quality and Sound are the same as they are in Windows, and you can go full screen and it works great! Thanks Erich for creating this awesome solution.


Do you want to be notified every time a new tutorial is posted?

If you want to see more tutorials like these subscribe to this feed and get them instantly in your inbox or feed reader. If you want to get all my updates, subscribe to the main feed here.


More Recent Articles

 

 

Thank you for subscribing to my updates! 

Follow Jeremy on Facebook | Twitter | Linkedin

 




Your requested content delivery powered by FeedBlitz, LLC, 9 Thoreau Way, Sudbury, MA 01776, USA. +1.978.776.9498

 

No comments:

Post a Comment