Ruby violates hallowed Structured Programming sophistries

From: Phlip (phlip_cpp_at_yahoo.com)
Date: 08/21/04


Date: Sat, 21 Aug 2004 12:58:34 GMT

Comp.Object:

Before Object Oriented Programming, people who studied the difference
between cruft and maintainable code learned to improve things by linking
control flow to variable scope. An example in C:

    if (whatever())
    {
        int x = something();
        somethingElse(x);
    }
    x; /* this line won't compile, unless another x is available from
above */

Structured Programmers decry the mighty goto keyword, able to leap tall
procedures in a single bound. When a language permits a goto unfettered by
that language's block system, {}, variables defined in statements controlled
by goto have ambiguous scope. Structured Programming enables the style
guideline "give identifiers the most restricted scope possible".

I am unaware if Ruby supports goto. (Please please please don't tell me if
it does or not!) Object Oriented Programming subsumes all Structured
Programming ideals. However, Ruby permits this:

        def newThing()

            if something() then
                element = constructor(TkcLine)
            else
                element = constructor(TkcPolygon)
            end

            element.configure('smooth', '1')

        end

Ruby creates variables by assigning to them. That cures an ancient problem
with the C languages, where variables can define without values. The lowly C
statement int x; efficiently produces an x that might contain anything, and
should not be used until assigned. Ruby fixes that problem by forbidding
creation without assignment.

Ruby variables create into their method's scope, not their block's scope.
That's why the above code works - it tweaks the Structured Programming
rules. The element inside the if statement's blocks should, in theory, exit
scope and disappear before the last element.

Ruby supports Structure Programming much more subtly than by decrying goto.
When a goto leaps a tall procedure in a single bound, the root problem is
that tall procedure itself. Ruby's syntax maximizes opportunities to
minimize code. The style guideline "don't goto" is subservient to the
Simplicity Principle "Minimize Statements, Methods, and Classes".

For example:

        def newThing()
            klass = if something() then TkcLine else TkcPolygon end
            element = constructor( klass )
            element.configure('smooth', '1')
        end

-- 
  Phlip
  http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces


Relevant Pages

  • Re: strict pragma & scope
    ... > Sometimes the book isn't direct enough on "this is how you will be programming ... This is, in part, because not everyone programs in Ruby quite the same ... It seems that there are some different approaches to scope. ... The idiomatic way to loop in Ruby is with #each or other iterator items. ...
    (comp.lang.ruby)
  • Re: Probably an FAQ, but...
    ... You referred to 'dedicated' Ruby coders, ... Another person on this list that's been programming a "longish" ... time and is in the intersection of language lawyers (aka people who ... innovations have *led* the formal specifications, ...
    (comp.lang.ruby)
  • Re: GOTO vis-a-vis professional vs amateur programmers
    ... I trained dozens of programmers who were used to using GOTO all the ... A good programming language looks after a lot of those things. ... the language can protect you only to a certain point. ... You can practice and hone your skill in using the risky tool so ...
    (comp.lang.basic.misc)
  • Re: Rene is a hypocrite (OK, what else is new?)
    ... I guess this means that Rene and his minions blew it again :-). ... > underscore can be used to make numbers appear more easy to read. ... misunderstanding of what programmers consider "good programming ... It is creating scope, ...
    (alt.lang.asm)
  • Re: Ideas on "Why Living Dangerous can be A Good Thing" in Ruby?
    ... The fear in part has to do with type checking. ... essential to 'safe' programming. ... I personally have never crashed my computer using Ruby. ... just removing the type checking and other 'security' measures. ...
    (comp.lang.ruby)