Typing Spanish characters in Mac keyboards

I want to share this information with other spanish speaking users of Apple computers. Yes, this can be found in several places all over the web but republishing it once again won’t hurt anyone:

  • á = Opt + e, then a
  • é = Opt + e, then e
  • í = Opt + e, then i
  • ó = Opt + e, then o
  • ú = Opt + e, then u

For the ñ, hold down the Option key while you type the n; release and type n again.

  • ñ = Opt + n, then n

To place the diaeresis over the u, hold down the Option key while pressing the u key; release and type u again.

  • ü = Opt + u, then u

The inverted punctuation marks are achieved as follows:

  • ¡ = Opt + 1
  • ¿ Opt + shift + ?

¡Allí está! Realmente útil, ¿no? :)

Import this

Since i’m still new to the Python culture I wasn’t aware of the “import this” eastern egg. I just heard my friend Gustavo mentioned that IronPython is lacking “import this” and my curiosity was invoked. I quicky tried a few Google searches with no results. Maybe I should had tried something like “import this eastern egg” but I was only suspecting that could be the case so I changed the strategy and fired up a python interpreter on an xterm with this result:

Once the eastern egg had been confirmed I googled a bit to learn more and found it even has a PEP of its own, PEP-20. The text featured is The Zen of Python by Tim Peters. It’s a great teaching! It even appears listed as the first item in this list of python must-read documentation. Nice to know :)

There are some more Python import eastern eggs. Try import __hello__ and from __future__ import braces. Well, for the last one you always have Tim Hatch’s pybraces.

The Evolution of Java

I’m coding some Java this days due to academic reasons: mostly  J2EE web stuff using the now ubiquitous MVC pattern using servlets, JSP, JSLT and JSF. Unless you are a bit of a clueless programer it should be no news to you that Java development is a little bit bloated and no, even if some people like to think that way, Java is NOT the language Bjarne Stroustrup would have designed if he hasn’t had to remain compatible with C as he clarifies in the FAQ of his website. So you shouldn’t be that surprised after all when reading how Cay Horstmann, a computer science professor at the San Jose State University, has summed up the evolution of Java from C and C++:

1980: C
printf(”%10.2f”, x);
1988: C++
cout << setw(10) << setprecision(2) << showpoint << x;
1996: Java
java.text.NumberFormat formatter = java.text.NumberFormat.getNumberInstance();
formatter.setMinimumFractionDigits(2);
formatter.setMaximumFractionDigits(2);
String s = formatter.format(x);
for (int i = s.length(); i < 10; i++) System.out.print(' ');
System.out.print(s);
2004: Java
System.out.printf("%10.2f", x);
Don’t know you but I really found it funny :)

TinyURL got custom aliases

I’ve just realized TinyURL got custom aliases, that is, you can customize the part of the URL beyond their domain to point to the desired URL. I like it. I had that idea myself some time ago. So now http://tinyurl.com/gnrfan redirects to this blog. Pretty neat, huh? The only problem, in this specific case it’s I’ve got a 17 characters url shortened to 25 :)

It looks like not all words are available. When I tried to point to the RIAA homepage using the custom alias “suckers” I got a message telling me that custom alias is not available and no, it’s not somebody grabbed it before. Seems fair.

What are you waiting for? Go grab your custom alias now!

Keyboard freezes on Mac OS X

I got a white Macbook Santa Rosa a few months ago basically because I wanted to have a long-period first-hand experience using OS X on proper hardware. I’m a Linux fan and FLOSS advocate and surely Apple is a gross propietary-minded corporation and yes, I know I should be rejecting their products as the plague but I’m also a curious geek and I came to the point where I was clearly feeling I was missing 1/3 of today’s desktop action if I only used Linux and Windows. So I bought it and can’t regret it, I’ve been quite happy so far.

It’s no news Apple computers just work. When you switch, you learn your way around the new system in the first couple of weeks. Of course you don’t get to know every aspect of the OS but you get to learn all the stuff you really care about and use everyday.

Macbook

So everything was really fine with Leopard until I started to notice this very rare keyboard freezes. Wait! Keyboard freezes? I guess I had never experienced something like that with Windows and Linux before unless it was a bad cable or the keyboard cord got disconnected so at the very first time the concept seemed a bit alien. I’m a geek and the most of the times I know may way around computers and even without the keyboard to perform some basic testing and diagnosis I concluded that most of the running software was doing just fine and you could continue browsing the net and doing anything else that only required the use of the mouse so this was something strange that only compromised the use of the keyboard. But having no keyboard is quite annoying! Specially for a Linux guy that has at least one terminal open under OS X in order to feel sane taking my daily dose of bash, vim, wget and a few other command line utilities I can’t live without.

I realized that the keyboard freeze got fixed when I closed the current session and started a new one. Despite the session fix finding I must admit I was worried the thing was some weird hardware issue and I was somewhat expecting it to be gone by itself because it was happing once every week or two weeks and I could handle that.

Now a couple of weeks ago this freezes started to happen more and more often and the last days almost daily. Sometimes I had too many apps opened or I was using Windows under VMware so I was really a hassle so I searched the web for a solution and quickly found the patches from Apple in the form a small 978K update that requires a bigger 110 MB update that upgrades the system to version 10.5.1.

The upgrade process requires you to reboot your Macbook twice: first after the upgrade to OS X 10.5.1 has been completed then after installing the keyboard freezes fix. I’ve been using the Mac for a while and the problem seems to be gone.

I’m posting this as a reminder to myself because I might be trying a full reinstall of the software in my laptop in a few weeks in order to get a clean triple boot system up and running and because someone else may find the post useful.

