RabbitEar
RabbitEar

Reputation: 101

Benefits (and drawbacks) of C# nested method calls

I'm getting back into coding in C#, after being neck deep in Objective-C. And I was wondering, in C# are there any particular benefits or drawbacks to writing your code with nested method calls, in terms of performance, memory overhead, and code readability & maintenance? Or is it better to go with easier to read and/or easier to follow code, over any possible speed or memory benefits (the memory benefits part can be the ObjC/non-managed code talking)?

Example (x, y, z, w, t, e & g are all int's):

// non-nested
// someMethod returns an int

int b = someMethod(w,t,e);
int a = ((x + y) * b);
int c = a + (b * g);
return a + b + c;


// nested
// someMethod returns an int
return (((x + y) * someMethod(w,t,e)))
+ (someMethod(w,t,e)) + (((x + y) * someMethod(w,t,e))
+ (someMethod(w,t,e) * g));

It's easier to follow the non-nested code, but in the nested code you're not having to instantiate the three variables; it's also four lines of code to one line of code (albeit the one line is broken up into three lines for the sake of readability). I'm just not sure which way is better, or more accepted within the C# programming community. Any insight would be much appreciated. Thanks!

Upvotes: 3

Views: 1202

Answers (3)

naed21
naed21

Reputation: 155

It's good coding practice to separate everything into different methods for a number of reasons.

  1. Like you've mentioned it is easier to read, thus if your code is ever passed on, or if you revisit it after a while you can quickly understand what is happening.
  2. You can make code that is reusable everywhere. Which can cut down on coding time in larger projects
  3. It makes debugging easier because you can isolate problems quicker.

Of course there is more overhead, but as for speed and memory, I haven't ever tested it but I'm sure you may be losing a bit. Although considering the kind of computers people are running today I wouldn't consider it noticeable.

I've made plenty of small projects where I just toss out code as quickly as possible, but once I've started making headway or my program starts to get a little complex, I always go back and re-organize everything into methods.

It keeps everything simple, clean, and readable.

Upvotes: 3

Serj Sagan
Serj Sagan

Reputation: 30218

Readability is almost always better. Check out a book called Clean Coding. In your example there will be 0 additional memory used, once the code is built.

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726809

If someMethod(w,t,e) does something non-trivial, calling it three times will be slower than making a single call and storing the result.

Note that the two expressions are equivalent only if the function someMethod(w,t,e) is a "pure" function, meaning that it is free of side effects, and that it returns the same value for the same set of arguments.

During the initial design and coding you should strive for the best readability. Considering the overhead of method calls before profiling is a premature optimization.

Upvotes: 3

Related Questions