Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good to use functions as much as possible?

Tags:

c

function

linux

When I read open source codes (Linux C codes), I see a lot functions are used instead of performing all operations on the main(), for example:

int main(void ){
    function1();
    return 0;
}

void function() {
    // do something
    function2();
}

void function2(){
    function3();
    //do something
    function4();
}

void function3(){
    //do something
}
void function4(){
    //do something
}

Could you tell me what are the pros and cons of using functions as much as possible?

  • easy to add/remove functions (or new operations)
  • readability of the code
  • source efficiency(?) as the variables in the functions will be destroyed (unless dynamic allocation is done)
  • would the nested function slow the code flow?
like image 846
Johan Elmander Avatar asked Jun 27 '13 15:06

Johan Elmander


People also ask

What is a good reason for using functions?

A function is almost like a mini-program that we can write separately from the main program, without having to think about the rest of the program while we write it. This allows us to reduce a complicated program into smaller, more manageable chunks, which reduces the overall complexity of our program.

Is it better to have many small functions?

If you have a bunch of small functions, they can reduce the coherency of the functions that call them, by breaking up the task too far. They can force you to constantly look away from the function you're interested in to go read some little one line function to understand what is going on.

Can you have too many functions Python?

Python has a "recursion" limit. If you hit that, then you're probably using too many functions, otherwise it's probably not a big deal -- Usually you can only hit the recursion limit if you're calling a function recursively (and then usually because you did something wrong and didn't break when you should have).

How many things should a function do?

As you've probably read elsewhere, a function should do one thing, and only one thing. “Functions should do something, or answer something, but not both.” As he states, a function should change the state of an object, or return some information about that object, but not both.


2 Answers

  • Easy to add/remove functions (or new operations)

Definitely - it's also easy to see where does the context for an operation start/finish. It's much easier to see that way than by some arbitrary range of lines in the source.

  • Readability of the code

You can overdo it. There are cases where having a function or not having it does not make a difference in linecount, but does in readability - and it depends on a person whether it's positive or not.

For example, if you did lots of set-bit operations, would you make:

some_variable = some_variable | (1 << bit_position)

a function? Would it help?

  • Source efficiency(?) due to the variables in the functions being destroyed (unless dynamic allocation is done)

If the source is reasonable (as in, you're not reusing variable names past their real context), then it shouldn't matter. Compiler should know exactly where the value usage stops and where it can be ignored / destroyed.

  • Would the nested function slow the code flow?

In some cases where address aliasing cannot be properly determined it could. But it shouldn't matter in practice in most programs. By the time it starts to matter, you're probably going to be going through your application with a profiler and spotting problematic hotspots anyway.

Compilers are quite good these days at inlining functions though. You can trust them to do at least a decent job at getting rid of all cases where calling overhead is comparable to function length itself. (and many other cases)

like image 60
viraptor Avatar answered Sep 19 '22 15:09

viraptor


This practice of using functions is really important as the amount of code you write increases. This practice of separating out to functions improves code hygiene and makes it easier to read. I read somewhere that there really is no point of code if it is only readable by you only (in some situations that is okay I'm assuming). If you want your code to live on, it must be maintainable and maintainability is one created by creating functions in the simplest sense possible. Also imagine where your code-base exceeds well over 100k lines. This is quite common and imagine having that all in the main function. That would be an absolute nightmare to maintain. Dividing the code into function helps create degrees of separability so many developers can work on different parts of the code-base. So basically short answer is yes, it is good to use functions when necessary.

like image 25
StevenTsooo Avatar answered Sep 16 '22 15:09

StevenTsooo