What will be better between Small functions or big functions?( C# Interview questions)

When we speak about OOP there is a principle called SRP or Single Responsibility principle. This is one of the part of SOLID principles and it says “There should be only one reason for software modules to change”.
Following this principle means breaking our single function into multiple functions leading multiple small functions. Now the discussion point is, is this good or bad?

First let’s talk about its advantages –

  • Easy maintenance – because every function contain single logic and thus easy to understand.
  • Easy to replace – function can be easily replaced with other function with the help of virtual and override keyword. This is also possible when we have one big function but the difference is when we override long big functions there Is a chances of redundant logic.
    Lets have a example –public class MyCalc
    {
                public virtual void Add()
                {
                             Console.WriteLine(“Enter 2 number”);
                             Int x=int.Parse(Console.ReadLine());
                             Int y=int.Parse(Console.ReadLine());
                             Int sum=x+y;
                             Console.WriteLine(“Addition is ”+sum.ToString());
               }
               .
               .
               .
              //Other functions
              .
    }

Now let’s say another client expects this function to behave slightly in different manner. He expects, he should get prompt to enter first number and then for second number and then display the result.

     public class MyNewCalc:MyCalc
     {
                 public virtual void Add()
                 {
                              Console.WriteLine(“Enter 1st number”);
                              Int x=int.Parse(Console.ReadLine());
                              Console.WriteLine(“Enter 2nd number”);
                              Int y=int.Parse(Console.ReadLine());
                              Int sum=x+y;
                              Console.WriteLine(“Addition is ”+sum.ToString());
                }
}

Now if you look at the same code you will realize a fact that code/logic redundancy is a lot.
But in case you would have been defined MyCalc in following way, your life must have been in better place.

            public class MyCalc
            {
                        public virtual void Add()
                        {
                                     Intx;int y;
                                     ReadTwoNumber(out x, out y);
                                     Int sum=GetSum();
                                     DisplaySum(sum);
                                     Int sum=x+y;
                                     Console.WriteLine(“Addition is ”+sum.ToString());
                        }
                        public virtual ReadTwoNumber(out intx,outint y)
                        {
                                     Console.WriteLine(“Enter 2 number”);
                                     x=int.Parse(Console.ReadLine());
                                     y=int.Parse(Console.ReadLine());
                        }
                        public virtual GetSum(intx,int y)
                        {
                                     returnx+y;
                        }
                        public virtual DisplaySum (int sum)
                        {
                                     Console.WriteLine(“Addition is ”+sum.ToString());
                        }
         }

Now it is easy to override because we will override which is required.

First let’s talk about myths about this principle –
Many people think that increasing number of function call also affect performance. Is it true?
I say No, rather I will say it will improve performance. Let see how.

When someone try to call any function, memory will be allocated for all local variables declared inside that function. More is the size of your function is more will be chances of having more local variables declared. It means more memory will be allocated and thus affect performance.
When we break one big function to multiple small functions local variables also get distributed among them making less memory allocation and thus improved performance.

Hope this article was helpful to you. Please feel free to share your comments and thought. Thanks

We are thankful to www.questpond.com to provide this C# Function interview question with answers.

Other interview question site for references :-

Are you a fresher and want to learn C# step by step from scratch start from the below video

Advertisements

About c# and .NET Interview questions

This blog is for developers who want to crack .NET and C# interviews. It has all tips and tricks needed to crack .NET interviews , C# interview , SQL Server interview , Java interview , WCF Interview , Silverlight interview , WPF interview , LINQ interview , Entity framework Interview. Do not forget to watch our Learn step by step video series. Learn MVC in 16 hours:- https://www.youtube.com/watch?v=Lp7nSImO5vk Learn AngularJS Step by Step:- https://www.youtube.com/watch?v=0kmdjqgO9IY Learn Design Pattern in 8 hours:- https://www.youtube.com/watch?v=YDobmucohqk Learn C# and .NET in 60 days:- https://www.youtube.com/watch?v=yh2SrzCkNQA Learn MSBI in 32 hours:- https://www.youtube.com/watch?v=mGPJx3ocFgg Learn SharePoint Step by Step in 8 hours:- https://youtu.be/C2fW76SwJNU
This entry was posted in Uncategorized and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s