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

Code Style

Name: Anonymous 2010-01-24 16:09

The following program demonstrates your tab width, indentation style, and various other aspects of your code style. Please write it (in verbatim) in your preferred style. Remember to use spaces instead of tabs, as shitchan automatically converts tabs into 3 space characters.

[code]#include "stdio.h"

int main (void)
{   int   x;
    char  c = 'a';
    float y = 0;
   
    for (x = 0; x < 10; x++)
    {   if  (x % 2 == 0)
            printf ("%d\n", x);
        else
        {   printf ("%c\n", c);
            c +=1;
        }
    }
   
    return 0;
}

Name: Anonymous 2010-01-26 16:03

Why waste space?

#include "stdio.h"

int main (void) {
    int   x;
    char  c = 'a';
    float y = 0;
  
    for (x = 0; x < 10; x++) {
        if  (x % 2 == 0) {
            printf ("%d\n", x); }
        else {
            printf ("%c\n", c);
            c +=1; } }
  
    return 0; }

Name: Anonymous 2010-01-26 16:35

int                                                            main   (                              void                                                                                       )                                                                     {
                                                                                                                                                                                             int                                                                                                                                                                                                                  x                                                         ;
                                                                                                                                                                                                                                                                                                    char                                                                                                                                                                 c                                                                          =        'a'                                                                          ;
                                                                                                                                                                                                                                                                                              float     y                                                                                =                                                                                                   0                                                                                         ;
                                                                                                                                                
                                                                                                                                                                                                                                                                                                                           for                                                                (                                                                  x                  =                                 0                                                                                            ;                                                                       x                                                   <                                                                                10                                                                 ;                                                     x++                                                        )                             {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if                                                                                                                                     (                                    x                                         %                                                                          2                                                                                           ==                                                                         0                                                                 )                                                                                               {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        printf                                                                                  (                                       "%d\n"                ,                                                                         x           )                                                                             ;   }
                                                                                                                                                                                                                                                                                                                                                                                   else          {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               printf                                                                    (    "%c\n"                   ,                       c                                                                                                   )                                                                                  ;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  c                      +=          1     ;                                       }                                                                                        }
                                                                                                                                                         
                                                                                                                                                                                                                                                                  return                                  0                                                                                    ;                       }

Name: Anonymous 2010-01-26 16:40

>>82
You are now parsing white space manually

Name: Anonymous 2010-01-26 16:41

>>82
I think I see Cassiopeia!

Name: Anonymous 2010-01-26 17:05

[telelscope]
>>82
[/telescope]

Name: Anonymous 2010-01-26 17:20

EXPERT MALLARMÉ1 INDENTATION STYLE

1. ^ http://www.direz.fr/site/uploads/Mallarme/Coupde.pdf

Name: 86 2010-01-26 17:22

Fuck. I forgot to quote >>82. Fuck!

Name: Anonymous 2010-01-26 21:59

>>86
I chuckled knowingly

Name: Anonymous 2010-01-26 22:14

>>67
No, it's because the ancient compiler that K&R used had a bug in it where it couldn't parse the opening brace of a function unless it was after a newline. Why would the lack of nested functions matter to indentation style? Java in particular allows nested classes and the official Java style guide suggests putting the opening brace on the same line.

Here's a relevant question for the /prog/ crowd: Do you block and/or indent semantically relevant code blocks, such as mutex locking or glBegin/End?

void PaintRect(void) {
  glBegin(GL_TRIANGLE_STRIP); {
    glVertex2f(0,0);
    glVertex2f(0,1);
    glVertex2f(1,0);
    glVertex2f(0,1);
  } glEnd();
}

void DecRefCount(Object* obj) {
  pthread_mutex_lock(&mutex); {
    if (!--obj->refCount) {
      obj->destroy_fn(obj);
      free(obj);
    }
  } pthread_mutex_unlock(&mutex);
}


