Code Poetry, it’s an Art

Gopal VTo my left is a kickass compiler poet named Gopal Vijay(shortened). I worked with Gopal when I just started to learn compilers and frameworks on the DotGNU Project. He understands what hard work is, as does Rhys Weatherly, the original author of Portable.NET. Both of them had mentored me to become a better programmer all around and not only understand what it means to do it correctly, always. After a couple years of working on DotGNU I moved on to constructive network protocol development which pushed me straight back into webservices where I stand today repeating the circle.

The reality is that we like to speak, walk, run around, and play with code. It’s our paintbrush, tinker toy, classical instrument, and pen. It can do things that people can take for granite or widen peoples’ eyes.

We all need to respect the programmer and give him/her a pat on the back. A raise might be nice too ;)

Yes, it’s true, we develop for ourselves, some more than others. If we want to build a god damn game engine for a calculator, by damn we will. You can’t stop us!

A toast for the digital poets!

Dynamic Scalar Type Hinting on PHP6

I was recently following a thread on the php-dev list that intrigued me enough to write about a new syntax feature for PHP.

Sam Barrow recently posted a patch about optional scalar value type hinting that got me thinking that it was a smart approach to making php even more structured, but using a dynamic approach, since PHP has given us flexibility, let’s keep moving forward with it.

I propose a Dynamic Scalar Type Hinting syntax where we can declare data-types and our boundary checking callbacks during runtime.

We always have to do boundary checking and type checking on our functions, even though we already know the type to be used in context.

  1. <?php
  2.  
  3. function bounds_check_uint($d) {
  4. if($d < 0 && $d <= 65535)
  5. throw new Exception(‘Value out of range’);
  6.  
  7. return true;
  8. }
  9.  
  10.  
  11. function mul_uint($a1, $a2) {
  12. try {
  13. bounds_check_uint($a1);
  14. bound_check_uint($a2);
  15. } catch($e) {
  16. echo $e->getMessage();
  17. }
  18.  
  19. return $a1 * $a2;
  20. }
  21.  
  22. function sub_uint($a1, $a2) {
  23. try {
  24. bounds_check_uint($a1);
  25. bound_check_uint($a2);
  26. } catch($e) {
  27. echo $e->getMessage();
  28. }
  29.  
  30. return $a1 - $a2;
  31. }
  32.  
  33. function add_uint($a1, $a2) {
  34. try {
  35. bounds_check_uint($a1);
  36. bound_check_uint($a2);
  37. } catch($e) {
  38. echo $e->getMessage();
  39. }
  40.  
  41. return $a1 + $a2;
  42. }
  43. ?>

Let’s propose the following syntax:

  1. <?php
  2.  
  3. function bounds_check_uint($d) {
  4. if($d < 0 && $d <= 65535)
  5. throw new Exception();
  6.  
  7. return true;
  8. }
  9.  
  10. define_scalar(‘uint’, ‘bounds_check_uint’);
  11.  
  12. function mul_uint(<uint> $a1, <uint> $a2) {
  13. return $a1 * $a2;
  14. }
  15.  
  16. function sub_uint(<uint> $a1, <uint> $a2) {
  17. return $a1 - $a2;
  18. }
  19. function add_uint(<uint> $a1, <uint> $a2) {
  20. return $a1 + $a2;
  21. }
  22.  
  23. ?>

Here it appears that passing a scalar type reference via ‘<' + literal + '>‘ syntax just left adjacent from your parameter you can set an inline and possibly an internal type check.

Some might say why not use Java? This is what non-dynamic based languages such as C++, Java, C#, etc. platforms of that caliber would bring to the table.

They require that you declare your type ahead of time and don’t give you the option to define and adjust during runtime without an obscure mannerism. Dynamic Scalar Type’s do not have to be defined at all or during runtime. They can also be used as inline type hinting for runtime based performance optimizations in the execution engine. They can be used by documentation tools to better generate phpdoc’s.

When all is said and done, I think this could be a fine addition to the PHP6 or possibly future versions beyond the platform. Thanks for listening and feel free to share your comments. As I continue to work on this patch to PHP6 CVS, I’ll need a sounding board.

Wordpress Syntax Highlighting - Phase 2

Since recent times have suggested to me that I better start putting a good collection of code online about how the things I create work, I need to start with a better syntax highlighter which is so important to explaining how things work in this world.

I started off using google’s system, but found it too cumbersome. I shot straight through three more plugins tonight and found they also have strange issues. This last one I’m working with today has found all the problems I have had and made it nice to use, besides one large issue with the Wordpress TinyMCE Editor.

The winner is
CodeViewer by elasticdog - http://elasticdog.com/2004/09/code-viewer/

Below is a Test Program - this is a C program and you download here to it:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void main() {
  5. return;
  6. }

I found that formatting is more important than the actual syntax highlighting. So what I’m doing is using CodeViewer for the display and will use a child extension for doing actual syntax highlighting.

GeSHi has given a wonderful attempt at writing a variable syntax highlighter. It works while in chunk-based highlighting too. GeSHi is a O(n) based parser for syntax highlighting, yet it works pretty quickly as it keeps its use of substr and preg_replace at a minimum where doing character based descent to speed up the system.

I’ve partially implemented GeSHi for line-based highlighting inside CodeViewer. You can download and compliment your wordpress installation by grabbing this tarball - Code Viewer with GeSHi Support.

To write a proper geshi supported tag, you must use the lang attribute.

  1. <viewcode src="cv_geshi_example.html" lang="html" />

