## Help me decide what to do next?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

### Re: Help me decide what to do next?

On a somewhat related note, what are your thoughts on method arguments vs. using instance variables? To clarify, which of these would you prefer:

Code: Select all

Class DerpFactory:
def __init__(goal):
self.goal = goal

def produceDerps(goal):
print ("Derp!\n"*goal)

Code: Select all

Class DerpFactory:
def __init__(goal):
self.goal = goal

def produceDerps():
print ("Derp!\n"*self.goal)

kikito
Inner party member
Posts: 3139
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

### Re: Help me decide what to do next?

nevon wrote:On a somewhat related note, what are your thoughts on method arguments vs. using instance variables? To clarify, which of these would you prefer:

Code: Select all

(stuff)
It depends.

Classes, like functions, should be small, and do one and only one thing, whenever possible.

This means that you can't usually have lots of instance variables to begin with. On the other hand, you want to minimize the number of parameters you pass through.

Well, in my case, the decision of instance variable vs parameter depends on how many times is it used in a class. If a param is used in more than one method, I consider very seriously transforming it into an instance variable. If this contradicts other methods, or if I end up with lots of instance variables, then I consider splitting up my class into smaller ones (probably grouping the methods by the instance variables/parameters they use).

Similarly, if an instance variable is only used in one method, then I think about making it a parameter, or I try to get by without that parameter.

It's more usual for me to transform a parameter into an instance variable than the other way around, but that has happened too.
When I write def I mean function.

Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

### Re: Help me decide what to do next?

nevon wrote:On a somewhat related note, what are your thoughts on method arguments vs. using instance variables? To clarify, which of these would you prefer:

Code: Select all

(stuff)
You forgot the self arguments, man.

kraftman
Party member
Posts: 277
Joined: Sat May 14, 2011 10:18 am

### Re: Help me decide what to do next?

Nice explanation, thanks
I think I was thinking of functions in the wrong way. In my mind one function that does all of the things it needs to do is less bulky then splitting it into loads of different functions.

I learnt Lua through WoW so some of the first example code i saw had functions like this:

Code: Select all

 function TradeSkillFrame_SetSelection(id)
local skillName, skillType, numAvailable, isExpanded, altVerb = GetTradeSkillInfo(id);
local creatable = 1;
if ( not skillName ) then
creatable = nil;
end
TradeSkillGuildFrame.queriedSkill = nil;		-- always cancel any pending queries
if ( skillType == "header" ) then
if ( isExpanded ) then
else
end
return;
end
return;
end

if ( not cooldown ) then
elseif ( not isDayCooldown ) then
elseif ( cooldown > 60 * 60 * 24 ) then	--Cooldown is greater than 1 day.
TradeSkillSkillCooldown:SetText(COOLDOWN_REMAINING.." "..SecondsToTime(cooldown, true, false, 1, true));
else
end

if ( maxMade > 1 ) then
else
end
if ( TradeSkillSkillIconCount:GetWidth() > 39 ) then
end
else
end

-- Reagents
if(numReagents > 0) then
else
end
for i=1, numReagents, 1 do
local reagentName, reagentTexture, reagentCount, playerReagentCount = GetTradeSkillReagentInfo(id, i);
if ( not reagentName or not reagentTexture ) then
reagent:Hide();
else
reagent:Show();
SetItemButtonTexture(reagent, reagentTexture);
name:SetText(reagentName);
-- Grayout items
if ( playerReagentCount < reagentCount ) then
SetItemButtonTextureVertexColor(reagent, 0.5, 0.5, 0.5);
name:SetTextColor(GRAY_FONT_COLOR.r, GRAY_FONT_COLOR.g, GRAY_FONT_COLOR.b);
creatable = nil;
else
SetItemButtonTextureVertexColor(reagent, 1.0, 1.0, 1.0);
name:SetTextColor(HIGHLIGHT_FONT_COLOR.r, HIGHLIGHT_FONT_COLOR.g, HIGHLIGHT_FONT_COLOR.b);
end
if ( playerReagentCount >= 100 ) then
playerReagentCount = "*";
end
count:SetText(playerReagentCount.." /"..reagentCount);
end
end
-- Place reagent label
local reagentToAnchorTo = numReagents;
if ( (numReagents > 0) and (mod(numReagents, 2) == 0) ) then
reagentToAnchorTo = reagentToAnchorTo - 1;
end

