Good day!
Usually I try to make my code more compact and I formatting similar code parts to the procedures and functions.
But in some cases I see that with my programming skills this refactoring can make my code more difficult.
I will show typical case from my project. This procedure move selected items from inventory to the equipment and and vice versa.
procedure THeroItemsInterface.SortItems(Sender : TObject);
var
I : Integer;
begin
if Length(Inventory) > 0 then
begin
for I := 0 to High(Inventory) do
if Inventory[I].Button.Pressed then
begin
SetLength(fHero.Equipped.Items, Length(fHero.Equipped.Items)+1);
fHero.Equipped.Items[High(fHero.Equipped.Items)] := FHero.Inventory.Items[I].CopyItem();
end;
for I := High(Inventory) downto 0 do
if Inventory[I].Button.Pressed then
FHero.Inventory.DelItem(I);
end;
if Length(Equipped) > 0 then
begin
for I := 0 to High(Equipped) do
if Equipped[I].Button.Pressed then
begin
SetLength(fHero.Inventory.Items, Length(fHero.Inventory.Items)+1);
fHero.Inventory.Items[High(fHero.Inventory.Items)] := FHero.Equipped.Items[I].CopyItem();
end;
for I := High(Equipped) downto 0 do
if Equipped[I].Button.Pressed then
FHero.Equipped.DelItem(I);
end;
Redraw;
end;
In the one hand, I can make additional procedure with 2 parameters: GUI of the inventory and real inventory bounded with this GUI… But GUI is just array of items interfaces and I need to create new type to use them as parameter of procedure.
So I think the current code huge and some ugly, but it have more clear logic for me then potential code after refactoring.
What you think about situation like this? I think this problem seems little, but it very typical for me.
(You can also say about another problems of this procedure and my coding ).
Thanks!