Schnitzel IT-alia

Schnitzel IT-alia

Die IT-Crowd hat sich ohne mich getroffen und gekocht. Es gab, dem Berufsstand entsprechend, ein Schnitzel IT(alia). Leider hat Julia das Rezept aus diesem Buch (kein Affiliate-Link) und daher kann ich das Rezept* leider nicht veröffentlichen. Dafür gibts aber als Entschädigung ein schönes Bild!

* Hier im Schnelldurchlauf: Schnitzel mit Pesto bestreichen und mit stückigen Tomaten aus der Dose, Zucchiniraspeln, Sahne, Basilikum und Mozarella im Ofen überbacken. Fertig!

Prototype JavaScript von Rails 2.3.x für Rails 3.0.x mit jQuery austauschen

Beim Update einer Rails 2.3.14 Anwendung auf Rails 3.0.11 habe ich das komplette Prototype JavaScript rausgeschmissen und durch jQuery ersetzt. Dafür habe ich alle vorhandenen JavaScript-Dateien gelöscht und durch eine einzige application.js ersetzt. Diese und jQuery binde ich nun so (am Ende meiner application.html.erb) ein:

<%= javascript_include_tag 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js' %>
<%= javascript_include_tag 'application' %>

Leider funktionieren dann alle Links nicht mehr um Einträge zu löschen, da Rails 2.3.x dort Prototype JavaScript verwendet, um eine DELETE-Anfrage abzusenden. Das ist notwendig falls Browser und/oder Server nicht die HTTP-Methode DELETE verstehen. Der alte Code wurde an ein onlick="..." gehängt und sah so aus:

if (confirm('Are you sure?')) {
  var f = document.createElement('form');
  f.style.display = 'none';
  this.parentNode.appendChild(f);
  f.method = 'POST';
  f.action = this.href;
  var m = document.createElement('input');
  m.setAttribute('type', 'hidden');
  m.setAttribute('name', '_method');
  m.setAttribute('value', 'delete');
  f.appendChild(m);
  var s = document.createElement('input');
  s.setAttribute('type', 'hidden');
  s.setAttribute('name', 'authenticity_token');
  s.setAttribute('value', 'fzj20hGLkT...PPsw=');
  f.appendChild(s);
  f.submit();
};
return false;

Ich habe diesen Code nach jQuery übersetzt und in meine application.js gepackt:

//Beispiel für einen Link:
//<a href="/posts/post1" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Destroy</a>
$('a[data-method="delete"]').click(function(ev){
  ev.preventDefault();
  if(confirm($(this).attr('data-confirm'))) {
    var f = $('<form>');
    f.css('display', 'none');
    f.attr('method', 'POST');
    f.attr('action', $(this).attr('href'))
    var m = $('<input>');
    m.attr('type', 'hidden');
    m.attr('name', '_method');
    m.attr('value', 'delete');
    m.appendTo(f);
    var s = $('<input>');
    s.attr('type', 'hidden');
    s.attr('name', $('meta[name="csrf-param"]').attr('content'));
    s.attr('value', $('meta[name="csrf-token"]').attr('content'));
    s.appendTo(f);
    f.appendTo('body'); //CRAZY: not required to submit that form!
    f.submit();
  };
});

Vielleicht nicht der beste jQuery-Code aber es erfüllt seinen Zweck. Für Vorschläge von etwas talentierteren Frontend-Entwicklern bin ich dankbar!

Shaun das Schaf Muffins

Shaun das Schaf Muffins

Letztens im Penny Markt in Dresden konnte ich mich nicht beherrschen und musste mir diese fragwürdige aber lustige Fertigmischung aus der Aktionstheke kaufen. In die Schokoglasur habe ich noch einen großen alten Lindt-Schoko-Hohlkörper-Bär eingeschmolzen für das absolute Schoko-Schaf-Armegeddon. Als schnelle Zuckerzufuhr fürs Hirn beim Programmieren sehr gut geeignet. Aber Vorsicht: Nichts auf die Tastatur bröseln lassen!

Die Backfire Freifunk Firmware auf einem WRT54G flashen

Ich hatte die aktuelle Freifunk Firmware (OpenWRT Backfire 10.3.1/LuCi 0.10.0) mit dem Imagebuilder gebaut und auf meinen Linksys WRT54G geflasht. Als ich sie wieder loswerden wollte, hat das Webinterface immer mit einem Timeout reagiert und eine andere Firmware (z.B. ein reines OpenWRT) nicht auf den Router geflasht. Der einzige Weg, der bei mir funktioniert hat, war der Folgende:

per SSH einloggen

$ cd /tmp/
$ wget http://downloads.openwrt.org/backfire/10.03.1/brcm47xx/openwrt-brcm47xx-squashfs.trx
$ mtd write /tmp/openwrt-brcm47xx-squashfs.trx linux && reboot

etwas warten und dann ganz normal einloggen

Wenn das auch noch nicht funktioniert, kann man vor Schritt 4 nochmal ein mtd unlock rootfs_data versuchen. Wenn das immernoch nicht funktioniert hat (was ich nicht hoffe), dann einfach nochmal im OpenWRT Wiki nachlesen und ein paar andere Sachen ausprobieren.

