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;
}
}
int main (void)
{
int x = 0;
char c = 'a';
float y = 0;
for(; x < 10; x++)
{
if(x%2 == 0)
{
printf("%d\n", x);
}
else
{
printf("%c\n", c);
c += 1;
}
}
return 0;
}
(What was the float for?)
There are additional things this short code does not demonstrate. I tend to place ternary operations in parenthesis. The simplicity of the statement executed by the if...else branch decides whether I use more lines or put that part on a single line. I declare purpose-related, type-same variables in the same line. I have specific spacings: between adjacent classes (three lines), between adjacent functions/methods and their comments (also two lines). If I declare, initialize, set, or compare variables, I make sure there are spaces, save for the modulus operator. Putting spaces sparsely on the outside of parenthesis only where it assists readability.
Here's an example of something I'm oft to do when using Java: System.arraycopy(src,0, dest,0, (j-1));
The first two and the next two parameters may be related but there's no reason to space them like that: I just do.
>>7
From a comment to that: This is the image I got when talking to Gosling about his project Jackpot, in which the notion of program truth is not text, but an annotated parse tree... The really interesting conceptual leap I got from the conversation was that maybe future languages could be defined not as a text syntax but as some kind of data structure.
Has no one read their SICP?
Name:
Anonymous2010-01-24 17:47
Fuck, I forgot my other coed tag.
#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;
}
}
>>6
The float was to see how you did the spacing between variable types and variable names. You put 1 space in between them, for example, while I make all of my variable names line up.
#include "void.h" //handles all the common functions,#defines,#ifdefs and #includes
start int x;char c='a';float y=0;for(x=0;x<10;x++){if(!x%2){printf("%d\n",x) ENDIF else{printf("%c\n",c);c+=1 ENDIF } finish
Name:
Anonymous2010-01-24 19:23
>>23
how fast does that run? 1 line must be pretty fast
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;
}
/*
GRUNNUR
;
*/
Name:
Anonymous2010-01-24 19:51
>>23
Not yet void quality. The negation in !x%2 can be removed by swapping the two cases, and c+=1 should be sepples. Of course, to obtain true FV quality you should manually unroll the loop and simplify it.
Name:
Anonymous2010-01-24 20:25
#include "stdio.h"
int main(void) {
int x;
char c = 'a';
float y = 0;
I try to stick to variables being 4 characters wide, just a habit I got into. I also put an underscore at the front of any local variable. If I'm coding in C++ I will declare structs as s_*name*, unions as u_*name*, enums and classes in the same style. I always declare a counter as close as possible to it's use in it's first loop.
Name:
Anonymous2010-01-25 1:04
#include "stdio.h"
int main(int argc,char **argv)
{
char c = 'a';
float y = 0;
for(int x = 0; x < 10; ++x){
if(x % 2 == 0){
printf("%d\n",x);
}else{
printf("%c\n",c);
c += 1;
}
}
>>33-
Asymptotically Approaching Glacial Entropy, if you know what I mean.
Name:
Anonymous2010-01-25 2:41
if(condition()) single_line_if();
This is probably the style choice that throws me off the most. Is it so hard to newline? To keep the condition on one line and the procedure on the other? Even in these resource-conscious times, there's no reason to cram all that logic.
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:
Anonymous2010-01-25 8:03
FROZEN VOID QUALITY
Name:
Anonymous2010-01-25 8:54
/* This is the main function. The main function will do
everything in the program. */
int main (void)
{
/* In this section rest the declarations
for the variables used througout the program. */
int x; /* This will be use for the counter. */
char c;
float y; /* ``float'' is a nice word. */
/* Here we initialize the char (declared above)
and the float. */
c = 'a';
y = 0;
/* This section of the code loops from 0 to 9,
and handles things differently depending on whether
``x'' divides by two. */
for (x = 0; x < 10; x++) {
/* If-else clause. Handles what happens depending
on the value of x. */
/* If... */
if (x % 2 == 0)
/* If x divides, it prints x, and a newline. */
printf ("%d\n", x);
/* Else... */
else {
/* If x does not divide, it prints ``c'', and a newline,
and adds 1 to c. */
for (int x = 0; x != 10; ++x) {
if (x % 2 == 0)
printf("%d\n", x);
else
printf("%c\n", c++);
}
return 0;
}
The one true style. And no, I don't do C89 bullshit block declarations. It's 2010 for fuck's sake.
Name:
Anonymous2010-01-25 20:40
Oh god, so many hideous code styles. The only one remotely passable is >>31, and it has the legacy compatibility limitations of old K&R, such as requiring the open brace of a function on a newline. >>29 actually isn't terrible, except for the fact that his spacebar seems to be broken.
As the author of >>60, I feel I must educate furthur as to the one true style. There's lots of stuff this did not cover:
double braces around assignment in if statements if ((x = 7)) do_some_shit();
related declarations should have equal signs aligned int something = 4;
int something_else = 7;
repeated related function calls should have arguments aligned (with space only after the commas, none trailing; no alignment of commas or closing brace) some_func(an_arg, another_arg);
some_func(a_third_arg, last_arg);
braces can be used around and in ternary operator when needed, and put spaces around ? and : (but never inside braces). usually put braces around arms when needed to clarify, even when the precedence order would allow no braces (mainly because the inner arm of ?: breaks the precedence order) some_func(a ? (b + c) : d, e);
if an else block needs braces, add braces for the if block as well (don't do like OP). they don't add any lines in this case. if (x % 2 == 0) {
printf ("%d\n", x);
} else {
printf("%c\n", c);
c = c + 1;
}
no line length limits; this isn't the stone ages. but be reasonable. repeated related function calls however can break the 'reasonable' limitation, and run well over 200 characters; scrolling horizontally to see a table of well-aligned function calls is much preferable to 30 lines of repeated function calls with no alignment to show you the argument layout.
Uh... No, all of the non-joke coding styles listed in this thread are fine, as long as they're consistent.
Name:
Anonymous2010-01-26 2:31
>>61 related declarations should have equal signs aligned int something = 4; int something_else = 7;
Fair enough. Although I think Guido might object. repeated related function calls should have arguments aligned (with space only after the commas, none trailing; no alignment of commas or closing brace) some_func(an_arg, another_arg); some_func(a_third_arg, last_arg);
Uggggh. IHBT.
When it comes to C-likes, here's what I think of them:
K&R - okay, if consistent, but I find it less readable
Allman - more readable, I tend to find it easier to parse
BSD - okay, but less readable than Allman, however I like how function return type is declared on a separate line
Whitesmith - I don't see any advantage to it.
GNU - It's not terrible, but the braces having their own indent level is just plain weird.
Horstmann - Pretty good. Combines Allman's advantages with K&R's advantages. Probably one of the better ones, but it requires a good editor like Emacs to be used with. Is this what OP used?
For C-likes, I tend to use Allman, but if I'd use a more superior editor like Emacs for C code, I'd write in Horstmann style.
Indentation style is an already fully solved problem for some languages, for example Lisp has its own style, which is used by all sane Lispers (it's actually hard to read code written in non-standard lisp indentation styles, which are usually nonexistent and only used by misguided newbies which don't have a good editor). Emacs (and other lisp editors) provide full (re)indentation support and paren-matching, as well as many other useful features. This is ideal, but it helps having an uniform syntax, which is not what C-likes have.
Name:
Anonymous2010-01-26 6:41
>>70 Allman - more readable, I tend to find it easier to parse
And how is that possible?
>>71
I didn't mean "parse" in the sense of writing a program which parses such source code. I meant it in the mental sense: my brain seems to take less time reading C code indented in Allman than it does with K&R style. It may be a small difference, but it's enough for me to write my own(when working with code written by others, it's best to keep consistent) C code in Allman style.
K&R style
Yes, though I don't like having the brace on a newline after a function definition if I can help it. Long function definitions and constructors that initialize data are my exceptions. 1TBS
No. Allman style
Awful. BSD KNF style
Could be worse. I hate including else statements on the closing brace of an if, though. Whitesmiths style FUCK NO!!! GNU style
As pig-disgusting as Stallman himself. Horstmann style
Maybe if I was a troll. So basically, no.
Name:
Anonymous2010-01-26 7:44
you forgot frozenvoid style. no newlines.
Name:
Anonymous2010-01-26 8:19
The One True Indentation Style for C #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;}
>>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?
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?