Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Saturday, 15 October 2011

Play with Cakephp's find query


CakePHP allows you to use your own "find-types" for the Model::find() methodology. Those of your who are familiar with the find() method know that there are currently four types in the core: 'list', 'all', 'first' and 'count'. However, sometimes it is nice to specify your own type so you can have a call like this:
$this->Comment->find('pending');
So how would you go about implementing this? Correct, you would overwrite the find() method in your model and provide the default functionality if there is one of the default types used. Let's have a look at how you could implement this pending find from above:
class Comment extends AppModel {
  var $belongsTo = array('User');

  function find($type, $queryData = array()) {
    switch ($type) {
      case 'pending':
        return $this->find('all', array(
          'fields' => array('User.email', 'User.first_name', 'User.last_name', 'Comment.name')
          , 'conditions' => array(
            'Comment.active' => 0
            , 'Comment.blocked' => 0
          )
          , 'contain' => array('User')
        ));
      default:
        return parent::find($type, $queryData);
    }
  }
}
So this is actually not very difficult code, but it is very powerful. You could specify different sets of conditions, fields, orders, groups by's and containments just by adding a new entry to the switch statement. Please note that there is no break needed within the switch as we return home.
Let's add some code for finding pending users that were invited by the currently logged-in user. This is what we could use in the controller:
$this->Comment->find('pending', array('created_by_id' => User::get('id')));
Now for the model we would need to add some code that tracks if $queryData['conditions'] is set, and use that as well in conjunction with the conditions specified in our find type. While we are at it, let's also add some code that would handle fields, order, group, recursive and contain statements:
class Comment extends AppModel {
  var $belongsTo = array('User');

  function find($type, $queryData = array()) {
    $defaults = array(
      'conditions' => null
      , 'fields' => array()
      , 'order' => null
      , 'recursive' => null
      , 'contain' => null
    );
    $queryData = am($defaults, $queryData);

    switch ($type) {
      case 'pending':
        return $this->find('all', array(
          'fields' => am(
            array('User.email', 'User.first_name', 'User.last_name', 'Comment.name')
            , $queryData['fields']
          )
          , 'conditions' => am(array(
            'Comment.active' => 0
            , 'Comment.blocked' => 0
          ), $queryData['conditions'])
          , 'contain' => am(array('User'), $queryData['contain'])
        ));
      default:
        return parent::find($type, $queryData);
    }
  }
}
With some default values we can happily use our custom find type alongside dynamic conditions inserted by our controllers. If you think this am() stuff is overkill, you could as well just provide another find type:
$this->Comment->find('pending-created-by-logged-in-user');
class Comment extends AppModel {
  var $belongsTo = array('User');

  function find($type, $queryData = array()) {
    switch ($type) {
      case 'pending':
        return $this->find('all', array(
          'fields' => array('User.email', 'User.first_name', 'User.last_name', 'Comment.name')
          , 'conditions' => array(
            'Comment.active' => 0
            , 'Comment.blocked' => 0
          )
          , 'contain' => array('User')
        ));
      case 'pending-created-by-logged-in-user':
        return $this->find('all', array(
          'fields' => array('User.email', 'User.first_name', 'User.last_name')
          , 'conditions' => array(
            'Comment.active' => 0
            , 'Comment.blocked' => 0
            , 'Comment.created_by_id' => User::get('id')
          )
          , 'contain' => array('User')
        ));
      default:
        return parent::find($type, $queryData);
    }
  }
}
... well you get the idea. By the way, do you notice how useful using a static functions like this User::get() methods are for fetching properties and data from the currently logged in user? More on that later.

C creator Dennis Ritchie passes away



  It is with sad hearts that the Red Hat community mourns the passing of computing pioneer Dennis MacAlistair Ritchie. Dennis Ritchie was the principal designer of the C programming language and co-developer of the Unix operating system, working closely with Ken Thompson, his longtime Bell Labs collaborator.


