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

C naming convention

Name: Anonymous 2010-04-09 3:53

I'm trying to decide on a naming convention for my C library. Do I do pthreads/libpng/etc style, using capital letters only for constants and macros?

typedef struct foo {
  int* bar;
} foo_t;

void foo_init(foo_t* self) {
  self->bar = (int*) malloc(sizeof(int) * 5);
}

void foo_destroy(foo_t* self) {
  free(self->bar);
}


Do I do CamelCase?

typedef struct Foo {
  int* bar;
} Foo;

void FooInit(Foo* self) {
  self->bar = (int*) malloc(sizeof(int) * 5);
}

void FooDestroy(Foo* self) {
  free(self->bar);
}


Or do I do something even more crazy?

I'm definitely leaning towards pure lowercase. It's not really more work to write, and it's a lot easier to read afterwards. Plus it's really consistent with the standard library, among others.

What convention do /prog/lodites use?

Name: Anonymous 2010-04-09 4:17

>>1
Either is fine as long as it's consistent. I tend to use CamelCase, but K&R style is fine too. My favorite style is lowercase and a minus sign, like most-positive-fixnum, but I've never seen any language except Lisps use that, mostly because their syntax wouldn't support it (infix notation).

Name: Anonymous 2010-04-09 4:39

I prefer CamelCase for variables and the first style(i forgot what it was called) for functions.

Name: Anonymous 2010-04-09 4:51

>>3
Interesting, that's pretty much the opposite of Google style. They use CamelCase for all types, functions and methods, and use lower_case for all variables. (They also forbid the use of shitCase.) Unless you meant CamelCase for types and not variables, in which case your style isn't all that uncommon (it's just ugly.)

Also, what is /prog/'s opinion on the trailing _t? It seems redundant if you properly prefix all your global identifiers, i.e. ns_foo where ns is the acronym for whatever namespace you want. Thoughts?

Name: Anonymous 2010-04-09 5:12

>>4
shitCase is buttugly OOP crap. cant stand it.

Name: Anonymous 2010-04-09 5:39

>>4
I am not a fan of the trailing _t in certain cases. I think it helps in things like time_t, but for primtives, I MUCH prefer int64 to int64_t.

Name: Anonymous 2010-04-09 5:52

I don't suffix user data types with _t. I read about it somewhere as being bad in some way.

Anyway it's usually K&R style for functions and CamelCase for types.

Name: Anonymous 2010-04-09 5:54

>>7
Here it is! http://www.jetcafe.org/~jim/c-style.html#Naming

Typedef Names
    Capitalized, with no _t suffix or other cutesy thing to say ``I'm a type'' - we can see that from it's position in the declaration! (Besides, all names ending with _t are reserved by Posix.) The capitalization is needed to distinguish type names from variable names - often both want to use the same application-level word.

Name: Anonymous 2010-04-09 8:47

>>4
What are you talking about? shitCase has semantic meaning in Go. And it isn't "abort compilation."

Name: Anonymous 2010-04-09 11:50

>>4-5
haskellCase > You

Name: Anonymous 2010-04-09 11:56

>>10
There's nothing saying you can't use lispy-names-for-functions in Haskell (or Forth, or any language without a grammar that intrudes...)

Name: Anonymous 2010-04-09 11:59

>>11
Yes there is, - is reserved for operators.

Name: Anonymous 2010-04-09 12:08

>>11
I'd love to intrude your mom.

Name: Anonymous 2010-04-09 12:22

>>11
[JEWS@JEWS ~]$ ghci
GHCi, version 6.10.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
Prelude> let my-totally-awesome-square-function = \x -> x * x
<interactive>:1:4: Parse error in pattern
Prelude> let haskellCaseIsShit = \x -> x * x
Prelude> haskellCaseIsShit 9
81
Prelude>

Name: Anonymous 2010-04-09 13:31

I use lowercase for functions and variables, no underscores except as a namespace delimiter (e.g. ns_getcbfl(), not ns_get_cb_fl()), and all uppercase for #defines. Global variables, which tend to be rather important, start with an uppercase letter.

Name: Anonymous 2010-04-09 13:32

>>11
Works in ANS Forth and PostScript.

Name: Anonymous 2010-04-09 13:38


[JEWS@JEWS ~]$ ghci
GHCi, version 6.10.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
Prelude> let my-totally-awesome-square-function = \x -> x * x
* Exception: Stack Overflow

Name: Anonymous 2010-04-09 13:43

>>9
Yeah um, just because Go is sponsored by Google doesn't mean they actively use it. It's just an experiment right now. Here's Google's real style guide:

http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#General_Naming_Rules

Also, doesn't anyone else think that assigning semantic meaning to identifier name style is, well, dumb? I have the same problem with Python's "private" variables by leading with underscores...

Name: Anonymous 2010-04-09 14:21

Also, doesn't anyone else think that assigning semantic meaning to identifier name style is, well, dumb? I have the same problem with Python's "private" variables by leading with underscores...
It does make it more of a pain to change the visibility of something, but I'm not inherently against it. Enforced conventions are fine when you would be following them anyway, and a pain when the convention is stupid.

Name: Anonymous 2010-04-09 14:24

>>18
Leading underscores is different to changingTheCaseStyle. Clearly anything beginning with __ isn't going to be used in everyday code. But yes, Python's version of int main() is idiotic.

Name: Anonymous 2010-04-09 14:30

>>18
All I'm saying is that dealing with a preexisting atrocity is one thing, but when they make their own, they enforce shitCase.

And yeah, their way of going about it is masterfully retarded. The Go team acts like a bunch of goddamn hippies most of the time so I'm not surprised.

Name: Anonymous 2010-04-09 14:50

I never saw a respectable C library use CamelCase.

Name: Anonymous 2010-04-09 16:27

>>22
That's because no respectable programmer would ever consider using CamelCase.

Name: Anonymous 2010-04-09 18:05

Does anyone know how to make Vim color underscores at 50% gray?

Name: Anonymous 2010-04-09 18:25

>>24
Threadjacking attempt DENIED

Name: Anonymous 2010-04-09 18:26

>>24
Nevermind, figured it out.

$ cat ~/.vim/after/syntax/c.vim
" gray underscores
syn match underscore +_+
hi underscore guifg=Gray ctermfg=Gray


Makes them a bit dimmer, which is sort of nice. Not sure if I like it yet. Maybe I'll try the same with semicolons.

Name: Anonymous 2010-04-09 18:27

>>25
Actually this isn't threadjacking, I'm OP trying to make lowercase underscore style look better.

Name: Anonymous 2010-04-09 18:42

'+_+

Name: Anonymous 2010-04-10 13:21

+_+;

Name: Anonymous 2010-04-10 14:13

JACK MY ANUS

Name: Anonymous 2011-02-03 6:22

Name: Anonymous 2011-02-03 6:46

Name: Anonymous 2011-02-04 12:38

Name: Anonymous 2011-02-04 15:28

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