Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

When to declare your variables

Name: Anonymous 2009-09-03 8:36

I was reading 'Beej's guide to Network Programming' again this morning, and I noticed he declares all his variables at the start of his function.[sub]Ex. 1[/sup] Does anyone else consider this hideous style? Or does it pose some advantages? I can well imagine that it would be easy to check your variables are initialised, but not much else. Personally, I much prefer to declare my variables as and when I use them a la dynamic languages.

[sub][sub]--
1. <snipped>
int main(void)
{
    fd_set master;    // master file descriptor list
    fd_set read_fds;  // temp file descriptor list for select()
    int fdmax;        // maximum file descriptor number

    int listener;     // listening socket descriptor
    int newfd;        // newly accept()ed socket descriptor
    struct sockaddr_storage remoteaddr; // client address
    socklen_t addrlen;

    char buf[256];    // buffer for client data
    int nbytes;

    char remoteIP[INET6_ADDRSTRLEN];

    int yes=1;        // for setsockopt() SO_REUSEADDR, below
    int i, j, rv;

    struct addrinfo hints, *ai, *p;

    FD_ZERO(&master);    // clear the master and temp sets
    FD_ZERO(&read_fds);

    // get us a socket and bind it
    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;
    if ((rv = getaddrinfo(NULL, PORT, &hints, &ai)) != 0) {
        fprintf(stderr, "selectserver: %s\n", gai_strerror(rv));
        exit(1);
    }

   <snipped>

Name: Anonymous 2009-09-03 8:43

C requires this, or at least used to require it until C99. Yes, it is usually considered bad style, since it makes it hard to find out in what code a variable is relevant. Declaring your variables as late as possible (typically, when they first get their value) is a Good Thing (tm).

Name: Anonymous 2009-09-03 8:47

In C I declare my vars as late as possible while still keeping them at the beginning of a scope. Maybe I overuse C scopes....

Name: Anonymous 2009-09-03 8:48

>>1
You need a c99 compliant compiler for that.
Curiosly enough, that means giving "-std=gnu99", not "-std=c99" to gcc, because otherwise a lot of functions (including snprintf if memory serves me) become undefined.

Name: Anonymous 2009-09-03 8:49

C requires this, or at least used to
I should've expected that to be the case, C really was designed more for compilers than humans

Name: Anonymous 2009-09-03 9:03

>>5
It's not quite that bad when you compare it to pascal, which doesn't even have variables for inner blocks.

Name: Anonymous 2009-09-03 9:19

>>4
Try including the right headers next time, bro. Hint: man snprintf.

Name: Anonymous 2009-09-03 9:29

>>5
Try refactoring some time.

Name: Anonymous 2009-09-03 14:16

>>1
When you've got everything declared in the beginning of the function you can estimate stack usage more easily just by looking at the code. It's a valid but rather obsolete reason nowadays and doesn't make it any less hideous.

Name: Anonymous 2009-09-03 16:04

>>2
What C89 requires is declaration of identifiers in a block before code. That means int foo(void) { int bar; printf("Hello\n"); { int baz; } return 0; } is valid. (you can even shadow a name: {int bar = 1; {int bar = 2; /* here bar == 2 */ } /* here == 1 */}

Name: Anonymous 2009-09-03 16:27

I actually prefer to keep all my variables up front, just to ensure that I'm not using too many of them. Functions shouldn't be more than 25-50 lines, anyways, so I find it hard to "lose" variables within a function.

Name: Anonymous 2009-09-03 16:42

>>11

Functions shouldn't be more than 25-50 lines

I hate this nonsense and the morons who keep propagating it. Functions should be as long as it reasonably takes to describe atomic tasks. Arbitrary maximum line counts serve no purpose.

Name: Anonymous 2009-09-03 16:57

I put all my variables after my #include statements.

Name: Anonymous 2009-09-03 17:15

Variables are for fags.

l2stack.

Name: Anonymous 2009-09-03 18:23

Variables should be declared when they're defined. Since they shouldn't be redefined. If you're trying to redefine a variable, you need more functions.

Name: Anonymous 2009-09-03 20:05

>>12
Notice how I wrote "should", not "must." Of course some functions can't help but be longer, but sometimes those long ones can be factored because one of the internal steps is useful for other reasons, which can help bring the line count down.

Name: Anonymous 2009-09-04 5:22

>>12
50 lines is a whole lisp program. Obviously lines are exponentially decreasing with expressiveness and the rule applies for /code/, not /data/ in the code. If you don't understand the subtle (and imaginary) dinstiction, consider:


switch(foo) {
  /* 10 cases */
}

Name: Anonymous 2010-12-09 23:17

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List