Getting double returns when using a function

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1154 Views - Last Post: 18 December 2016 - 09:53 AM Rate Topic: -----

#16 jimblumberg   User is offline

  • member icon

Reputation: 5780
  • View blog
  • Posts: 17,684
  • Joined: 25-December 09

Re: Getting double returns when using a function

Posted 17 December 2016 - 09:29 PM

Quote

They're pretty big functions, and switches.

What exactly do you mean by "pretty big"? To me a "pretty big" function is about 20 to 30 lines. You really should strive to have your functions do as little as possible, one thing is ideal, and have that function do that one thing well. If you write small functions you will have a better chance of reusing that code in more than one program making your job easier and easier as time wears on. Also small functions are much easier to test and troubleshoot when things go wrong.

Quote

Should I create one .cpp file and place all of the function within it,

Normally I only place "related" functions in the same files, I try to keep my files to less than 3 printed pages (printed on Letter sized paper) whenever possible. I usually end up with a lot of small functions contained in quite a few small files. I usually have header files that contain the prototypes for closely related functions that may be contained in one to several files. I also try to keep the User Interface as separate from the program logic as possible so that I can switch UIs and still use the same program logic, when possible.


Jim
Was This Post Helpful? 1
  • +
  • -

#17 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3826
  • View blog
  • Posts: 13,945
  • Joined: 08-August 08

Re: Getting double returns when using a function

Posted 18 December 2016 - 06:58 AM

I want to emphasize what Jim said. Big functions are code smells by themselves. The longer a function is, the more likely that the code is poorly written. I'd say that the probability of it being good code drops dramatically as the code approaches 100 lines. While it's still possible to have decent code with functions over 300 lines, the probability is low, and by the time you've got 500 lines it's almost certainly bad code.

I've had the misfortune of working on code with that has many methods over 1000 lines, some as much as 2000 lines! It's all horrifically bad stuff requiring much effort to make the slightest changes. It creates fear in not only the developers working on it, but in management as well. No one (especially management) wants to attempt refactoring it, so it grows a little with each laborious change, getting worse over time! Believe me, you don't want any part of this.
Was This Post Helpful? 1
  • +
  • -

#18 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7187
  • View blog
  • Posts: 24,357
  • Joined: 05-May 12

Re: Getting double returns when using a function

Posted 18 December 2016 - 08:22 AM

I second that advice to keep functions small.

I too worked on code base that had one C function weighing in at about 2000 lines. It followed the Single Responsibility Principle. And this was a brand new code based written from scratch (e.g. no previous code being ported in). The complete project was at about 12000 LOC with almost all the other functions following our team policy that a function should target to be at 30 lines and never exceed 50 lines. The reason for the huge function was performance. At the time the code was meant to target Windows 3.3, Windows 95, and Windows NT, and performance sucked on the older Windows, and the then under construction Win95 machines when there were too many context switches between small functions. We gained at least 15-20% speed gain by keeping everything in one function. Speed was essential for this function because it was going to be the user's day to day editor when composing email, or using anything else that needed rich text. The code would recalculate all the line wraps and line breaks in real time as the user was typing. To make matters worse, the code was also re-entrant so one could potentially have multiple threads executing it and possibly manipulating adjacent data structures. Although, written in C, it was object oriented, so that meant that we had to build and maintain our own v-tables. Code reviews would have us spreading out multipage print outs of this one function and have 6 of us poring over the code. Debugging and modifying it was a major pain.
Was This Post Helpful? 0
  • +
  • -

#19 jv1597   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 143
  • Joined: 18-October 16

Re: Getting double returns when using a function

Posted 18 December 2016 - 09:53 AM

What a mess. I had the same problem with the switch code I'm working on. I had to rearrange the whole thing every time I wanted to update it. After some minor research, I realized I needed to get a better perspective, and got it organized a bit better. Right now the longest I have is 150-line functions. I'll be breaking it down as much as possible, like Jim suggested. It makes alot of sense.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2