I have a relatively long method, and I'm wondering:
- How readable is it?
- Is a method this long okay, or will it be bad later on?
Note: This method enables users to select from a console menu - UpArrow
and DownArrow
change option, and LeftArrow
and RightArrow
change amount.
Here is the method (from here):
public static List<Item> Spend(string[] text,List<Item> items,int currency,out int currencyLeft,string singular=null,string textZero=null){
Initiate(items.Count);
var maxNameLength=items.Max(n=>n.Name.Length);
var sellIsEnabled=items.All(v=>v.Value>=0);
var maxCurrencyLength=Math.Max(items.Aggregate(currency,(i,j)=>j.Value).Length()+text.Take(3).Aggregate(0,(i,j)=>i+j.Length)+
Math.Max(0,(singular=singular??text[1].RemoveLast()).Length-text[1].Length),textZero?.Length??0);
var line=Top+1;
var totalItems=items.Select(i=>new Item(i)).ToList();
while(true){
switch(Input){
case UpArrow:
Option--;
Index--;
break;
case DownArrow:
Option++;
Index++;
break;
case LeftArrow:
if(totalItems[Option].Amount-items[Option].Amount>0){
totalItems[Option].Amount--;
currency+=items[Option].Cost;
} else if(sellIsEnabled&&totalItems[Option].Amount>0){
totalItems[Option].Amount--;
currency+=items[Option].Value;
} else{
Input=ReadKey(true).Key;
continue;
}
break;
case RightArrow:
if(currency-items[Option].Cost>=0){
totalItems[Option].Amount++;
currency-=items[Option].Cost;
} else if(sellIsEnabled&¤cy-items[Option].Value>=0&&totalItems[Option].Amount-items[Option].Amount<0){
totalItems[Option].Amount++;
currency-=items[Option].Value;
} else{
Input=ReadKey(true).Key;
continue;
}
break;
case Enter:
currencyLeft=currency;
return totalItems;
default:
Input=ReadKey(true).Key;
continue;
}
if(Input==LeftArrow||Input==RightArrow){
totalItems[Option].Amount.WriteAt(maxNameLength+3,line,maxLength:totalItems[Option].Amount.Length()+2);
(currency!=0||textZero==null?text[0]+(currency==0?"no":currency.ToString())+(currency==1?singular:text[1])+text[2]:textZero).WriteAt(0,Top+MaxLines+2,maxCurrencyLength);
}
Maths.RMod(ref Option,TotalOptions);
Select.TurnPage();
if(ThePage.Was(Turned)){
var maxAmountLength=totalItems.Max(t=>t.Amount.Length());
var top=Top;
foreach(var i in Indices){
items[i].Name.WriteAt(2,++top,maxNameLength);
totalItems[i].Amount.WriteAt(maxNameLength+3,top,maxAmountLength);
}
for(var i=Indices.Count;i<MaxLines;i++) "".WriteAt(2,++top,1+maxNameLength+maxAmountLength);
}
if(Input==UpArrow||Input==DownArrow){
' '.WriteAt(Left,line);
'>'.WriteAt(Left,line=Index+Top+1);
}
Input=ReadKey(true).Key;
PagePlus=0;
}
}