Least amount of lines of code

Discussion in 'Programmer's Corner' started by nanobyte, May 13, 2010.

  1. nanobyte

    Thread Starter Senior Member

    May 26, 2004
    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?
  2. CVMichael

    AAC Fanatic!

    Aug 3, 2007

    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 ?
  3. nanobyte

    Thread Starter Senior Member

    May 26, 2004
    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.
  4. John P

    AAC Fanatic!

    Oct 14, 2008
    If the language is C, then in your main() routine you could have
    Code ( (Unknown Language)):
    2. ...
    3.    do_nothing();
    4. ...
    and the function could be
    Code ( (Unknown Language)):
    2. void do_nothing(void)
    3. {
    4.   ;                      // Do nothing
    5. }
    Call it a null function. It has zero lines!
  5. someonesdad

    Senior Member

    Jul 7, 2009
    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.
  6. russ_hensel

    Distinguished Member

    Jan 11, 2009
    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.
  7. BMorse

    AAC Fanatic!

    Sep 26, 2009
    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
  8. coldpenguin

    Active Member

    Apr 18, 2010
    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 wouldn't worry too much.