Advanced Rails - Building Industrial-Strength Web Apps in Record Time

(Tuis.) #1
How to Read Code | 49

How to Read Code


As implied by the quote introducing this chapter, the primary purpose of source
code should not be expressing implementation to a computer; it should be express-
ing meaning to people. Programming languages are an incredibly expressive and
terse medium for the concepts programmers talk about. Proposals to make program-
ming languages more English-like inevitably fail not because of poor implementa-
tion but because there is an inherent impedance mismatch between the domains of
English language and computer programming.


Thus, computer programming languages should be compared not by their levels of
raw power (any Turing-complete language trivially satisfies this requirement) or
speed of execution (for most applications, speed is not critical) but by their program-
mer efficiency—the speed at which a programmer can accurately translate his
thoughts into code.


Closely related to programmer efficiency is maintainer efficiency: the ability of a
maintainer (who may be the original developer, 12 months later) to read the code
and deduce what is going on. Perl is often criticized for being “write-only”; it is easy
to write code that is nearly unreadable to future developers. Such code would have
high programmer efficiency at the cost of maintainer efficiency.*


Ruby wins on both fronts: most Ruby code is easy to write and read, once you know
the basic syntax and semantics.†Still, diving into any large project such as Rails is
difficult. Here, we discuss ways to begin reading a codebase.


How to Locate Code


One disadvantage of the dynamic nature of Ruby is that there is little opportunity for
development-time reflection on Ruby code. When using a more static language, IDEs
can infer the type of variables, and from that deduce the methods available to those vari-
ables. Thus, they can offer assistance in coding by suggesting variable and method
names. In Ruby, in principle the only way to know the methods available to an object is
to evaluate the expression returning that object’s value. This is clearly impractical due to
side effects of evaluation or differences in development and execution environment. The
effect is that it is impossible to write a general code-completion-style IDE for Ruby.


In practice, this is usually not a problem. Ruby follows a development style that is
closer to Lisp than to C/C++. Developers interact with their application while cre-
ating it, and usually answer questions about the state of the system by asking



  • I don’t mean to bash Perl here. It is very possible to write structured, easily readable code in Perl. But it takes
    some self-discipline.
    † For some amusing counterexamples, seehttp://iorcc.blogspot.com/.

Free download pdf