Page 2 of 8

Re: What techniques that everyone should know?

Posted: Sat Feb 15, 2014 10:18 am
by Davidobot
micha wrote:
Davidobot wrote:How to have clean code. Clean code is of outmost importance.
Here's a great book about clean code: http://www.amazon.com/Clean-Code-Handbo ... 0132350882
And of course there are two great videos about it. Part 1 and Part 2.
Yup! I found out about it from there!

Re: What techniques that everyone should know?

Posted: Sat Feb 15, 2014 4:45 pm
by Azhukar
Robin wrote:Personally I prefer to use whatever is idiomatic for that language
What language other than python does this apply to?

Re: What techniques that everyone should know?

Posted: Sat Feb 15, 2014 5:36 pm
by ejmr
Azhukar wrote:What language other than python does this apply to?
Haskell, occam, and FORTRAN from twenty-three years ago.

Re: What techniques that everyone should know?

Posted: Sat Feb 15, 2014 5:47 pm
by Azhukar
ejmr wrote:Haskell, occam, and FORTRAN from twenty-three years ago.
I'm sure he meant those. :awesome:

Re: What techniques that everyone should know?

Posted: Sat Feb 15, 2014 6:33 pm
by Inny
I think the best thing to learn in any language, is to learn that the unit of reuse is the function. Different languages have different ways of bundling and shipping those functions around, but as a principle, you have to use a function to facilitate code reuse. As it applies to Lua, you have a lot of freedoms when it comes to bundling functions, but the best (IMO) is the simple mixin. A table whose only purpose is to serve as a collection of functions to allow other tables or objects to share and reuse the code.

Code: Select all

function mixin(to, from)
  for k, v in pairs(from) do to[k] = v end
end
Or perhaps a better style that's a bit more usable:

Code: Select all

function mixin(self, ...)
  for i = 1, select('#', ...) do
    for k, v in pairs(select(i, ...)) do self[k] = v end
  end
  return self
end
What this ultimately facilitates is that if you design your mixins as simple tables, you can give functionality to any object just through this sharing mechanism. Here's something i wrote recently that demonstrates this, it's a mixin that lets a object house and draw static image.

Code: Select all

local image_component = {

  load_image = function(self, name)
    self._image = love.graphics.newImage(name)
    self._image:setFilter("nearest", "nearest")
  end,

  draw_image = function(self, x, y)
    love.graphics.draw(self._image, x, y)
  end,

  image_width = function(self) return self._image:getWidth() end,
  image_height = function(self) return self._image:getHeight() end,
}
And no point here did I have to talk about metatables when talking about code reuse.

Re: What techniques that everyone should know?

Posted: Sat Feb 15, 2014 9:04 pm
by ejmr
Azhukar wrote:I'm sure he meant those.
Yeah it was not easy to think of languages that apply different semantics based on whitespace, lol.
Inny wrote:I think the best thing to learn in any language, is to learn that the unit of reuse is the function. Different languages have different ways of bundling and shipping those functions around…. As it applies to Lua, you have a lot of freedoms when it comes to bundling functions, but the best (IMO) is the simple mixin. A table whose only purpose is to serve as a collection of functions to allow other tables or objects to share and reuse the code.
I think that’s a great point Inny. Even when writing assembly language I still use labels and such to organize my code in the same way I would with functions in other languages.

Going back to the OP’s original question, I think Inny’s post highlights something important about Lua: *you really, really, really need to be comfortable with tables.* They are ubiquitous. Tables in Lua act as arrays, hash tables, containers, linked lists, graphs—pretty much any non-trivial data structure that you will see in other languages. The book “Programming in Lua” is a useful resource about tables. This page may also help. And make sure to read about the standard library for tables because you will both see and use those functions a lot.

Re: What techniques that everyone should know?

Posted: Sat Feb 15, 2014 9:28 pm
by bartbes
ejmr wrote:
Azhukar wrote:I'm sure he meant those.
Yeah it was not easy to think of languages that apply different semantics based on whitespace, lol.
He did say idiomatic, not significant.

Re: What techniques that everyone should know?

Posted: Sat Feb 15, 2014 9:38 pm
by ejmr
bartbes wrote:
ejmr wrote:Yeah it was not easy to think of languages that apply different semantics based on whitespace, lol.
He did say idiomatic, not significant.
You’re right, but I would say the significant whitespace in all of those languages is the genesis for many idioms found in each.

Re: What techniques that everyone should know?

Posted: Sat Feb 15, 2014 9:55 pm
by Robin
Yeah, but other languages still have idiomatic use of whitespace. That's what I was going for. Nothing to do with the off-side rule.

Re: What techniques that everyone should know?

Posted: Sat Feb 15, 2014 10:19 pm
by ejmr
Can we all agree that Whitespace (the language) has the most idiomatic use of whitespace? Heh.