C# Return multiple values from method

ช่วงนี้พยายามจะ Refactor Code เดิมที่ทำขึิน โดยพยายามแตก Code ให้ได้ Method ที่เล็กที่สุด และอยากให้มันทำ Test Coverage ง่ายด้วย ไม่อยากจะเจอภาพแบบนี้อีกแล้ว

พอที่นี้พอจะ Extract Method ดันไปเจอว่า Code มันคำนวณ 2 ค่า ใช้เงื่อนไขเดียวกัน ไม่อยากไปแยก 2 Method ตอนนี้มี 2 ทางเลือก

  • ใช้ ref แต่ส่วนตัวไม่ค่อยชอบ มันทำให้อ่าน Code ยากข้ามไป
  • ทางแรกสร้าง Class ที่ทำหน้าเป็น DTO มาเก็บเลย แต่ทว่าทั้ง Solution จะมี Class เล็กๆพวกนี้เต็มไปหมดเลย แล้วถ้ามันไม่ได้ใช้หลายจุด อย่าเพิ่มเลยดีกว่าครับ
  • C# เอามี Tuple ด้วยนะ แต่จำได้ว่า Syntax มันไม่ค่อยสวย และจำยากด้วย ตัวนี้เลย
public static Tuple<Decimal, Decimal> sumLeftRightSide(Decimal pSumCollateralValueATFC, Decimal pSumUnderlyValueATFC, Decimal pFixedAmount)
{
   Decimal sumAmountRightSide = 0, sumAmountLeftSide = 0;
   if (pSumCollateralValueATFC > 0)
   {
      sumAmountRightSide = pSumUnderlyValueATFC + pFixedAmount;
      sumAmountLeftSide = pSumUnderlyValueATFC - pFixedAmount;
   }
   else
   {
      sumAmountRightSide = pSumUnderlyValueATFC - pFixedAmount;
      sumAmountLeftSide = pSumUnderlyValueATFC + pFixedAmount;
   }
   //Creating an object of Tuple class by calling the static Create method
   Tuple<Decimal, Decimal> t = Tuple.Create(sumAmountRightSide , sumAmountLeftSide );
   //Returning the tuple
   return t;
}

ตอนเรียกใช้ก็ประมาณนี้ครับ แล้ว Access แต่ละ Property เรียกใช้ Item1 / Item2 .... ซึ่งมันไม่สื่อเลย

Tuple<Decimal, Decimal> t=sumLeftRightSide(SBLMarginP.SumCollateralValueATFC
                                         , SBLMarginP.SumUnderlyValueATFC
                                         , SBLMarginP.SBL.FixedAmount);

 Console.WriteLine(t.Item1);
 Console.WriteLine(t.Item2);

แต่ในลองหาข้อมูลใน C# 7 มันเปลี่ยนแล้ว มีตัว named parameters ช่วยให้ใช้งานได้ง่ายมากขึ้นเลย ลองมาปรับ Code กันครับ

public static (Decimal, Decimal) sumLeftRightSide(Decimal pSumCollateralValueATFC, Decimal pSumUnderlyValueATFC, Decimal pFixedAmount)
{
   Decimal sumAmountRightSide = 0, sumAmountLeftSide = 0;
   if (pSumCollateralValueATFC > 0)
   {
      sumAmountRightSide = pSumUnderlyValueATFC + pFixedAmount;
      sumAmountLeftSide = pSumUnderlyValueATFC - pFixedAmount;
   }
   else
   {
      sumAmountRightSide = pSumUnderlyValueATFC - pFixedAmount;
      sumAmountLeftSide = pSumUnderlyValueATFC + pFixedAmount;
   }
   return (sumAmountRightSide, sumAmountLeftSide);
}

ตอนเรียกใช้ก็สามารถใช้งานแบบนี้ได้เลย

(Decimal sumAmtRightSide, Decimal sumAmtLeftSide) =sumLeftRightSide(SBLMarginP.SumCollateralValueATFC
                                                                  , SBLMarginP.SumUnderlyValueATFC
                                                                  , SBLMarginP.SBL.FixedAmount);

 Console.WriteLine(sumAmtRightSide);
 Console.WriteLine(sumAmtLeftSide);

ค่าที่ได้จาก Method sumLeftRightSide จะอยู่ตัวแปร sumAmtRightSide/sumAmtLeftSide เอาไปใช้งานต่อได้เลยครับ ไม่ต้องมาทำเป็น เรียกใช้ Item1 / Item2 .... ซึ่งมันไม่สื่อเลย

Reference


Discover more from naiwaen@DebuggingSoft

Subscribe to get the latest posts to your email.