Professional Web Applications Themes

Simple litle style question - Ruby

This is just a simple little stylistic query that I became curious about after one of Ara Howard's questions the other day. In general, do people prefer: def simpleExample(t) x = block_meth do r = nil if t == 'forty-two' r = 42 else r = 0 end r end x end or: def simpleExample(t) x = block_meth do if t == 'forty-two' r = 42 else r = 0 end end end both assuming this method is available: def block_meth yield end i.e. do you explicity declare a return variable and put that variable right before the 'end' or ...

  1. #1

    Default Simple litle style question

    This is just a simple little stylistic query that I became curious about
    after one of Ara Howard's questions the other day.

    In general, do people prefer:

    def simpleExample(t)
    x = block_meth do
    r = nil
    if t == 'forty-two'
    r = 42
    else
    r = 0
    end
    r
    end

    x
    end


    or:

    def simpleExample(t)
    x = block_meth do
    if t == 'forty-two'
    r = 42
    else
    r = 0
    end
    end
    end


    both assuming this method is available:

    def block_meth
    yield
    end


    i.e. do you explicity declare a return variable and put that variable
    right before the 'end' or do you rely on your audience being smart enough
    to perceive what the return value is going to be?

    On a really short piece of code like this, I appreciate the elegant
    simplicity of not being explicit, though my person trend is to be explicit
    anyway. On a longer piece of code, though, I can certainly see where it
    might not be as clear to a reader of the code just what is being returned,
    especially from a casual scan, if it is not done explicitly.

    Is this clarity helpful, or is it a minor enough thing that the ugliness
    that it imparts to the code isn't worth it? What do you all think?


    Kirk Haines




    Kirk Guest

  2. #2

    Default Re: Simple litle style question

    Kirk Haines wrote:
     

    I would do the following, which is more in the spirit of an expression-based
    language:

    def simpleExample(t)
    block_meth do
    if t == 'forty-two' then 42 else 0 end
    end
    end

    Personally, I wouldn't assign to variables that I'm not going to use.

    --
    Grzegorz Chrupała | http://pithekos.net
    | org
    Grzegorz Guest

  3. #3

    Default Re: Simple litle style question


    David A. Black said:
     
    >
    > I prefer "simple_example" :-)[/ref]

    Amen!
     

    I prefer either of these one-liners over the longer examples. They
    succinctly express the heart of the calculation without any extra noise.
     

    Good Heavens! Thank you. Where the other oneliners express the logic
    directly, this one tends to cloud it (IMHO).
     [/ref]

    On the short examples given, I would let Ruby be Ruby and just have the
    last (and only) expression be the implicit result of the function.

    If the function is longer (more than one expression), then I might use a
    return variable, especially if the expression I wish to return is the last
    expression only by coincidence. When I use a return variable, I _always_
    spell it "result", and only use the name "result" for return variables.

    --
    -- Jim Weirich org http://onestepback.org
    -----------------------------------------------------------------
    "Beware of bugs in the above code; I have only proved it correct,
    not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)



    Jim Guest

Similar Threads

  1. Need Simple Answer to Simple Contribute/Firefox question
    By Adobe nickname in forum Macromedia Contribute General Discussion
    Replies: 2
    Last Post: February 13th, 06:59 PM
  2. Nested Paragraph Style Question
    By Kenny_Hoff@adobeforums.com in forum Adobe Indesign Macintosh
    Replies: 5
    Last Post: September 12th, 01:13 AM
  3. alert style question
    By globalview in forum Macromedia Flex General Discussion
    Replies: 0
    Last Post: July 15th, 08:55 PM
  4. Hello a have litle trouble with one of my servers.
    By Kjell tofte in forum Windows Server
    Replies: 1
    Last Post: June 29th, 08:48 PM
  5. Style question: this should be simple, but
    By Joe in forum Macromedia Dreamweaver
    Replies: 2
    Last Post: July 30th, 05:11 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139