Oleg Andreev

speaking few languages about programming, interfaces, culture, maths and nature

Subscribe to RSS feed

Novemberain.com co-author

Hi! I'm starting write to http://www.novemberain.com/ (Russian only, sorry)

I'll put most interesting thoughts here (translated in English).

Aspects in MVC, part 1

,

ActiveRecord validation messages should be stored together with controller and views' messages. Messages should be grouped by feature they appear in.

When we click a link, we teach The Machine

Web 2.0 explained in musical video:
http://www.youtube.com/watch?v=6gmP4nk0EOE

Automated refactoring (idea only)

Consider the following code (Ruby):

class Customer
  attr_reader :name

  def initialize(name)
    @name = name
    @rentals = []
  end

  def add_rental(arg)
    @rentals << arg
  end

  def statement
    total_amount, frequent_renter_points = 0, 0
    result = "Rental Record for #{@name}\n"
    @rentals.each do |element|
      this_amount = 0

      # determine amounts for each line
      case element.movie.price_code
      when Movie::REGULAR
        this_amount += 2
        this_amount += (element.days_rented - 2) * 1.5 if element.days_rented > 2
      when Movie::NEW_RELEASE
        this_amount += element.days_rented * 3
      when Movie::CHILDRENS
        this_amount += 1.5
        this_amount += (element.days_rented - 3) * 1.5 if element.days_rented > 3
      end

      # add frequent renter points
      frequent_renter_points += 1
      # add bonus for a two day new release rental
      frequent_renter_points += 1 if element.movie.price_code == Movie.NEW_RELEASE && element.days_rented > 1

      # show figures for this rental
      result += "\t" + element.movie.title + "\t" + this_amount.to_s + "\n"
      total_amount += this_amount
    end
    # add footer lines
    result += "Amount owed is #{total_amount.to_s}\n"
    result += "You earned #{frequent_renter_points.to_s} frequent renter points"
    result
  end
end


Many code for a single routine, huh? Let's refactor it. See one-line comments like "# show figures for this rental"? These are signs you need outsource a code to another method. So, we do it.

class Rental
  ...
  def determine_amount  
      this_amount = 0
      case movie.price_code
      when Movie::REGULAR
        this_amount += 2
        this_amount += (days_rented - 2) * 1.5 if days_rented > 2
      when Movie::NEW_RELEASE
        this_amount += days_rented * 3
      when Movie::CHILDRENS
        this_amount += 1.5
        this_amount += (days_rented - 3) * 1.5 if days_rented > 3
      end
  end

  def bonus
     if movie.price_code == Movie.NEW_RELEASE && days_rented > 1
       1
     else
       0
     end
  end

  def frequent_renter_points; 1; end
end
class Customer
  attr_reader :name

  def initialize(name)
    @name = name
    @rentals = []
  end

  def add_rental(arg)
    @rentals << arg
  end

  def statement
    total_amount, frequent_renter_points = 0, 0
    result = "Rental Record for #{@name}\n"
    @rentals.each do |element|
      this_amount = element.determine_amount
      
      frequent_renter_points += element.frequent_renter_points
      frequent_renter_points += element.bonus

      # show figures for this rental
      result += "\t" + element.movie.title + "\t" + this_amount.to_s + "\n"
      total_amount += this_amount
    end
    # add footer lines
    result += "Amount owed is #{total_amount.to_s}\n"
    result += "You earned #{frequent_renter_points.to_s} frequent renter points"
    result
  end
end


First, we have thrown away operations involving calls only to "element". Not exactly away, but into element's class - Rental. We have preserved comments over operations involving routine-local variables.

I hope to describe this more detailed to make a tool like tidy/beautifier which could cope with this somehow automatically.

ActionScript 2.0 nonsense

,

I have a bunch of old AS code (you know, function(){} instead of class{}, .prototype using and so on) and trying to compile it in Flash 8:

1) First frame: #include "cube.as" (BTW, there is no class named "cube" or "Cube")

2) Ctrl+Enter: ***Error*** ActionScript 2.0 class scripts may only define class or interface constructs.

3) Renaming cube.as into cube.as1

4) Doing #include "cube.as1"

5) Error message disappears!