Many of us have a proud history of involvement in the UNIX operating system well before the emergence of Linux. To the UNIX world, Ritchie and Thompson were as influential as Linus Torvalds is today to the Linux community. Unix and C's direct and spiritual descendants cannot be counted, but include Linux, Android, Mac OS, iOS, JavaScript, C++, the genius of the internet, and a planet of developers. The major impact of UNIX is not so much in the elegant code itself but rather in the culture of sharing work across industry and academia that became UNIX’s hallmark. Prior to UNIX, operating system code was locked in corporate vaults – inaccessible to the masses. UNIX flung open these doors by allowing code to be shared among software engineers across the nascent computer industry, ushering in an unprecedented wave of collaborative development – and, at the same time, liberating many applications from being locked into a single proprietary hardware vendor.
UNIX code was also shared with universities where it became the foundation of the learning and advancement of operating system practices. Similarly, the C programming language became a staple of the computer science classroom. Many of us literally grew up in Dennis' technical shadow and still have his book, The C Programming Language, co-authored with Brian Kernighan and more fondly referred to as K & R, on our shelves. It remains a source of inspiration and practical help to programmers to this day.
Most of what we do is heavily influenced by Dennis’s outstanding contributions – both in the technical arena and as a founder of the concept of community development. We at Red Hat look with awe and reverence on his legacy.

Thursday, 13 October 2011

Cloud will change the programming


The early preview of Dart, a new alternative to JavaScript for Web programming unveiled by Google earlier this week, is the latest entrant into the pantheon of programming languages. As such, it's hardly a rarity. There are hundreds of programming languages--perhaps thousands if experimental and academic variants are included. That said, the number of widely used languages is much smaller, numbering perhaps in the dozens, with fewer still broadly relevant to general-purpose server operations and Web software.
Changes in programming languages are more about evolution than revolution.
(Credit: CC Flickr user Lindsey Bieda)
In fact, what's so notable about the computer programming language landscape over time isn't so much its diversity and adaptability, but rather its inertia. COBOL and Fortran, the longtime standards for business and scientific programming respectively, remain in use albeit less widely so than at one time. Object-oriented programming, which encapsulates data together with the associated functions that operate on that data for more structured and maintainable code, came into initial widespread use largely through extending an existing language, C. C itself, originally designed as a language for programming systems at a very low level, was put into use for all sorts of application programming tasks for which it was arguably not very well-suited.
Does this change with cloud computing or, to be more precise, with an increased emphasis on browser-centric application access, big and unstructured data processing, and the development of a huge mobile ecosystem?
To some degree, it already has. Scripting languages, including JavaScript but also Perl, Python, PHP, Ruby, and others are children of the Web. Languages that lend themselves to writing code quickly with less of the stringent correctness checking common to traditional enterprise code-writing have become the norm in many Web environments. However, at the same time, more radical approaches to dealing with large-scale parallel operations on the server side haven't taken off in a big way. (Intel's James Reinders discussed the state of parallel programming with me two years ago in this interview; the picture hasn't changed in any big way.)
Public platform-as-a-service clouds introduce new possibilities to broaden the Web programming landscape. However, to the degree that an application programming interface (API) is limited to a single provider, moving an application elsewhere will require at least some porting. As a result, while we do see some providers offering APIs that are specific to a hosted environment, there's a strong argument for the flexibility of application portability across on-premise and a variety of hosted clouds.
The overall picture I see is one of change, but change that is mostly evolutionary and that doesn't involve a lot of radical overnight change from existing models.
As Al Gillen, program vice president for system software at market researcher IDC puts it in reference to a survey I helped put together for VMworld in San Francisco at the end of August: "thought it very revealing that "yesterday's'' frameworks were target for "tomorrow's" apps." He went on to write that "tools will evolve and utilize new programming frameworks, then use will evolve over time, not so revolutionary."