Least amount of lines of code

Thread Starter

nanobyte

Joined May 26, 2004
120
Out of curosity.....What is the minimum lines of code a programming function/subtroutine can have? Is there a written or unwritten rule to this?
 

CVMichael

Joined Aug 3, 2007
419
LOL...

No, there are no limits.

But maybe it depends on the programming language, but you did not mention about any programming language...

Why do you ask ? did you run into some kind of problems ?
 

Thread Starter

nanobyte

Joined May 26, 2004
120
No problems, but while currently in the midst of writing a problem I am finding myself confronting the issue of possible creating one line functions. This is something I want to avoid doing.
 

John P

Joined Oct 14, 2008
2,025
If the language is C, then in your main() routine you could have
Rich (BB code):
...
   do_nothing();
...
and the function could be
Rich (BB code):
void do_nothing(void)
{
  ;                      // Do nothing
}
Call it a null function. It has zero lines!
 

someonesdad

Joined Jul 7, 2009
1,583
There is absolutely nothing wrong with creating functions with few lines. A one line function is just fine too. In fact, I'll often design a program and when I start writing it, I put in all the functions and they're initially empty. This helps me decide if I like the interfaces I've designed. Then I start fleshing them out once I like what I see.

Inexperienced programmers will wail about the inefficiency of setting up the call stack, making the call, and cleaning up the stack to return. Ignore these wails like the plague and remember the famous quote (by Knuth IIRC) "premature optimization is the root of all evil". You should NEVER* worry about performance until you know your code is correct first. Another quote: "It doesn't matter how fast you get the wrong answer."

Besides, your focus should be on designing good algorithms and data structures. This does more for speed and efficiency than any optimization every will.

Use functions where you feel they are needed. Unless you're doing toy programming, you're gonna need to refactor later anyway (if you're wise). Functions make it easier to understand your code -- just make sure you use appropriate names.

* There are some places where you do worry about performance first, but these tasks are rarely handled by beginning or intermediate programmers.
 

russ_hensel

Joined Jan 11, 2009
825
If a subroutine turns out to be "to short" you can, in some languages, declare it to be "inline" thus eliminating the linkage to and from the subroutine. Some compilers may be smart enough to do this themselves? It is true that premature optimization is a problem, beware.
 

BMorse

Joined Sep 26, 2009
2,675
Unless it is an ISR (Interrupt Service Routine) then it should be as short as possible especially if using multivectored interupts, spending too much time in 1 ISR can cause your program to miss another, which could lead to greater problems.....

B. Morse
 

coldpenguin

Joined Apr 18, 2010
165
What I have seen in a couple of programs recently, is people who thought their functions were too short, and instead replaced them with multi-line defines instead.
Although this did save a call statement, a good compiler would have in-lined them anyway.
Unless you are writing an algorithm to determine something like the first few thousand prime numbers, and are writing functions called, increment:

void increment( int &i){
i++;
}

I wouldn't worry too much.
 
Top