ActionScript is still as stupid as it was in Flash 4. Much more powerful, but still stupid.

Ruby on Rails is not for the beginners

Finally, I've understood how is it hard to explain some features of RoR to those who are not experienced much with the webdev.

Looking at all these RJS Minus R, REST, automatic pluralization, hidden methods and all, I see that it is not trivial. It seems, that you can understand all that stuff and reasons behind it in only two cases: you're amazingly genious, or you've worked hard with PHP, Java or Perl before. If the latter, you understand the concept of RoR easily.

Thus, RoR's helloworld-like applications are too complicated to very beginners, because in such case a beginner must accept an idea of MVC, directory hierarchy and such even before getting actual "Hello world". When he/she wants a more complex examples, it gets more complicated (according to possibly fast and dirty alternative in PHP). In reality it's not complex, but for beginner it too much, since he/she doesn't have any idea of What's Going On behind These Methods. To understand all the background stuff a beginner have to learn much and accumulate an experience.

RoR doesn't provide easy and clear way to learn WebDev, as PHP does. I don't see a million-army of RoR developers (most of them, of course, are nubies) in the nearmost two years.

The only thing that may lead to such an army is a redesigned version of RoR, maybe light library to Ruby with ERb or something, which allows you to access .rhtml directly through the browser. It will look exactly like PHP, so why do we need it then?

And you know what? I think it pretty cool that RoR is a cute professional framework and most of its users are smart, opinionated and experienced. I feel myself in a good place then.


Ruby DSL

, ,

1. The following is a valid Ruby program:
a b c

in other words:
a(b(c()))

in the very other words:
method_missing(:a, method_missing(:b, method_missing(:c)))

2. Test case:
put an apple on the table to the left OR to the right of me

HAML tested

, ,

Hypertext Abstraction Markup Language (HAML) is a great template layer by Hampton Catlin (Toronto, Canada). The basic idea is to increase productivity (as usual, though).

I've marked up about 4 Kb already (all in all, first day usage) and there're results:

1. It's easy
2. It's compact (twice less lines and three times less bytes compared with HTML)
3. It's perfectly readable
4. It's fast
5. There're few bugs

What bugs:

1. Closing tag from the false if-then-else clause may appear where is not intended. This is an issue in the release 0.2.0 (trunk branch), but fixed on edge branch (use /branches/edge instead of /trunk when installing)
2. Fix yourself: tags id's ar cut by first alphabet characters (
#step-1-a
transforms into
id="step-"
). Find
buffer.rb
, method
parse_class_and_id
and add
0-9
to regexp.

In summary, it's worth installing it.

¡Hola amigas y amigos!

,

Hay una nota en el titulo de esta pagina: "hablo en quatro idiomas sobre ...". En realidad, tengo un poco articulos y los todos en ingles o ruso. Que pena. Pero ahora empiezo escribir en español. ¿Sabe como es dificil usar el teclado con ruso y ingles repartimientos? Tengo que apretar los Alt+0161 para "¡", Alt+0191 para "¿" y Alt+0241 para ñ. Y para Ñ, y para todos é, á, ó, í, ï, ú, ü, ý. ¡Es tan abburido!

Pero escribo de un razon: quiero aprender español para communicar con amigos en España, trabajar con ellos y finalmente viajar allí para vivir. Donde es el calor, el sol, el mar y no mas tener que llevar la ropa para invierno, primavera y otoño.

¡Gracias!

Portable software

User's software has:
1. Program files (or web resource)
2. User data
3. User preferences (settings)

Every SW must provide a well documented process of moving user data and user settings (with and without user data) from one installation of this SW to another, or to archive file to be able recover data.

The principle is dead simple: use of SW is connected with data creation. This data is owned by the user, not SW developer. So, user must be provided with friendly interface to fetch his data and move it easily, at least, to another box with the same SW installed.

For example, Opera's email client is dumb enough to hate it while moving on the new PC. In contrary, Thunderbird tells you how to migrate.

The best solution is to pack the whole SW with all relevant data and to unpack it on the another system. Of course, setup of the new environment must be done automatically with possibility to control and track the process.

It must look just like a migration algorythm with DO and UNDO infinitely repeatable operations.