Performance Issue..... C#
-
Hi I have to write some functions so i want to confirm whats better... 1. if i write one function with lot of functionalities For exa.
Function Unit()
{
---- some code
.
.
.
}2. if I break down this function in multiple functions and the call them inside the function and these function will be written in business layer. For exa.
Function Unit()
{
---- some codeFun1(arg1,arg2)
Fun2(arg1,arg2)
.
.
.
}Question 2 - is this a good practice (if not what are the drawbacks) or i must use directly Convert.ToInt16(oData). ****I am calling this at so many places
public static short ConvertToShort(object oData)
{
if (oData.ToString() == "")
{
return 0;
}
else
{
return Convert.ToInt16(oData);
}
}Thanks
Dinesh Sharma
-
Hi I have to write some functions so i want to confirm whats better... 1. if i write one function with lot of functionalities For exa.
Function Unit()
{
---- some code
.
.
.
}2. if I break down this function in multiple functions and the call them inside the function and these function will be written in business layer. For exa.
Function Unit()
{
---- some codeFun1(arg1,arg2)
Fun2(arg1,arg2)
.
.
.
}Question 2 - is this a good practice (if not what are the drawbacks) or i must use directly Convert.ToInt16(oData). ****I am calling this at so many places
public static short ConvertToShort(object oData)
{
if (oData.ToString() == "")
{
return 0;
}
else
{
return Convert.ToInt16(oData);
}
}Thanks
Dinesh Sharma
For 1st Q: Regading performace both are almost same but for better managebility it always preferred to split in functions so that every function implement one feature.It's good practice. for 2nd : It'll be better if u use directly Convert.ToInt16(oData).but do use it in Try catch block cause if this function will not be able to convert it in int then it'll throw exception.
Cheers!! Brij
-
For 1st Q: Regading performace both are almost same but for better managebility it always preferred to split in functions so that every function implement one feature.It's good practice. for 2nd : It'll be better if u use directly Convert.ToInt16(oData).but do use it in Try catch block cause if this function will not be able to convert it in int then it'll throw exception.
Cheers!! Brij
Thanks for your reply...but are you sure the breaking of functions will not affect the performance (speed)
Dinesh Sharma
-
Thanks for your reply...but are you sure the breaking of functions will not affect the performance (speed)
Dinesh Sharma
As I said, It is almost same.When we do something there is a tradeoff associated.Breaking it into multiple function will just add very few LOC in if you convert it in Machine language.Those lines won't take significant time.But this will surely help u a lot while managing the code which is more important. Also,You can prioritise and implement as per your requirement.
Cheers!! Brij