The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
More about naming conventions

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Eigen Class

Posts: 358
Nickname: eigenclass
Registered: Oct, 2005

Eigenclass is a hardcore Ruby blog.
More about naming conventions Posted: Dec 25, 2005 5:37 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Eigen Class.
Original Post: More about naming conventions
Feed Title: Eigenclass
Feed URL: http://feeds.feedburner.com/eigenclass
Feed Description: Ruby stuff --- trying to stay away from triviality.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Eigen Class
Latest Posts From Eigenclass

Advertisement

I patched ruby 1.8.4 to gather some statistics regarding naming conventions, as far as identifiers are concerned. My local build will now emit such information at parse time as follows:

 $ ruby -t -c -e "def a(x); c = x.size; def x.bar; 1 end; c + 1 end"
 [tokinfo] method def: a
 [tokinfo] tIDENTIFIER assignment for lvar: c
 [tokinfo] singleton method def: bar
 Syntax OK

These are the statistics for Ruby 1.8.4's stdlib:

typeaverage lengthstd devcount
symbol7.04.33214
ivar8.63.91915
smethod9.64.91001
gvar9.14.3377
cname8.34.82124
cvar11.44.4103
dvar_curr3.52.71158
constant10.95.01660
dvar4.73.749
method10.55.67566
lvar4.32.93427

I just added some code to the parser and some support functions in order to capture:

  • uses of symbols (symbol)
  • method definitions (method)
  • singleton method definitions in the form def object.meth (smethod)
  • class/module names (cname)
  • plain constant names (constant)
  • block-local variables assigned to inside a nested block (dvar)
  • block-local variables (dvar_curr)
  • local variables (lvar)
  • instance variables (ivar)
  • global variables (gvar)
  • class variables (cvar)

The difference between dvar and dvar_curr is fairly subtle; in the following example, x is a dvar_curr:

%w[a b c].each{|x| puts x * 2}

"curr" means that the variable was defined in the current block.

When a block reuses a variable defined in an enclosing block, it is considered a dvar:

%w[aasdad dfdsb sdfsfc].map do |x|
  max = 0
  x.each_byte{|b| max = b if b > max }
  max
end

Here max, as seen from the block passed to each_byte, is a dvar.

Comparison across Ruby versions


Read more...

Read: More about naming conventions

Topic: ADV: iAssistant.NET - personal web assistant or online virtual office. Previous Topic   Next Topic Topic: color or colour in Ruby? Both!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use