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

Pages: 1-

C++ code style

Name: Anonymous 2010-05-24 12:37

Hi, /prog/.
What is the most beloved syntax style here? KR? BSD? GNU, perhaps?

Name: Anonymous 2010-05-24 12:38

in b4 sicp snake.

Name: Anonymous 2010-05-24 12:49

/prog/ style
int
main
              (
 int     argc,
 char  **argv
)
                    {
 const char *s;
 s = "Hello world!";
 printf("%s", s);
 return 0;
}

Name: Anonymous 2010-05-24 12:50

I like my Lisp style s-exp.

Name: Anonymous 2010-05-24 12:52

>>3
It is... strangely understandable.

Name: Anonymous 2010-05-24 13:09

>>3
I suppose you won't believe me and I completely understand, but as far as I know I was the one to invent this indentation style and post it in a thread here on /prog/.

Name: Anonymous 2010-05-24 13:13

>>6
I AM A RICHARD STALLMAN
I AM A RICHARD STALLMAN
I AM A RICHARD STALLMAN
HAX MY ANUS
HAX MY ANUS

Name: Anonymous 2010-05-24 13:35

>>6
Oh I remember the thread, in fact, >>3 was taken directly from it.

Name: Anonymous 2010-05-24 13:48

my style.

#include <iostream>
using namespace std;

const char *what = "an anus";

int main( int argc, char *argv[] ) {
    if( argc != 2 ) {
        cout << "usage: " << argv[0] << " [text]" << endl;
        return 0;
    }

    cout << argv[1] << " is " << what << endl;
    return 0;
}

Name: Anonymous 2010-05-24 15:17

The C standard using 1TBS and 8 space tabs that are turned into spaces

Name: Anonymous 2010-05-24 16:18

Linux kernel style, indented with spaces, with 128 characters max width per line.

// comments inside indented blocks, /* comments */ at top level.

Name: Anonymous 2010-05-24 16:28

>>11
// comments considered harmfull in C code.

Name: Anonymous 2010-05-24 16:33

>>12
Hello, FV

Name: Anonymous 2010-05-24 16:56

>>12
Control structures considered harmful
let's go to goto

Name: Anonymous 2010-05-24 17:01

Didn't we have this thread a while ago?

Name: Anonymous 2010-05-24 17:30

I just used Eclipse formatter a while ago.

I came.

Name: Anonymous 2010-05-24 17:45

int give_me_dicks() {
    int dicks;
    if (stuff()) {
        dicks = aids(); }
    else {
        dicks = youre_mom(); } //
YHBT
    return dicks; }


Indeted using proper tabs of course because I'm not a faggot.

Name: Anonymous 2010-05-24 17:46

>>9
That would be Java style.

>>13
That guy uses Sepples comments in C code all the time. You're thinking of Xarn, who actually understands why they're a bad idea.

Name: Anonymous 2010-05-24 17:53

FrozenVoid style, of course ! ! !

#include "void.h" //handles all the common functions,#defines,#ifdefs and #includes
const char *what="an anus";mainstart if(argc!=2){cout<<"usage: "<<argv[0]<<" [text]\n";return0;}cout<<argv[1]<<" is "<<what<<"\n";mainend

Name: Anonymous 2010-05-24 18:03

>>17

[aa][o]Stroustrup Approved!![/aa][/o]

Name: Anonymous 2010-05-24 18:45

>>19
Elitism, I detect it on great quantities.

Name: Anonymous 2010-05-24 19:51

I have no idea what my style is called.

#include <iostream>
#include <stdlib.h>
#include <time.h>

std::string phrases[4] = {
        "I personally use Notepad2 and avoid any \"makefiles\"(which i consider bloated and poor design)",
        "I use one include file for everything.",
        "Anything which uses makefiles is defective Sepples-level shit.",
        "All the C code posts on my blog are void.h merged with somefile.c"
};

class Frozen {
        public:
        Frozen();
        void post();

        private:
        std::string retarded_phrase;
};


Frozen::Frozen() {
        srand( time( NULL ) );
        retarded_phrase = phrases[ rand() % 4 ];
}


void Frozen::post() {
        std::cout << retarded_phrase << endl;
}


int main(void) {
        Frozen * progtroll = new Frozen;
        progtroll->post();
        delete progtroll;
        return 0;
}

Name: Anonymous 2010-05-24 21:33

