Should global functions be static?

[b][red]This message was edited by bilderbikkel at 2006-10-31 7:40:46[/red][/b][hr]
[red]Edit: Fixed code tag[/red]

Hi everybody,

I found some code in which all global functions were static. IMHO this is strange/unneccesary/redundant.

Does anybody know an advantage/disadvantage of doing this? In my entire book collection I have never found this idiom (except in C# books).

Here is give an example:

[code]
#include
static void sayHello() [red]//is the use of 'static' unneccesary and/or redundant?[/red]
{
std::cout << "Hello" << std::endl;
}

int main()
{
sayHello();
}
[/code]

Thanks for your opinions in advance,
bilderbikkel


Comments

  • [b][red]This message was edited by stober at 2006-10-31 8:6:21[/red][/b][hr]
    : Hi everybody,
    :
    : I found some code in which all global functions were static. IMHO this is strange/unneccesary/redundant.
    :
    : Does anybody know an advantage/disadvantage of doing this? :


    Used in this context the static keyword is used to limit the scope of the object or function to the file in which it is declared. Other *.c or *.cpp files can not access it. There are occasions where doing that is desirable -- to insure the function can not be called from outside the file in which it is declared -- even accidentally. If the program consists of only one file then there is no point in declaring globals static.


  • : [b][red]This message was edited by stober at 2006-10-31 8:6:21[/red][/b][hr]
    : : Hi everybody,
    : :
    : : I found some code in which all global functions were static. IMHO this is strange/unneccesary/redundant.
    : :
    : : Does anybody know an advantage/disadvantage of doing this? :
    :
    :
    : Used in this context the static keyword is used to limit the scope of the object or function to the file in which it is declared. Other *.c or *.cpp files can not access it. There are occasions where doing that is desirable -- to insure the function can not be called from outside the file in which it is declared -- even accidentally. If the program consists of only one file then there is no point in declaring globals static.
    :
    :


    Yep. It is good practice to declare functions static in order to get encapsulation. For the very same reason as why you put private member functions of a class below "private" rather than "public".

    Same goes for global variables only used in one file. That is the C way of providing encapsulation for your code modules. Not as pretty as in C++ but it gives more or less the same result in the end.

  • : : [b][red]This message was edited by stober at 2006-10-31 8:6:21[/red][/b][hr]
    : : : Hi everybody,
    : : :
    : : : I found some code in which all global functions were static. IMHO this is strange/unneccesary/redundant.
    : : :
    : : : Does anybody know an advantage/disadvantage of doing this? :
    : :
    : :
    : : Used in this context the static keyword is used to limit the scope of the object or function to the file in which it is declared. Other *.c or *.cpp files can not access it. There are occasions where doing that is desirable -- to insure the function can not be called from outside the file in which it is declared -- even accidentally. If the program consists of only one file then there is no point in declaring globals static.
    : :
    : :
    :
    :
    : Yep. It is good practice to declare functions static in order to get encapsulation. For the very same reason as why you put private member functions of a class below "private" rather than "public".
    :
    : Same goes for global variables only used in one file. That is the C way of providing encapsulation for your code modules. Not as pretty as in C++ but it gives more or less the same result in the end.
    :
    :
    If you declare a global variable as static in VC++ then it doesn't appear in the class viewer window, which means you can't just double click on the variable there to be taken to its declaration in the code. Obviously this doesn't make any difference to hwo your code compiles, but if you're used to shortcuts provided by the IDE then it seems inconvenient.
  • [b][red]This message was edited by Lundin at 2006-11-2 0:30:44[/red][/b][hr]
    : : : [b][red]This message was edited by stober at 2006-10-31 8:6:21[/red][/b][hr]
    : : : : Hi everybody,
    : : : :
    : : : : I found some code in which all global functions were static. IMHO this is strange/unneccesary/redundant.
    : : : :
    : : : : Does anybody know an advantage/disadvantage of doing this? :
    : : :
    : : :
    : : : Used in this context the static keyword is used to limit the scope of the object or function to the file in which it is declared. Other *.c or *.cpp files can not access it. There are occasions where doing that is desirable -- to insure the function can not be called from outside the file in which it is declared -- even accidentally. If the program consists of only one file then there is no point in declaring globals static.
    : : :
    : : :
    : :
    : :
    : : Yep. It is good practice to declare functions static in order to get encapsulation. For the very same reason as why you put private member functions of a class below "private" rather than "public".
    : :
    : : Same goes for global variables only used in one file. That is the C way of providing encapsulation for your code modules. Not as pretty as in C++ but it gives more or less the same result in the end.
    : :
    : :
    : If you declare a global variable as static in VC++ then it doesn't appear in the class viewer window, which means you can't just double click on the variable there to be taken to its declaration in the code. Obviously this doesn't make any difference to hwo your code compiles, but if you're used to shortcuts provided by the IDE then it seems inconvenient.
    :


    Good programming practice should always be put in first place, the programmer's laziness doesn't seem like something that should get precedence. If you aren't content with your IDE, get a new one. It seems odd that Visual Studio doesn't support the feature you mention though, I know several way less sofisticated IDE's that have it.


  • : [b][red]This message was edited by Lundin at 2006-11-2 0:30:44[/red][/b][hr]
    : : : : [b][red]This message was edited by stober at 2006-10-31 8:6:21[/red][/b][hr]
    : : : : : Hi everybody,
    : : : : :
    : : : : : I found some code in which all global functions were static. IMHO this is strange/unneccesary/redundant.
    : : : : :
    : : : : : Does anybody know an advantage/disadvantage of doing this? :
    : : : :
    : : : :
    : : : : Used in this context the static keyword is used to limit the scope of the object or function to the file in which it is declared. Other *.c or *.cpp files can not access it. There are occasions where doing that is desirable -- to insure the function can not be called from outside the file in which it is declared -- even accidentally. If the program consists of only one file then there is no point in declaring globals static.
    : : : :
    : : : :
    : : :
    : : :
    : : : Yep. It is good practice to declare functions static in order to get encapsulation. For the very same reason as why you put private member functions of a class below "private" rather than "public".
    : : :
    : : : Same goes for global variables only used in one file. That is the C way of providing encapsulation for your code modules. Not as pretty as in C++ but it gives more or less the same result in the end.
    : : :
    : : :
    : : If you declare a global variable as static in VC++ then it doesn't appear in the class viewer window, which means you can't just double click on the variable there to be taken to its declaration in the code. Obviously this doesn't make any difference to hwo your code compiles, but if you're used to shortcuts provided by the IDE then it seems inconvenient.
    : :
    :
    :
    : Good programming practice should always be put in first place, the programmer's laziness doesn't seem like something that should get precedence. If you aren't content with your IDE, get a new one. It seems odd that Visual Studio doesn't support the feature you mention though, I know several way less sofisticated IDE's that have it.
    :
    :
    :
    I don't think it's a case of VC++ not supporting it, rather it is Microsoft's opinion that static variables should not be displayed in the class viewer. I think you're right though; good programming should take precedence over laziness.

  • Thanks everybody for your helpful comments!
    bilderbikkel

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories