lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Russell Haley once stated:
> On Tue, Jun 6, 2017 at 10:03 AM, Sean Conner <sean@conman.org> wrote:
> > It was thus said that the Great Russell Haley once stated:
> >> Sorry for the top post.
> >>
> >> Oh, and twenty indents? The version of Lua I use has a keyword called
> >> 'function'. And adhering to 80 columns is just silly. It was silly 20
> >> years ago when I started writing code.
> >
> >   But where's the cutoff?  I have some XSLT code [1] where some of the lines
> > are long.  Really long.  Like 250 characters long:
> >
> >   <xsl:param name="image"><xsl:value-of select="/site/section[@id='About']/subsection[position()=1]/page[position()=1]/@filename"/>.<xsl:value-of select="/site/section[@id='About']/subsection[position()=1]/page[position()=1]/img/@type"/></xsl:param>
> >
> > (note the two character indent)
> >
> >   This is, in my opinion, a bit too long (but if broken up, would be a real
> > mess and would probably mess up the output too much).
> >
> >   I find 80 to be nice---I can fit several xterms side by side when
> > programming (on my monitor at work---five side by side).
> >
> >   -spc (It's not a hard rule wit me, but a guideline I try to follow)
> >
> > [1]     I was playing around with it about fifteen years ago and decided to
> >         convert my site [2] to XML, and use XSLT to convert it to HTML (and
> >         to generate all links between pages and sections)
> >
> > [2]     http://www.conman.org/
> 
> My preference is to maximize the amount of information on a single
> screen. While 250 characters is very long, picking an arbitrary number
> of characters applicable to computing in 1981 is suboptimal. Your
> example of multiple xterms is one good reason to keep lines short, but
> I can also easily put two windows at 150 characters side by side on a
> reasonable modern monitor. 

  I found this bit of code I wrote [1]:

  window.events = 
  {
    -- Some other methods snipped

    Expose = function(event)
      if event.count == 0 then
        event.window:copy_area(
                  background,
                  gc,
                  background.width,
                  background.height,
                  math.floor((384 - background.width) / 2),
                  400
          )
        event.window:copy_area(
                  church,
                  gc,
                  church.width,
                  church.height,
                  0,
                  0
          )
      end
    end,
  }

  The functions tend to require a large number of parameters [3] (hey, blame
the designers of Xlib) and thusly, I came to this style of formatting for
calling such functions.  Otherwise, the code would look like:

  window.events =
  {
    -- Some other methods snipped

    Expose = function(event)
      if event.count == 0 then
        event.window:copy_area(background,gc,background.width,background.height,math.floor((384 - background.width) / 2),400)
        event.window:copy_area(church,gc,church.width,church.height,0,0)
      end
    end,
  }

which I personally find hard to read.  Also, as several of the functions
require tables, that style also tends to fall out:

	local function refresh_win_over()
	  display:send_event(
	        'ExposureMask',
	        {
	          type   = 'Expose',
	          window = window,
	          x      = 0,
	          y      = 0,
	          width  = window.width * 16,
	          height = window.height * 16,
	        }
	  )
	  display:send_event(
	        'ExposureMask',
	        {
	          type   = 'Expose',
	          window = overview,
	          x      = 0,
	          y      = 0,
	          width  = OS * land.WIDTH,
	          height = OS * land.HEIGHT
	        }
	  )
	end

  This also brings up another style convention I use---the use of "" or ''
for strings.  I tend to use "" for strings that will be displayed to the
user, and '' for strings that are used as enum type data, such as
'ExposureMask' or 'Expose' here.  You can also see my preference for
vertical alignment (again, I find it easier to read and is a hold over from
my days as an assembly language programmer).

> After looking at some of my code, it seems
> my lua lines are very short and my C# lines can get up to about 140
> characters. Either way, I try to be descriptive rather than
> restrictive in my code style. That means longer variable names and
> sometimes putting everything on one line if it makes sense. One of my
> current favorites in Lua is if/then/else on one line, which will
> rarely fit in 80 characters:
> 
> if table.value == "another value" then number1 = number1 + 6 else number1 = 0 end
> 
> (admittedly that looks much nicer in ZeroBrane with color coding).

  I'd write that out on several lines:

	if table.value == 'another value' then
	  number1 = number1 + 6
	else
	  number1 = 0
	end

  But that's me.

  -spc

[1]	It uses a module I wrote that wraps Xlib.  I have no immediate plans
	to release this because 1) it requires you know Xlib to actually use
	it and 2) it uses TCC [2], which is a bit touchy to use.

[2]	Tiny C-Compiler---a C compiler that is also a library you can link
	into an application if you want to compile C code on the fly [4].

[3]	There are actually 8 parameters required for copy_area().  You see
	six specified; the seventh is the implicit window parameter (via the
	object call syntax) and the eight is an upvalue containing the
	current display.

[4]	Don't even ask what I need this for.