Nachtrag:

Jetzt trat der gleiche Fehler auch bei einem normalen OpenWRT auf. Nach dem Löschen des Browsercaches ging es dann aber. Ist also auch noch ein Versuch wert…

Why PHP still sucks, even if you like it

There is some discussion going on about this post. So here is what I think of it.

If PHP is the first programming language you’ve ever learned, you maybe feel comfortable with it because it is easy to get things done, it is easy to deploy copy to a webspace and forgives you anything which is important to know as an upcoming programmer. Short: It feels just good. But if you will ever get in the real world with real problems, you will notice that something isn’t quite right. Something started to get weird, it’s like missing an important thing, but you don’t even notice what it is. After some time you get frustrated and ask yourself what the problem is. Maybe you start thinking about and try a language you probably flirted with in the past, but you never had the time to look on beside your extensive Wordpress template hacking. I saw enough people trying other languages like Python or Ruby after years of PHP development and they were so happy to have a real language without all the problems they were struggling with in the past. I was one of that guys and thought “Wow, how fun coding can be”!

I think nowadays for most PHP people it feels like using a walkman. It kinda works, but the sound is crap. But hey, this is the one I grew up with, so lets stick with it. Maybe it’s an argument for some kind of cool old hardware, but not if you want to write good (web) applications. There is no place for nostalgia.

After the initially mentioned post, some people started to write blog posts with titles like “PHP Sucks! But I Like It!”. There are some reasons mentioned in this post, why PHP is still useful but they are not the best ones. Because they are not an advantage of the language PHP. The reasons why PHP is widely used in that way are more historical than technical ones.

Since I am a Ruby guy, who switched from PHP years ago, I will give examples from the Ruby world.

PHP is best in HTTP. Tell me tangible parts where Ruby (or Python) is not, but PHP. At least Ruby has also built-in HTTP Methods. And it’s not the benefit of the language PHP that there are many CMS like Wordpress, Typo3 and all the rest. In fact, it’s the opposite of that. Because PHP is so ugly, unmaintainable and inconsistent, no one wants to write plain PHP to build web apps or frameworks. If you can have one or two very good and effective frameworks, who’s in need for just another crap CMS which covers only 80% of what you initially want to do, where the other 20% are a pain in the ass to implement against that CMS?

Ruby got famous (at least in the western world) with the rise of Ruby on Rails in 2005. Thats the main reason why people are often confused about Ruby as an independent programming language. People started to write Ruby because they started using Ruby on Rails. And no one felt like there was time to pimp the Ruby language with some stupid things, which are only needed for the specific web domain.

PHP started as some kind of “Personal Home Page” tools and evolved to a “language” with nifty things like register_globals, safe_mode und magic quotes, which as far as I know aren’t really good features of a native web programming language. Also the whole OOP thing was introduced after some years, which is a big and still noticeable issue today. Maybe there was no better alternative back in 1998 and everyone started to use PHP to get things done in web. So yes, PHP was a good choice for fast coding web tools, but that was 14 years ago.

PHP is for fast web development. Given the fact, that it is really hard to measure the speed of development, I will give a simple example. Sinatra is a DSL for fast developing web applications. It’s written in Ruby. Take a look at it and tell me: How will you be faster with anything else? There are some things like Sinatra for PHP. So have a look at the example (I know, that the programmers view doesn’t count for a customer, I will get to that later):

How a Hello World web app needs to look like with PHP:

// Fat-Free Framework http://bcosca.github.com/fatfree/
require_once 'path/to/base.php';
F3::route('GET /','home');
  function home() {
    echo 'Hello, world!';
  }
F3::run();

How a Hello World web app should be with Ruby:

# Sinatra http://www.sinatrarb.com/
require 'rubygems'
require 'sinatra'

get '/' do
  'Hello, world!'
end

Now tell me, what is better to read and therefore better to code, maintain, extend? This is a real example for differences on language level. And this is what developers see all day long.

With PHP it’s easy to deploy. Well, all I have to say is: What can be easier then doing a

git push heroku master

For everything else on that topic, read the initially mentioned blog post, which is quite more detailed than mine.

Customers want to have PHP because developers and hosting costs are cheap. If the price is the only thing a customer cares about building an application, then you threatened him in a bad way or he needs some serious enlightenment. Do you remember when you bought the cheapest car, phone or tv? No? Me too. So think about that, if you want to use it as a reason for something in the future. Between “doing the job” and “solving a problem in a efficient and sustainable way” lies (at least for me) a slightly different approach to deal with a customer. Sure, maybe it will be more expensive sometimes, but the product is even better. If you can’t make your customers understand that, then you failed before you even started coding PHP. Beside that, everyone would work for more money, if there weren’t so many middle-class developers beating down the price. Also costs for hosting Ruby will (or are already) be adjusted to the same level of PHP. This is also a historical reason, why every provider offers webspace with PHP and nothing more. It’s simply there for a longer time.

Are there reasons left to stick with PHP?

If yes, let me know, if not, go ahead learn something new to built a better web.

Try this ones and see which language fit’s your needs without being stupid:

http://learnpythonthehardway.org/

http://tryruby.org/

or if you are brave: http://nodejs.org/