PHP Best Practices

Yesterday I attended the Ubuntu 8.04 Hardy Heron LTS Release Party event the folks from Ubuntu Peru and Linurp organized here in Lima at Universidad Ricardo Palma. Nice talks on Ubuntu, KDE, GNOME, Drupal and PHP among some other stuff. I particularly liked the talk by the good old Jesus Castagnetto of PEAR and Professional PHP Programming fame. His slides (spanish) are posted at his site. My summary including bits i’ve been adding follows:

  • PHP is an easy language to pick up & learn but it’s a bit tricky to master
  • During the development phase of your project set the error_reporting value of php.ini to E_STRICT in order to get all sort of errors
  • Please set an error log and read it! You are going to need it to monitor your application when it’s deployed for production. Don’t forget to stop displaying errors to the end user in production sites. If you do, chances are you’re disclosing very useful information for an attacker trying to take control of your site or shutting it down.
  • If you’re using clean urls (and you should!) and not using the .php extension for your pages you don’t need to disclose that you’re site is running PHP. Setting the expose_php value to zero in php.ini might be a good idea.
  • Please get rid of code (yours or written by someone else) that depends on registers_globals = On. This configuration option won’t even exists when PHP 6 is released. Having PHP or your own code create global variables automagically from data sent by the user is a major security threat. So please resit the temptation of using it.
  • Please stop using magic quotes. Today they’re not a good idea. Actually they never were a good idea and will be depracated in PHP 6. As the programmer you’re responsible for adding quotes properly to any string that might need them.
  • Since version 5, PHP got exception handling. Please don’t abuse then exception handling mechanism by raising an exception at the minimal user-generated error. Raise an exception instead when a critical condition that is need for your program to continue working properly is not met, specially when you expect some other part of you code catching the exception and doing something about it.
  • Exceptions can consume considerable memory if your stack trace gets big. They are useful but not cheap.
  • Don’t debug you’re code using exclusively echoes, prints and var_dumps. You should be using a proper debugger instead. A very good one is XDebug. You get the extra advantages of getting a full stack trace and profiling capabilities.
  • No matter if you’re a solo coder or part of a team you need to follow coding standard in order to produce a consistent-looking codebase that can be easily read by anyone including yourself. PEAR has a good coding standard that has been proved in many PEAR contributions over a good few years. The Zend Framework provides you with a newer coding stardard (draft) that is also based on PEAR’s.
  • Documenting might be a bit boring but it’s critical for the success of your project over it’s lifetime. If you’re code is non-trivial you’re much better embedding good documentation in it that will help you in the first place to understand that are those lines of code doing when you come back to make changes. A quality tool for generating PHP documentation is phpDocumentor. It’s fast and can generate not just only API docs but tutorials by parsing your code if you learn to use it properly.
  • Please don’t reinvent the wheel. PHP has lots of useful functions that are implemented in C and will definitely run faster than you’re code. If you caught yourself reimplementing native functionality it’s your fault for not reading the manual and keep up with the changes and additions to PHP and it’s core extensions. Know your tool. Investing a bit of time in reading the manual will definitively pay off.

There are a few tips related to OOP with PHP, specially PHP 5:

  • Don’t over-engineer your code. PHP is not Java and in many circumstances you’ll be good using just a simple array which is a native data structure instead of a user defined class. Keep things simple.
  • Don’t use is_a() but instanceof() instead for checking if an object is a member of a class since is_a() was depracated as of PHP 5.

In order to improve the security of your code you can follow this guidelines:

  • You must ALWAYS initialize your variables, specially those involved in authentication, authorization and security checks.
  • Never ever trust your user on providing the right kind of input for your program to work properly. You must always validate that the user’s input is good for you
  • PHP has the filter and ctype extensions for validating user input. You can always use regular expressions for custom validations.

Finally, for performance improvements you have this recommendations available:

  • Don’t use double quotes in strings if there’s no variable interpolation or character escaping taking place. This saves a few miliseconds in each string so if you have many strings in your code and many visits in your website this alone can be a huge CPU and time saver.
  • In many places you can speed things a bit up by avoiding string concatenation. Just use a comma in echo to get the same output effect.
  • Prefix variable incrementation is a bit faster than postfix incrementation so if you’re not assigning the value of the variable before incrementing it you can easily gain a few extra milliseconds and consume less server CPU with this simple change so you ++$i instead of $i++ whenever you have the chance.
  • By all means you must avoid recreating the same dynamic content over and over again if you can be sure it won’t be changing. This is when content caching comes handy. Something as simple as reading a generated HTML chunk from a file in the filesystem instead of issuing again the database queries and/or performing the calculations needed to generate can be a big improvement. Generally cached content is not useful or trusty after some period of time has passed so put your attention in regenerating the cached content when it’s no longer valid.
  • Your own PHP can benefit a lot from using a caching mechanism so PHP doesn’t have to compile your code over and over again if it hasn’t change. Avoid wasting server CPU and response time. APC is a very good code caching solution for PHP and is mantained by Rasmus Lerdorf, the creator of PHP himself.
  • Don’t guess which parts of your code are the bottlenecks. Use a profiling tool instead. Remember XDebug includes a profiler.
  • Even if it’s not something you do at the PHP level you can always take advantage of a data compressing mechanism like Apache’s mod_gzip. Compressing content in both requests and replies is something that has been available in the HTTP specs for a while and you can easily save up to 80% of the bandwidth and response time.

I really think it’s a good idea to use lists like this as check-lists and use the techniques in your projects.