for i=numReagents + 1, MAX_TRADE_SKILL_REAGENTS, 1 do
end

if ( spellFocus ) then
else
end

if ( creatable ) then
else
end

else
end
-- Reset the number of items to be created

local skillLineName, skillLineRank, skillLineMaxRank, skillLineModifier = GetTradeSkillLine();
local color;

--Hide inapplicable buttons if we are inspecting. Otherwise show them
-- highlight color
-- title
-- bottom bar
if ( GetTradeSkillSelectionIndex() > 0 ) then
else
end
-- status bar
else
-- highlight color
-- title
-- bottom bar
-- status bar
if ( skillLineModifier > 0 ) then
else
end

if IsTrialAccount() then
local _, _, profCap = GetRestrictedAccountData();
if skillLineRank >= profCap then
text = text.." "..RED_FONT_COLOR_CODE..TRIAL_CAPPED
end
end

else
--Change button names and show/hide them depending on if this tradeskill creates an item or casts something
if ( not altVerb ) then
--Its an item with 'Create'
else
--Its something else
end
else
end
end
end
if ( color ) then
end
end

kikito
Inner party member
Posts: 3139
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

### Re: Help me decide what to do next?

kraftman wrote:Nice explanation, thanks
I think I was thinking of functions in the wrong way. In my mind one function that does all of the things it needs to do is less bulky then splitting it into loads of different functions.

Code: Select all

(stuff)
I mean no disrespect, but that was ... bad.

"Bulky" is the linchpin here. True, separating into more functions yields usually more lines of code. More lines of code increase the complexity. But if those lines of code look like English and less than just symbols, the complexity is reduced.

The complexity you "gain" by adding lines of code is much smaller than the one you gain by keeping everything in a single, big function.
When I write def I mean function.

thelinx
The Strongest
Posts: 844
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

### Re: Help me decide what to do next?

Robin wrote: You forgot the self arguments, man.
That's a python class definition, where functions with self arguments are implied. Right?

kikito
Inner party member
Posts: 3139
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

### Re: Help me decide what to do next?

thelinx wrote:
Robin wrote: You forgot the self arguments, man.
That's a python class definition, where functions with self arguments are implied. Right?
Python's self is explicit. But I got the point anyway.
When I write def I mean function.

lesslucid
Prole
Posts: 20
Joined: Sun Mar 20, 2011 8:25 am

### Re: Help me decide what to do next?

kikito wrote:
The problem lesslucid has is that his functions look a lot like that one (or are even more complex).
Is it really that bad, I thought? Then I check... I have an updateBall function with almost 100 lines!

...this gives me an idea of a gentle way back into coding; I will try to move all of that functionality out into smaller functions and then call all those functions from within updateBall...

Thanks! I really appreciate all the helpful discussion and criticism.

nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

### Re: Help me decide what to do next?

thelinx wrote:
Robin wrote: You forgot the self arguments, man.
That's a python class definition, where functions with self arguments are implied. Right?
No, that was just a mess-up on my part. I haven't really written any Python in ages, but I still find the syntax to be extremely readable (and hence good for examples like this).

BlackBulletIV
Inner party member
Posts: 1260
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

### Re: Help me decide what to do next?

kraftman wrote:In my mind one function that does all of the things it needs to do is less bulky then splitting it into loads of different functions.
*Huge sigh*

### Who is online

Users browsing this forum: No registered users and 8 guests