>>22
I think that's called "shit".

Name: Anonymous 2010-05-24 23:41

>>23
Man, that's harsh. You could at least give >>22 some pointers.

Name: Anonymous 2010-05-25 0:09

>>24
Okay. Most offensively ugly first:

  • Indenting public: et al. to the same level as other code in a class definition is awful, because they're fundamentally different from the rest of the content. Same applies for labels and case statements. I prefer writing these lines at the indentation level of the enclosing block, but anything except what you did would be an improvement to readability.
  • Spaces inside parentheses / brackets are stupid. You don't write like that in natural language. There's no need to write like that; it doesn't help things make more sense, it just makes them wider. (And if your code is so convoluted with nested parentheses and brackets, consider breaking it up and using a temporary variable or two.)
  • Writing a space on both sides of the asterisk when referring to a type is misleading and will confuse people. Incidentally, this ambiguity is one of those dumb things about C syntax that I hate, but we're stuck with it. I prefer the space before the asterisk, since that way it's consistent with dereferencing. Plus, that way int *a, *b is consistent, but I don't like declaring a bunch of variables on one line, either, so really which way you write it doesn't matter a whole ton. Consistency and clarity do, however, so the goal is to come up with something that communicates your intent, and stick with it, whatever it might be.
    This is one of my biggest annoyances with many reformatting tools: they tend not to actually read the code, so user-defined types end up being formatted incorrectly.
  • Omitting the final comma in a list is asymmetrical, and makes reordering the list or adding items more difficult. C permits it; take advantage of that. (This is one of my Haskell annoyances)
  • I'll assume that you created progtroll dynamically simply to show how you format pointer declarations, but if not, there really wasn't a reason to allocate and then deallocate one single instance of something unconditionally within the same function.
  • And getting totally into nitpicks about the code rather than the style, you should probably be calling srand exactly once, at the start of your program. (If you meant to have a different RNG for each instance of that class, you did it wrong.) I would also avoid the [4] in the array declaration, using [] instead. (Or does C++ disallow that for some insane reason? I'm not really a C++ programmer, I just use it when I have to)

And isn't endl part of std?

Well, those points aside, it is indented, and on top of that, fairly consistent, which is more than a good 90% of first-year CS students seem to do.

Name: Anonymous 2010-05-25 3:52

Omitting the final comma in a list is asymmetrical, and makes reordering the list or adding items more difficult. C permits it; take advantage of that. (This is one of my Haskell annoyances)

What's wrong with using [a, b, c, d] or
[ a
, b
, c
, d
]

?

Name: Anonymous 2010-05-25 4:05

>>25
As you can see, the syntax is the root of all evil.
Un-ambiguos, easily editable, both manually and programmatically S-Exp's are superior.

Name: Anonymous 2010-05-25 5:36

>>25
Spaces inside parentheses / brackets are stupid. You don't write like that in natural language.
You don't end every sentence with a semicolon either, and you don't put parentheses around the subjects for each verb. The contents of parentheses aren't an aside like they are in natural language, they are just as important ( if not more so ) than the name of the function.
Not only does padding the arguments with whitespace separate them from the parentheses (as they are separate things), but it also makes more sense when 'spelling out' the function. Consider:
foo( a, b, c );
foo (a, b, c);
foo(a,b,c); /* FV-stylée */

The first has the start of a function, then the arguments, each followed by some whitespace and a comma (bar the last), then the end of a function.
The second has a function name, then a parenthesis holding an argument with a comma, then some more arguments, the last of which is attached to another parenthesis.

Name: Anonymous 2010-05-25 5:53

FV-Style saves about 5% keystrokes, tru fax. Its also trolling every future programmer that ever touches your code.

Name: Anonymous 2010-05-25 7:35

>>25
Writing a space on both sides of the asterisk when referring to a type is misleading and will confuse people.
bzzzzzt, wrong

but thank you for the review.  I wouldn't have created progtroll dynamically or put srand() in Frozen's constructor like that in 66real code99, but the part about not indenting public: (and case / labels) is a good point

Name: Anonymous 2011-02-04 12:42

Name: Anonymous 2011-02-04 18:48

Name: Anonymous 2011-02-17 20:08

check 'em dubz

Name: Anonymous 2013-01-19 23:06

/prog/ will be spammed continuously until further notice. we apologize for any inconvenience this may cause.

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