Page 1 of 2

Is there a way to group lines of code in Löve?

Posted: Mon Feb 27, 2017 7:10 pm
by nice
So I'm currently using Unity and C# at school a lot so in the recent days I've been pretty spoiled by using regions.

For those of you that doesn't know what region is:

Code: Select all

#region Let's hide our super fun code here
	
	// Totally cool and legit code right here
	
#endregion
By using regions in a project you can group up a lot of code, let's say you have this complex movement code for an enemy but you feel that it's a bit of a pain to scroll through to get to another part of the code, then regions is really good to have.

So does that kind of solution exist?

Re: Is there a way to group lines of code in Löve?

Posted: Mon Feb 27, 2017 7:22 pm
by airstruck
Functions!

No, that's not sarcasm. If your editor does code folding, it probably does it for functions. Maybe do..end blocks would also work, but functions really seem like the right choice (SRP, SoC and all that) unless you have a situation where it would produce too many closures and it's too much trouble to move it up to the "top level" for some reason.

Re: Is there a way to group lines of code in Löve?

Posted: Mon Feb 27, 2017 7:25 pm
by nice
airstruck wrote: Mon Feb 27, 2017 7:22 pm Functions!

No, that's not sarcasm. If your editor does code folding, it probably does it for functions. Maybe do..end blocks would also work, but functions really seem like the right choice (SRP, SoC and all that).
but if there's code inside the function that I want to group?

Re: Is there a way to group lines of code in Löve?

Posted: Mon Feb 27, 2017 7:26 pm
by airstruck
More functions! ;)

It's just my opinion, but this sounds like a sign that your functions are doing too much work. I'd break them up into smaller functions, not try to hide the stuff inside.

Re: Is there a way to group lines of code in Löve?

Posted: Mon Feb 27, 2017 7:30 pm
by nice
airstruck wrote: Mon Feb 27, 2017 7:26 pm More functions! ;)

It's just my opinion, but this sounds like a sign that your functions are doing too much work. I'd break them up into smaller functions, not try to hide the stuff inside.
well you're partly true, right now I'm trying to get back to LÖVE after not using it for approx. 2 years and I've left some code in my current project as a reminder for another time like this:

Code: Select all

	
-- What happens reach a certain threshold
	--[[
	if moveX < 700 then
		moveX = moveX + 100 * dt
	end
	]]

-- Here we're playing around with true/false
-- You can use "if moving == true" but that's overflow
-- For a false statement use "not" after "if".
	--[[
	if not moving then
		moveX = moveX + 100 * dt
	end

	moveX = moveX + 10 * dt
	-- moveY = moveY + 10 * dt
	]]

Re: Is there a way to group lines of code in Löve?

Posted: Mon Feb 27, 2017 7:38 pm
by zorg
So,
#region lets you specify a block of code that you can expand or collapse when using the outlining feature of the Visual Studio Code Editor. In longer code files, it is convenient to be able to collapse or hide one or more regions so that you can focus on the part of the file that you are currently working on.
Things like this are usually not part of any languages, rather it's dependant on the code editor one uses; for example, sublime text 2 can hide blocks, but it's horribly simplistic (it can't hide the closing ends unless i indent them as well).

I really think that embedding such functionalities in a language itself is just meaningless; it's equivalent (from a code perspective) to comments; also see this: http://softwareengineering.stackexchang ... code-smell
That's just my opinion though.

(Oh, and regarding your code snippet, you can just use "if moving then", since it's a boolean, merely putting it there will be evaluated to true... in fact, apart from nil and false, everything evaluates to true in lua)

Re: Is there a way to group lines of code in Löve?

Posted: Mon Feb 27, 2017 7:48 pm
by nice
zorg wrote: Mon Feb 27, 2017 7:38 pm I really think that embedding such functionalities in a language itself is just meaningless; it's equivalent (from a code perspective) to comments; also see this: http://softwareengineering.stackexchang ... code-smell
That's just my opinion though.
Well I like them :C but seriously there were in one project where I used it every where and it looked bad after extensive use, so I set as a rule for myself that I should only use it if I have to.
zorg wrote: Mon Feb 27, 2017 7:38 pm (Oh, and regarding your code snippet, you can just use "if moving then", since it's a boolean, merely putting it there will be evaluated to true... in fact, apart from nil and false, everything evaluates to true in lua)
When you say it, I realize you're right.
I went back and saw my small "mistake", thanks for pointing it out!

Re: Is there a way to group lines of code in Löve?

Posted: Mon Feb 27, 2017 7:48 pm
by airstruck
Hmm, so really you want to fold up a bunch of comments? You could wrap the whole thing in a multiline comment, like this:

Code: Select all

--[==[
    ...other comments here...
--]==]
The thing with the equals signs is a special syntax for block comments that happen to contain ]] (or ]=], or ]==], etc.). Just make sure you use the same number of equals signs at the beginning and end.

With some luck your editor can figure out what's going on here and do the folding properly.

Re: Is there a way to group lines of code in Löve?

Posted: Mon Feb 27, 2017 7:51 pm
by nice
airstruck wrote: Mon Feb 27, 2017 7:48 pm Hmm, so really you want to fold up a bunch of comments? You could wrap the whole thing in a multiline comment, like this:

Code: Select all

--[==[
    ...other comments here...
--]==]
The thing with the equals signs is a special syntax for block comments that happen to contain ]] (or ]=], or ]==], etc.). Just make sure you use the same number of equals signs at the beginning and end.

With some luck your editor can figure out what's going on here and do the folding properly.
Hey thanks for pointing it out but the only downside I see with this is that it ain't pretty..
But nonetheless thanks!

Re: Is there a way to group lines of code in Löve?

Posted: Mon Feb 27, 2017 7:55 pm
by airstruck
nice wrote: Mon Feb 27, 2017 7:51 pmHey thanks for pointing it out but the only downside I see with this is that it ain't pretty..
But nonetheless thanks!
If you want pretty, delete all those comments and call it a day. I doubt you'll miss them much. ;)