Ideas


Development and EPICS and Howto and Ideas and Opinion19 Mar 2008 09:45 am

As the Active Record class has become increasingly popular due to it’s wild success in Ruby on Rails, it seems as though every framework has an implementation of it. It is no surprise that inside the covers of the MyEPICS framework lives an active record class that I had created. This class has evolved and changed over time, which I am going to share my experiences writing it. It is far from finished, but it has been quite a great learning experience along the way.

2.0 Implementation

The MyEPICS 2.0 implementation contained one master database class (ME_DB) which contained generic CRUD functions, and each table contained its own class which would implement the specific CRUD functions for each table (ME_DBO). A sample way to read users with the name ‘bob’ would be the following:

$user=new ME_DBO_User();
$user->read(array('name'=>'bob'));

And to update a person’s last name to “foobar”, you would do the following:

$user->lastname="foobar";
$user->update();

About 90% of each of the ME_DBO classes were the same code, and there was even a script to create a DBO class given a SQL create statement. A major problem with this approach is that for every new table created, you needed to make sure you created a new ME_DBO file, and that the file really only contained such things as which columns were in the table and their default values. We would have about 2k lines of code between 30 files, of which only about 50 lines were different… YUCK! Over Christmas break, I decided that this was a major, major issue that needed to be worked out before MyEPICS 2.1.

MyEPICS 2.1 Implementation

With the 2.1 implementation, I took a step back and attempted to create two classes which would fix the pitfalls of the 2.0 implementation. What I ended up doing was using MBD2’s reverse engineering module in order to find out which fields were available and what their default values were. This reduced the need for a separate file for each table and removed about 1500 lines of code from the Active Record implementation. Now, instead of

$user=new ME_DBO_User();

You would do this instead

$user=ME_Db::factory('User');

This has the main database create a Dbo object with just the right properties that you need.

The only major gripe I have with the current implementation is that I don’t implement anything for relationships. If you need information from a relationship, then you have three options.

  1. Cry
  2. Write Manual SQL
  3. Write many lines of code which does a lot of extra sql queries and is inefficient and crappy

I mainly want to create something so that the relationship table is transparent. A lot of the time, your relationship table doesn’t have anything of value in it, and it only exists for a many to many relationship.

SQL Command Functions

About

One such feature that I see in a lot of Active Record implementations is the ability to use functions named after SQL commands, such as:

$db=new Some_Active_Record_Class();
$db->select('firstname')->from('User')->where('id=5');

Mostly, I had disregarded this need in my Active Record implementation because it seemed like it was a lot of work for very little gain. Why would you need to allow such functionality when creating a simple sql statement would do?

Well, I think I had just stumbled upon the reason for its need.

The Problem

I have a class for a user and contains information pertaining to the user such as first name, last name, e-mail address etc… It has some very useful functions such as getUserWithRole($role) and getPriviliges($user). What I need to be able to do, is extend this information through one of the MyEPICS modules. Such an example is in my Roommate Finder application, which extends the user to give them traits. The user module does not know about the Roommate Finder application, nor should it have to. Now, here’s the problem. What I need to be able to do, is return a result set of all the users’ traits, where the user has a certain role. I already have the code to return all users with a certain role in the Users module, now I just need to attach the trait information to the output of it.

I have come up with a few different solutions that would plug this hole, but not fix the problem, with most of them being some variation of typing up a single sql statement to get me the information. What I don’t want to do, is getUsersWithRole(), then iterate through all of theses picking out the traits for each individual user. This would result in at least one sql statement per user, which is highly innefficient.

I want to be able to execute this functionality in one sql query, without having to tightly couple the User module with the Roommate Finder module, and without duplicating functionality already existing in the User module.

A Solution?

What I have been thinking about that would solve this problem would end up having me creating something like the database command functions. Then, what the getUserWithRoel() function would do, is instead of returning the results, it would return a Dbo object with the sql already added. So that if getUsersWithRole() would normally result in some query like “select role from roles where role=?”, then it would now return an object that represents this statement. What this would allow me to do is chain these functions together, then only execute the query at the end. For example:

//fetch results in an array
$dbo=$userModule->withRole('Tenant')->join('Traits')->fetchArray();

This has several features which I haven’t implemented. Easy relationships, chaining of functions, and the sql commands.

Is it bad that I am never satisfied with what I already have, and always want to refactor to make it better?

Ideas17 Mar 2008 12:03 pm

Currently, I have at least 7 e-mail accounts, of which, I regularly use three. There are plenty of desktop e-mail clients that will allow me to use IMAP to combine all of these applications into a single e-mail inbox, but why isn’t there a service that allows me to have all of this “in the cloud”. I hate it when I am at a different computer other than my own and have to install my mail software and set up 7 different e-mail accounts, or visit 7 different pages in order to get my e-mail. Why isn’t there a web 2.0 e-mail client? I would even pay a small monthly fee in order to use this service!

Ideas04 Jan 2008 08:31 am

I have been reading Code Complete (which I would highly recommend to any software developer) and came across an entry of refactoring. One of the tips for refactoring involves A program contains code that seems like it might be needed someday. It talks about how designing ahead (and developing ahead) w/o rigorously testing using code that is written is a bad idea for a few different reasons that were listed, among which is that you or someone else will use the methods and only realize that they aren’t implemented, or are poorly implemented. I do agree that scaffolding methods, creating methods w/o testing or really using them at all, is a bad idea, but is sometimes extremely helpful. It can allow a lead developer to outline what he or she is intending to do, and what vision they have for the future of said classes. It allows others to see these right away, but at the cost of having to know just as much about what is or is not implemented.

What I would like to see in an IDE is some sort of software health rating and monitor. Allow you to assign some value to a piece of code, let it be a class, method or property, and from there, you can view the “health” of your code. Imagine scaffolding out a class and being able to give it a “low health” rating. This information can be shared with other developers on the project (even versioned!), so that they may know what parts are well developed, and what parts may not be so well developed. It can allow developers to show “danger area’s” where code may radically change, and “stable” area’s, where code has been tested and is safe to heavily use. This is information that a single developer holds in their head as they are developing, but is hard to convey when other developers get involved. This way, you could be able to see at a glance which code is well tested, and which is not. If you use some piece of untested code, then your code gets the same label.

I believe that with tools like this, it takes an IDE well beyond a single text editor and provides many, many more benefits over just plain vi or emacs. In fact, the extremely poor quality of IDE’s is one topic that I would love to touch on, but is outside the realm of my post today. Maybe someday I will have the time to work on such a feature as a plugin to some unknown IDE that I might bet passionate about someday.

Ideas27 Dec 2007 04:24 pm

I was thinking about it today, and why isn’t there (or is there?) a gadget that uses wifi to broadcast a TV signal, and another companion device that receives the signal and outputs it through a coax or RCA cable? I know that there exist the Slingbox, but that’s not exactly what I’m looking for. Sometimes, you don’t have the TV right next to the cable outlet, and you don’t want to either rewire the cable or you don’t want the cable going all the way across the room / house. I’m thinking this would be the perfect device for a college town, because often they don’t have cable hookups in every room, and you could hook this up to the one hookup, then have all of the other devices tap into the one source.

Ideas26 Dec 2007 02:16 am

So I’ve been kicking this idea around in my head, and decided that I would write about it.

I was thinking about how cool it would be to have a stock market of internet meme’s that is driven by youtube. What you would do, is pick a video “stock” and if it got more successful, you could win money (or just points) and if it went less successful, less money/points. Success could be measured by hits/day, links, or any other random fact. If you start a meme, it could really make you rich / have a lot of points. In order to get the most functionality, you would need to operate your own site, such as break.com, youtube.com, or any other random dump for internet video’s. Obviously there would be a lot of security issues such as ballot stuffing and such, but still, I think it would be a fun game to play. Maybe have a leader board of the top brokers in the game.

You could make money by charging for trades, putting a small commercial before the most popular video’s, or just simple ads on the site.

Has this been done before or thought about? Or am I just a crazy nut with a terrible idea?

Buy fake rolex watch for sale replica watches replica rolex womens. Quality furniture furniture baby furniture.