It has the same advantage as 1TBS in that code merging by revision control systems is much less likely to produce bad code (you really can't resolve the conflict incorrectly, whereas without the indent, if you aren't paying close enough attention you might accidentally put a critical statement on the wrong side of the mutex, causing a race condition or deadlock). Plus you can't really forget the close/unlock as you're typing when you do it this way (if you're not in the habit of writing open/close at the same time). Lots of OpenGL examples use this style.

However it might be misleading. For instance I would expect to be able to break or return out of a nested scope (since closing the scope usually doesn't have an effect), whereas you really can't do that here. I guess you have to remember to check whether closing braces have a closing statement, whereas I'd be much more hesitant to skip the rest of the function when it's flat. Maybe that's just because my eyes are used to skipping over closing braces, since aside from do {} while, there's never anything after them. Another downside is you have to forward declare variables outside the scope if you want to get their values after unlocking (you could do the indent without the block, but that's even more misleading). And another downside is, where do you draw the line as to what you indent or don't indent? Do you indent temporary variables that need to be destroyed, for instance?

void doSomething() {
  SomeObj temp;
  SomeObjInit(&temp); {
    // do some shit
  } SomeObjDestroy(&temp);
}


I think despite these disadvantages I'm still considering using this style at least for mutex locking in my own projects. Of course if you're in Sepples you should just use RAII. I'm rambling at this point. What do you think?

Name: Anonymous 2010-01-26 22:21

What do you think?
Who, me? Shit, I dunno dude. Use a monad?

Name: Anonymous 2010-01-26 22:43

>>81
Indeed, why waste space?
That said, it is practical to see where the loops&stuff end.

#include "stdio.h"

int main (void) {
    int   x;
    char  c = 'a';
    float y = 0;

    for (x = 0; x < 10; x++) {
        if  (x % 2 == 0) {
            printf ("%d\n", x);
        } else {
            printf ("%c\n", c);
            c +=1;
        }
    }
    return 0;
}

Name: Anonymous 2010-01-26 22:49

>>91
Why two spaces after if?

Name: Anonymous 2010-01-26 22:54

>>92
Copypasta + lacklustre error-checking. In one word: typo.

Name: Anonymous 2010-01-26 23:14

>>92
I guess it's to make if sync up with for:


int main()
{
    for (...) ...
    if  (...)
    {
        ...  
    }
}

Name: Anonymous 2010-01-26 23:18

#include <stdio.h>

int main ()
{
    char c = 'a';

    for (int x = 0; x < 10; x++)
    {
        if (!(x % 2))
            printf ("%d\n", x);
        else
            printf ("%c\n", c++);
    }
   
    return 0;
}

Name: Anonymous 2010-01-26 23:23

>>81
Why waste space indeed...

#include "stdio.h"
int main(void){int x;char c='a';float y=0;for(x=0;x<10;x++){if(x%2==0){printf("%d\n",x);}else{printf("%c\n",c);c+=1;}}return 0;}

Name: Anonymous 2010-01-27 0:03

>>96
#include"stdio.h"
main(){int x=-1;char c='a';float y=0;for(;x++<9;){if(x%2)printf("%c\n",c++);else printf("%d\n",x);}}

Name: Anonymous 2010-01-27 3:14

>>97
#include<stdio.h>
x;char c=97;main(){for(;x<10;x++)printf(x&1?"%c\n":"%d\n",x&1?c++:x);}


or perhaps…
#include<stdio.h>
x,c=97;main(){for(;x<10;x++)x&1?putchar(c++):printf("%d",x),puts("");}

Name: Anonymous 2010-01-27 3:46

>>96-98, your <void.h> is missing.

Name: Anonymous 2010-01-27 15:57

#include "stdio.h"

int main (void)
{   int   x;
    char  c = 'a';
    float y = 0;
  
    for (x = 0; x < 10; x++)
    {   if  (x % 2 == 0)
            printf ("%d\n", x);
        else
        {   printf ("%c\n", c);
            c +=1;
        }
    }
  
    return 0;
}

Name: Anonymous 2010-12-06 10:04

Back to /b/, ``GNAA Faggot''

Name: Anonymous 2011-02-04 11:40

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