Which do you prefer?

Which do you prefer?

Other urls found in this thread:

gnu.org/prep/standards/html_node/Formatting.html
github.com/torvalds/linux/blob/master/Documentation/process/coding-style.rst
twitter.com/NSFWRedditVideo

bottom is easier to read

gofmt

Everything on one line

>else statement after the condition returns a value

bottom but with the else starting in a new line

return hours < 24 && minutes < 60 && seconds < 60;

This, premature return is bad form and some languages (like Object Pascal) won't even let you do it.

the top, which I know is not the Sup Forums way, but I think it was how I was originally taught back when I first learnt C(which may have been wrong ?)
I think im just conditioned to format my code like the top example.

I used to type in the top format in school till codes became long and i used the bottom one instead

I was looking at one of my programs from one of my first classes at school and I did:
if ( )
{

} else
{

}

no brackets necessary for single executions after expression.

/thread

That's stupid and illogical, though.
If the expression is false it ignores.. so it shouldn't matter at all.

bad style

if(condition) {
// Do something
}
else {
// Do something else
}

bottom, but no brackets on 1 line blocks

>if(condition)
fucking disgusting

this also >>>/sqt/

>MOOOOM!! I don't like peas and potato mash touching each other!!!!111
I'm fine with either tbqh familia.

This.

bottom
gofmt is fine but I like rustfmt more useful

That's the Visual Studio way, isn't it?

even the official google style guide says that is bad form

I care why?

It's just as, if not more, readable as long as you indent.
paradigms are fucking retarded. Use what you need when you need it.

if(condition)
{
// some shit
}
else
{
// else shit
}

readability, niggers

If you care about readability you should use
if (condition) {
// some shit
} else {
// else shit
}

if ( condition )
{
// usual, expected case
} else
{
// other
}

Neither.

if (hours < 24
&& minutes < 60
&& seconds < 60)
{
doSomethingWithHours (&hours);
doSomethingWithMinutes (&minutes);
doSomethingWithSeconds (&seconds);
}
else
{
doSomeOtherThingWithHours (&hours);
doSomeOtherThingWithMinutes (&minutes);
doSomeOtherThingWithSeconds (&seconds);
}


If you're not doing more than one instruction per branch:

if (hours < 24
&& minutes < 60
&& seconds < 60)
doSomethingWithTime (&hours, &minutes, &seconds);
else
doSomeOtherThingWithTime (&hours, &minutes, &seconds);


See:
gnu.org/prep/standards/html_node/Formatting.html

No, that's a fucking eyesore. Line that shit up. Some people have OCD.

>some people have ocd
That's your problem.

>GNU style
no thanks

this

Hello, pajeet.

And you actually took the time to decapitalize my quote to trigger me. That's dedication. Line that shit up, nigger.

not doing the superiour

if(!something){
// some shit
} else if(something) {
// something else
} else {
// some other shit
}

maybe i'm a babby but i like setting my tabs to generate 6 spaces instead of 4 spaces for increased readability in complicated code blocks

>CS grad.png
How about just return condition

int
main(int argc, char *argv[])
{
if(something) {
// do something
} else {
// do something else
}

if(one_line)
// something

return 0;
}


i think it's /thread

int
main()

What if you have to search the source for a function declaration and you don't know its exact return type/argument types, you moron?

Use an editor that was written after 1962?

I'm a "top".

I wouldn't even think of being a "bottom".

You are the reason why Trump won.

Yes, and do what exactly?

>hurr what does main return i forget maybe grep will tell me

>what are typedefs?
You've obviously never worked on a big C project.

Searching for a function whose prototype you don't know is a frequent problem, and breaking the declaration on two lines like that just makes your life infinitely easier because now you can just do grep ^function_name.

Now you fucking rookies can fuck off.

Just search for the function name in the file?

then you'll find all the places where it's called, not just its declaration

But that style is OBJECTIVELY bad famalam.

Anyone that puts the opening brace on a newline for anything besides functions is an idiot, but you're also an idiot for using braces when there's only one statement.

If you're placing the statement on its own line, you HAVE to use braces.

>(which may have been wrong ?)
It was.

K&R put the opening brace on the same line, and you should too.

No you don't

The Linux style guide, which is the most significant C code base in existence led by the largest C advocate still working, prefers the braces to be dropped if all statements in an if-else block are one line.

idc

>First off, I'd suggest printing out a copy of the GNU coding standards, and NOT read it. Burn them, it's a great symbolic gesture.
See:
github.com/torvalds/linux/blob/master/Documentation/process/coding-style.rst

