Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you declare your module specific functions as static?

I am thinking it is a best practice to declare them as static, as it makes them invisible outside of the module.

What are your thoughts on this?

like image 527
EvilTeach Avatar asked Nov 24 '08 16:11

EvilTeach


People also ask

Can we declare a function as static?

A function can be declared as static function by placing the static keyword before the function name. Now, if the above code is compiled then an error is obtained i.e “undefined reference to staticFunc()”.

Why do we declare a function as static?

Unlike global functions in C, access to static functions is restricted to the file where they are declared. Therefore, when we want to restrict access to functions, we make them static. Another reason for making functions static can be reuse of the same function name in other files.

Should all functions be static?

Absolutely yes. The non-static member functions are meant to cater the non-static member variables. If variables are not used then the function should be made static which makes your design cleaner and you can avoid passing this as the 1st hidden argument (which betters the performance a little). Save this answer.

How do you use a static function?

Static Function MembersBy declaring a function member as static, you make it independent of any particular object of the class. A static member function can be called even if no objects of the class exist and the static functions are accessed using only the class name and the scope resolution operator ::.


4 Answers

For C++, a better than static is to put it in an unnamed (anonymous) namespace. This is the preferred way to prevent pollution of the Global namespace.

namespace {
void myLocalFunction() {
// stuff
}
}
like image 154
KeithB Avatar answered Sep 28 '22 16:09

KeithB


If it is truly an function which is internal only to that .c file, then yes. It should help avoid polluting the global namespace. Also, I think that the compiler is able to do some optimizations with calling conventions if the function is static since it knowns no other source file needs to know how to call it. This only really applies to c because as others have noted, c++ has namespaces to address this issue.

like image 34
Evan Teran Avatar answered Sep 28 '22 18:09

Evan Teran


There was a lot about implementation details and not too much about concept.

Limiting the scope of variable/function etc.. is a good practice indeed. This is a basic concept of object oriented design - you want keep private as private. This way your interface is cleaner and code maintenance is easier. And you will not find one day that changing something that you considered as private broke compilation because somebody in another part of project liked your function and decided to use it.

like image 35
Ilya Avatar answered Sep 28 '22 17:09

Ilya


In C, I make everything - functions and variables - static at file scope until I can demonstrate they're necessary outside the file. I'll make things static within a function if only that function will use them and they are not too huge. Basically, if the declaration is bigger than the rest of the function, I may put the declaration outside the function. And, of course, there's a header for the public services provided by a source file.

like image 24
Jonathan Leffler Avatar answered Sep 28 '22 18:09

Jonathan Leffler