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 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.

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