If you would like to see the changes I’ve made to the plugin, please see this diff below:

  1. — code-viewer.txt     Mon Aug 29 07:08:37 2005
  2. +++ code-viewer.php     Sun Nov 18 09:33:15 2007
  3. @@ -9,14 +9,15 @@
  4. */
  5.  
  6. /* Configuration Settings */
  7. -$default_path = "http://elasticdog.com/code/";  // the absolute path of your code folder
  8. +$default_path = "http://{$_SERVER[‘HTTP_HOST’]}/code/";  // the absolute path of your code folder
  9.  
  10. /* --- STOP EDITING ---  */
  11. +require_once(‘geshi/geshi.php’);
  12.  
  13. function code_viewer($text) {
  14. global $default_path;
  15.  
  16. -       $count = preg_match_all(‘/<viewcode src="([^"]+)"(?: link="(?i:(yes|no))")?\s?\/>/’, $text, $matches);
  17. +       $count = preg_match_all(‘/<viewcode src="([^"]+)"(?: link="(?i:(yes|no))")?(?: lang="(?i:([a-z]+))")?\s?\/>/’, $text, $matches);
  18.  
  19. for ($i = 0; $i < $count; $i++) {
  20. // Determine if the specified path is absolute, or relative to the root path
  21. @@ -29,6 +30,9 @@
  22. $path = $default_path . $matches[1][$i];
  23. }
  24.  
  25. +                if(strtolower($matches[3][$i]) != "") {
  26. +                  $language = $matches[3][$i];
  27. +                }
  28. // Open the file
  29. // If the file can't be found, print an error message
  30. if ($lines = @file($path)) {
  31. @@ -44,8 +48,11 @@
  32. } else {
  33. $numtabs = strlen($line) - strlen(ltrim($line));  // determine the number of tabs
  34. $line = trim($line);                              // trim leading/trailing whitespace
  35. -
  36. -                                       $codelist .= "\t" . ‘<li class="tab’ . $numtabs . ‘ ‘ . $toggle . ‘"><code>’ . htmlspecialchars($line) . ‘</code></li>’ . "\n";
  37. +                                        if(isset($language)) {
  38. +                                          $codelist .= "\t" . ‘<li class="tab’ . $numtabs . ‘ ‘ . $toggle . ‘"><code>’ . geshi_highlight($line, $language, null, true) . ‘</code></li>’ . "\n";
  39. +                                        } else {
  40. +                                          $codelist .= "\t" . ‘<li class="tab’ . $numtabs . ‘ ‘ . $toggle . ‘"><code>’ . htmlspecialchars($line) . ‘</code></li>’ . "\n";
  41. +                                        }
  42. }
  43. }
  44.  
  45. @@ -76,4 +83,4 @@
  46.  
  47. add_filter(‘the_content’, ‘code_viewer’, 9);
  48. add_filter(‘the_content’, ‘fix_bad_p’);
  49. -?>
  50. \ No newline at end of file
  51. +?>

BestPartyEver.com Development

Hey Guys,

  It’s been quite a few weeks since I last posted.  I thought I would update you on my current tinker toys that I do during the day.  I’m a full time CTO/Developer as well as Partner with BestPartyEver.com.  After quite a bit of busy weeks developing we have launched our alpha version of the software and are going through a series of micro-releases.  It’s quite nice to work with a small, but agile team, creating a tremendous amount of growth in this application that has been needing an overhaul for quite a while.  We debuted a new Budget Calculator today that uses auto-saving and live calculations.  Numerous weeks went into making a new javascript library for the site that uses multi-level namespaces for organizing the code for re-use, speed, and compliance with third-party libraries and the silly global namespace issues with prototype, etc.   Some fun modern usability technologies were used to create better site navigation such as  the tag cloud on party ideas.  We can definitely see the popularity of halloween party ideas during this season.  We used geoip tracking to automatically detect the user’s region they are browsing from to target ad locations.  Leveraging an open source tech for this one, hostip.info, we transformed the data layer of hostip to fit snug in with our architecture and still be updated on a regular basis to make the ip system more accurate.  We also developed a layer between Best Party Ever and Wordpress MU.  This was a fantastic idea as we didn’t want to redevelop the wheel here and we can re-use technologies developed for the wordpress platform for blogging reducing a mountain of work on our part.  Don’t forget that we do full code reviews before deciding on technologies to be part of our core system.  I’ll write a bit more about the daytime party that I live later.  Take care.

Blogged with Flock

Tags: , , , , , , , ,

Pareto Principle

The Pareto Principle

Economist Vilfredo Pareto found in 1897 that about 80 percent of Italy’s wealth was owned by about 20 percent of the population.8 This has become the 80/20 rule or the Pareto principle, which is often applied to a variety of disciplines. Although some say it should be adjusted to a 90/10 rule, this rule of thumb applies to everything from employee productivity and quality control to programming.

Barry Boehm found that 20 percent of a program consumes 80 percent of the execution time.9 He also found that 20 percent of software modules are responsible for 80 percent of the errors.10 Donald Knuth found that more than 50 percent of a program’s run time is usually due to less than 4 percent of the code.11 Clearly, a small portion of code accounts for the majority of program execution time. Concentrate your efforts on these hot areas.

I just recently was doing some research on Javascript Optimization techniques and found this wonderful section on The Pareto Principle.  If you haven’t already read the above two paragraphs, please do.  If you feel brave, please try out this guide on Optimizing Javascript code.

Blogged with Flock

Tags: , , , ,