Ask HN: PHP & Python/Ruby?
I'm decently proficient in PHP. I chose it initially because it was easy to learn and (very easy) to deploy.
I just finished a series of small web projects, and am looking to start something more ambitious. Before I do, I want to reevaluate my choice of language: should I switch to python or ruby?
Let's say I would switch to ruby...
1) How much more productive/efficient would it make my coding?
2) How hard and expensive (relatively) would it be to deploy?
3) Why shouldn't I switch?
4) Other major ruby advantages?
And finally, how should I go about the transition if its worth making the jump to ruby/python?
31 comments
[ 2.8 ms ] story [ 82.0 ms ] threadIn general... ruby there are many ways to do things ( bit of a perl influence there ), python tends towards one sanctioned way to do things. That is a horrible overstatement but somewhat true.
I would advise you to send a few days with each and see which seems more natural. I prefer ruby w/ its blocks and more smalltalk and lisp-ish feel to things but I did python programming for a number of years and you will be fine learning either.
You should switch from php even if you go back to php to learn something new and expand your knowledge AND don't stop there, keep learning and go father afield later. Pick a functional language like scheme, clojure, haskell etc and learn that. Get up and cozy w/ smalltalk for some serious OO learning. Spend sometime with Erlang. Never stop switching what you use to learn even if you keep going back to other things to pay your bills- Java, Perl & C++ payed by bill for 7 years but during that time I learned Dylan, Smalltalk, Scheme, Python & several more.. each one I learned made me a better programmer.
People often write websites "in PHP" and that is a big, heavy and sharp double edged sword: it is this flexibility that has contributed largely to PHPs terrible reputation amongst languages.
The OP is looking to "take things more seriously" so that means developing PHP with a framework, hence it's not relevant to compare Python to Ruby to PHP - the language and it's syntax will have far less impact on the OPs experience than will the structure, idioms and deployment requirements of various frameworks written in each of those langauges.
Then I learned Ruby on Rails.
Kohana and codeigniter set up an MVC environment much like Ruby on rails does. The biggest difference for me is that if you look at kohana, you see how much effort it takes to get PHP to act in a way that Ruby does natively.
Kohana sets up PHP so you have access to all sorts of nifty methods by accessing the system Kohana classes and helpers. In ruby, everything is an object.
So long story short, I find DSL's in PHP try very hard to emulate how Ruby already behaves. So I prefer Ruby frameworks like sinatra and Rails.
I love Kohana, and I'm looking forward to learning Ruby/Rails this year. I have a few small projects lined up to work on while learning, which should be pretty fun, too.
Regarding whether or not to switch I will say this: you can build whatever you want with either language. The big reason you should switch is the fun and challenge of learning something new. Both languages/frameworks have their advantages and disadvantages. You will be LESS productive if you switch languages at first because you will be new and will have to learn the 'ruby way of doing things'. Eventually you may end up more productive in the long run, but productivity is influenced by so many factors and language/framework is just one of them.
Regarding deployment, Heroku (free for small apps) is a good option for if you don't want to deal with deployment much. Linode ($20/mo minimum) is great if you need to do anything fancy and/or want to have root access to your deployment environment.
http://railstutorial.org is the best rails tutorial I've seen
Good Luck!
For me, it mostly comes down to the fact that there's almost always an easy answer for questions in Rails. Pagination? Sorting? AJAX? There's usually a "Best" way too, that makes it easy in deciding which library/plugin to choose.
Disclosure: I've been using ruby (and rails) for 2 years and I am absolutely addicted to it.
This experience is pretty common. I think that says a lot.
Rumor control. While it is true that the lambda construct in Python is only one line, Python allows you to define functions in any scope, and pass those functions around. They are full closures. So anything you wanted to do with multi-line lambdas you can.
Ruby's ability to hijack the scope of a given block via class_eval is amazingly useful in creating DSLs. Creating DSLs in ruby is trivial. The router for Rails 3 is a perfect example of this. Python makes this far more difficult.
1. I am much more productive in Ruby than I ever was in PHP because the language is more consistent and testing is built in. But it takes a long time to understand Ruby so that you can be proficient.
2. it's not hard nor expensive anymore. Dreamhost hosts Rails applications just like PHP apps. Linode.com has VPS setups and scripts to get you going. Heroku is free for small apps. Worry about building something deployable first, and then tackle deployment.
3. You shouldn't switch if you can't take the short-term productivity hit. You're not going to be as productive in Ruby right off the bat because it's new. My interns all agree that the first two weeks of Ruby with me are pretty rough, (80 hours or so) but after that they take off. But it takes a lot longer to program Ruby like Ruby and not like PHP.
4. My opinion will get in the way here, and a lot of the really cool stuff isn't really for beginners. But I am so happy I have the tools I need to accomplish the crazy tasks I've gotten myself into.
5. RailsTutorial.org is a great start - the pdf is free. Also, I run RailsMentors.org - maybe you can find someone there willing to guide you. However, be prepared to come with questions and do some work. Also, find a local user group and hang out. A lot of Ruby people love to help you get started as long as you're willing to put forth the effort.
Feel free to ping me with questions. bphogan on twitter.
Best of luck.
2. There's a bit of a tradeoff between ease of deployment and cost. Specialized hosting services like Heroku are very easy, but can get pricey, especially as you scale. Setting up and running your own VPS can be very cheap[0], but requires learning basic *nix server administration. You should probably learn that anyway, as a web developer, but it does add to the barrier to entry.
3. Switching could be a distraction from actually getting stuff done. You can expect a significant drop in productivity as you learn, especially if PHP is the only language you know. There's a lot of new stuff in Ruby, and Rails has a decent-size API. You may also want to consider the universality of PHP on cheap web hosts if you're writing code for other people to run on their own servers.
4. Ruby, as a language seems to me like the designers really kept the user experience in mind. It's similar to the advantages often cited for Apple products. I've heard similar things about Python, but I haven't used it nearly as much. Another advantage I've found over PHP is that example code in both Ruby and Python is much more likely to be exemplary. There's a lot of example code for PHP floating around on forums and blogs that would be better used for counterexamples.
[0] prgmr.com starts at $5/month, though that plan isn't enough to handle much traffic.
So my advice is: First, work a weekend with bare ruby, there are lots of good and extensive resources on the web. Second, if you enjoyed working with ruby, try another weekend learning basic MVC Rails. Good resource is rails for zombies (http://railsforzombies.org/), where the basics are well shown, and you can work through this on one weekend. Third, try converting one of your small projects to rails. There will be a lot of hiccups and frustration, but once the first project is done, you will simply love ruby.
I hope this helped you a bit, although I have not directly answered your questions (mostly because the given answers are the ones I would write, too).
Personally, I ended up switching for my personal projects only. I look at my PHP code and despite my skills the language is simply limiting me. It's limiting my approach to problems, the readability/flexiblity of my code and the solutions I implement. Even if I have a saying in which technology I implement my solutions with, most of the web dev stuff is fairly well done in PHP _especially_ if you're using sophisticated frameworks. That's why, for clients, I'm still using PHP (for now)
Long version:
1) Ruby and Python are excellent languages. Elegant, readable, dynamic and in general well implemented. Personally, I prefer Ruby because I find it well designed and more intuitive than Python. Ruby is full OOP where Python has those built-in functions and I never know if the call to open and close a file is built-in or part of the File class. In general, Ruby tries to put things together in classes/modules and avoid "lost" functions.
Also, you can fully customize stuff in Ruby. To me, that's an advantage because after a couple of layers in your business model, if your want to do something like Product < Product2 instead of Product.function < Product2.function. Some people abuse it but it really helps simplifying your code when you have a lot of abstraction in your model.
3) Why shouldn't you switch? Like I said, it depends on the kind of dev you're doing. Most PHP programmers are enthusiastic hobbyists and PHP is more than enough to satisfy their needs. So, if you don't see a net return on switching to Ruby, don't. It's worth investigating languages but rarely worth switching e.g Haskell taught me a lot about modularity and gave me a new perspective on programming but I'm not gonna implement my next web app in Haskell
About the transition:
In my case, I like to experiment with languages using a problem. For example, I have a couple of batches in PHP and I needed threading. Guess what my first Ruby app was? So, find a project or something you'd like to build and ideally it's something you can't do with your current language or maybe something that is easier to build with Ruby.
Anyway, I enjoyed writing this stuff down. Hope it helps.
The upside is that there is always a market for Java developers (not that there isn't one for PHP but it's good to have options).
I'm sure that the script kiddies will bury this post and tell you to learn Erlang, Ruby, etc (which are all good in their own right) but that's this old cat's 2 cents.
Your reference to script kiddies? The icing on the cake my friend.
He didn't ask what he shouldn't learn. He asked what he should learn.
I suggested that he learn Java because it is so completely different from PHP. That's all.
Will you be doing scientific programming? Python.
Graphical games? Python.
Web apps? Ruby or Python are about comparable.
Do you really really really want multi-line lambdas? Ruby.
Do you really want to make DSL's? Ruby.
Do you really want code that's easy to read and maintain years from now after it's no longer fresh in your mind? Python
Do you want a language with a smaller number of rules for your brain/tools to parse (ie. a more "regular" syntax)? Python.
Do you want as much "magic" and implicit behavior as possible in the language? Ruby
Do you prefer things to be explicit, but still concise? Python
Do you want to be seen as cool and hip as possible? Definitely Ruby rather than Python
Will you likely be writing lots of CPU-bound code and therefore need a language that has a fast runtime characteristics? Python
Do you think Perl is a good thing? Ruby. A bad thing? Python.
Do you want a lot of open source projects and a rich ecosystem? Ruby and Python are about comparable
Do you want to hob-nob with folks who are, on average, generally younger? Ruby
Do you think "end end end end end end" everywhere looks dumb as hell? do Python
Do you think having to type "self self self self self" a lot in classes feel wrong to you? do Ruby.
Want the one of the two that's most popular/blessed/supported by Google? Python
> Do you really want code that's easy to read and maintain years from now after it's no longer fresh in your mind?
In my experience ruby code isn't inherently less readable than python if you know ruby moderately well. Ruby has more syntax but it's standard library is fairly clear and it's easy to look up.
> Do you think Perl is a bad thing?
I think it's a horrible language and absolutely I love ruby. :) Granted, I learned ruby before I learned perl.
Having said that I completely agree that RoR would be the way to go given the elegance of Ruby and the Rails framework. Will surely give it a try couple more times until I feel comfortable dealing with all the aspects of RoR. Until then CodeIgniter for me...
The key skill that you're looking to pick up is actually what professionals think of as "full stack web development". That is, you should aim to understand lots of things:
- MVC web frameworks like Rails and micro-frameworks like Sinatra
- MySQL and non-relational datastores like MongoDB
- web and proxy servers like thin and nginx
- Redis! it's like a Swiss Army knife... but also Memcached
- jQuery and Haml/Sass
- Backbone and websockets
I could go on and on... the point is that each of these things are awesome for different tasks. You should try your hand at all of them!
Bonus points: I think that SproutCore is going to be a very BIG deal in 2011. You could get the jump on everyone.