It is lined up, jackass.

Were you dropped on your head as a child, or have you never used C before?

No, you HAVE to do it or you end up with goto fail.

That's only if you have more than one statement for the condition

between the two? top one.

that was how I was taught from a M$ programmer when we were working on a MMO together. It reads easier when you get 17 conditionals deep in a script.

That seems like a sign that you need to refactor your code

You may only have one now, but it's only a matter of time before some retard who thinks he's writing Python adds another.

They did say that advice came from a Microsoft programmer.

that's his problem

But what about leap seconds?

Someone who uses an if-else branch instead of a single return statement wouldn't be thinking of that

...

>some mediocre student-made 25 year old MINIX1 ripoff's coding-style
>relevant

And you are OBJECTIVELY a street-shitter.

>That seems like a sign that you need to refactor your code
meh, some things can get complicated when you're coding what a fireball does.

seriously. especially when you have "heartbeat" scripts running, you need to time the "graphic explosion" to times based on distances. well it got a bit complex sometimes.

worse only a handful of scripted behavior to could coded and reused because the conceptual design was done by some jackass who had no idea of how a game works.

this is bait right

Whatever my boss wants, because I have a fucking JOB.

not using the braces at all

top for compiled languages, bottom for scripting languages

I prefer the first because it's PSR4 compliant, and keeping PSR4 compliance is what I need to do.

[spoiler]Plus the second is for shitters haha haha[/spoiler]

The first one. Extra lines are only expensive if you're a computer. You're a human, you only have a few degrees of vision. The ability to quickly be able to identify blocks of code while reading is infinitely more useful than winning code golf.
And for those complaining about the return within an else, consider this. That's both protection against future growth and more readable.
Read some Kerninghan before you start running your mouth about blocks.

>You're a human, you only have a few degrees of vision.
Which is why you shouldn't spread the code out vertically so you have to move your eyes more to read the same amount of code.

No. With a small degree of vision, that block is going to spend more time in your peripheral than it will in your primary vision. Your ability to distinguish its structure and treat it as a landmark disintegrates the more cramped you make it. What this means is that you need to spend more time focusing on the block of code to correctly identify it. So you may theoretically need to read less, but you will need to read it more often and closer. This limits your ability to quickly identify the overall structure of groups of blocks of code while skimming code and switch your immediate focus, both of which diminish your capacity for switching context quickly. Breaking your focus while switching context makes you a worse programmer by limiting your ability to chain thoughts together.

This. There is no other correct answer.

This.

I prefer
return (hours < 24 && minutes < 60 && seconds < 60)

Depends on the language

>leaving in magic numbers

if (a = 2)
return a;
else
return 0;
.

boi im about to lay some shit on ya

if (age

Top hands down.

Top is old school, which the way I like it
void AddItemToBag_(Bag *self, const char *item_name)
{
if (!IsBagFull(self))
{
for (int i = 0; i < self->capacity; i++)
{
if (self->items[i].type == None) // Find a empty slot
{
Bag_AssignItem(self, i, item_name);
self->size++;
return;
}
}
}
else
{
printf("Can't add %s to the bag\n", item_name);
printf("Bag is currently full!\n");
return;
}
}

void RemoveItemFromBag(Bag *self, Item item)
{
if (!IsBagEmpty(self))
{
for (int i = self->capacity - 1; i >= 0; i--)
{
if (self->items[i].type == item.type) // Find the slot that has the item were looking for
{
RemoveBagIndex(self, i);
return;
}
}
}
else
{
printf("Bag is empty!\n");
return;
}

there should be a space between if and the (

So I should create variables for how long a day is eh

if (hours < 24)
if (minutes < 60)
if (seconds < 60)
return true;
else
return false;

Whatever you do, be consistent.

/thread

if (hours < 24)
if (minutes < 60)
if (seconds < 60)
return true;
else
return false;

muh leap seconds

return hours < 24 && minutes < 60 && seconds < 60 ? true : false;

This is the best if. Prove me wrong.
PS: You can't.

>made gnu relevant
>irrelevant
Daily reminder that GNU could perish from the face of the earth tomorrow and Linux would still be relevant, but not the other way around.

That is the proper, K&R style.

>Read some Kerninghan before you start running your mouth about blocks.
Kernighan explicitly uses the second one in The C Programming Language you useless nigger. Having the top curly on it's own line needlessly clutters shit up because the bottom will line up with the key word for the block, and everything in between belongs to it.

Doesn't necessarily mean it's right.