Which do you prefer?
bottom is easier to read
>>59260041
gofmt
Everything on one line
>else statement after the condition returns a value
>>59260041
bottom but with the else starting in a new line
>>59260041return hours < 24 && minutes < 60 && seconds < 60;
>>59260083
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 /g/ 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.
>>59260041
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.
>>59260088
/thread
>>59260098
That's stupid and illogical, though.
If the expression is false it ignores.. so it shouldn't matter at all.
>>59260172
bad style
>>59260041if(condition) {
// Do something
}
else {
// Do something else
}
>>59260041
bottom, but no brackets on 1 line blocks
>>59260265
>if(condition)
fucking disgusting
this >>59260265
also >>>/sqt/
>>59260310
>MOOOOM!! I don't like peas and potato mash touching each other!!!!111
I'm fine with either tbqh familia.
>>59260060
This.
>>59260041
bottom
>>59260060
gofmt is fine but I like rustfmt more useful
>>59260131
That's the Visual Studio way, isn't it?
>>59260172
even the official google style guide says that is bad form
>>59260254
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
>>59260471
If you care about readability you should useif (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:
https://www.gnu.org/prep/standards/html_node/Formatting.html
>>59260484
No, that's a fucking eyesore. Line that shit up. Some people have OCD.
>>59260519
>some people have ocd
That's your problem.
>>59260511
>GNU style
no thanks
>>59260530
this
>>59260537
Hello, pajeet.
>>59260530
And you actually took the time to decapitalize my quote to trigger me. That's dedication. Line that shit up, nigger.
>>59260471
not doing the superiourif(!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
>>59260041
>CS grad.png
How about justreturn 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
>>59260986int
main()
>>59261034
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?
>>59261141
Use an editor that was written after 1962?
I'm a "top".
I wouldn't even think of being a "bottom".
>>59261186
You are the reason why Trump won.
>>59261167
Yes, and do what exactly?
>>59261141
>hurr what does main return i forget maybe grep will tell me
>>59261230
>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 dogrep ^function_name.
Now you fucking rookies can fuck off.
>>59261219
Just search for the function name in the file?
>>59261314
then you'll find all the places where it's called, not just its declaration
>>59260546
But that style is OBJECTIVELY bad famalam.
>>59260041
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.
>>59261375
If you're placing the statement on its own line, you HAVE to use braces.
>>59260106
>(which may have been wrong ?)
It was.
K&R put the opening brace on the same line, and you should too.
>>59261382
No you don't
>>59260254
>>59260457
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.
>>59261439
idc
>>59260511
>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:
https://github.com/torvalds/linux/blob/master/Documentation/process/coding-style.rst
>>59260519
It is lined up, jackass.
>>59261382
Were you dropped on your head as a child, or have you never used C before?
>>59261435
>>59261526
No, you HAVE to do it or you end up with goto fail.
>>59261566
That's only if you have more than one statement for the condition
>>59260041
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.
>>59261624
That seems like a sign that you need to refactor your code
>>59261583
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.
>>59261639
They did say that advice came from a Microsoft programmer.
>>59261690
that's his problem
>>59260041
But what about leap seconds?
>>59261759
Someone who uses an if-else branch instead of a single return statement wouldn't be thinking of that
>>59261478
>some mediocre student-made 25 year old MINIX1 ripoff's coding-style
>relevant
>>59261357
And you are OBJECTIVELY a street-shitter.
>>59261639
>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.
>>59260554
this is bait right
>>59260041
Whatever my boss wants, because I have a fucking JOB.
>>59260041
not using the braces at all
>>59260041
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.
>>59262510
>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.
>>59262542
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.
>>59260088
This. There is no other correct answer.
>>59260088
This.
>>59260041
I preferreturn (hours < 24 && minutes < 60 && seconds < 60)
>>59260041
Depends on the language
>>59260088
>leaving in magic numbers
if (a = 2).
return a;
else
return 0;
>>59260041
boi im about to lay some shit on yaif (age <= 0 || weight <= 0)
{
System.out.println("Please retry and ensure both values are whole numbers.");
}
else
{
if (age <= 10)
{
if(weight < 80)
{
System.out.println("You must ride the " + rc[0]);
}
else if(weight >= 80 && weight <=200 )
{
System.out.println("You must ride the " + rc[1]);
}
else if(weight > 200)
{
System.out.println("You must ride the " + rc[2]);
}
}
else if (age <= 20)
{
if(weight < 80 )
{
System.out.println("You must ride the " + rc[3]);
}
else if(weight >= 80 && weight <=200 )
{
System.out.println("You must ride the " + rc[4]);
}
else if(weight > 200)
{
System.out.println("You must ride the " + rc[5]);
}
}
else
{
System.out.println("You must ride the " + rc[6]);
}
}
}
Lining up curly braces is how all the old school programmers do it, so I picked it up as well.
Top hands down.
Top is old school, which the way I like itvoid 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;
}
>>59263620
there should be a space between if and the (
>>59263454
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
>>59263747if (hours < 24)
if (minutes < 60)
if (seconds < 60)
return true;
else
return false;
>>59263732
muh leap seconds
return hours < 24 && minutes < 60 && seconds < 60 ? true : false;
This is the best if. Prove me wrong.
PS: You can't.
>>59262030
>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.
>>59262161
That is the proper, K&R style.
>>59262510
>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.
>>59261439
Doesn't necessarily mean it's right.
>>59263708
>K&R C
>not old school
Top is pajeet/CS grad tier.
>>59263897
You can just return the result of the boolean expression you retard.
>>59264101
Nor does any other style guide. The best style is the declared style of whatever project you're doing, but the Linux style guide is by far the most relevant in a general sense, because of it's widespread use and significance.
That particular example is also the style the creator of C used, so it's even more so relevant than Google's style guide, considering Google is trying to move away from C.
return (COND) ? SOME_THING : SOME_THING_ELSE;
// If I only need a boolean returned, I forgo the ?:
// If I need nested ifs, I do this:
if (COND1) {
SOME_THING;
if (COND2) {
SOME_THING_ELSE;
} else if (COND3) {
ANOTHER_THING;
}
}
// I tend to use switches, where viable.
>>59260041if(condition)
{
return true;
}
return false;
Really depends on the context.
If both the conditional and the code to perform is simple and easy to understand at a glance, I will condense it in a way that makes use of the visual space, but otherwise more clear separators are necessary.if( (12 < hours && hours < 24) && minutes < 60 || seconds != 0)
{
clock.backgroundColor = RGBA( 255, 125, 125, 0.7);
}
else {
SoundUtilities.playSound( DEFAULT_BEEP_SOUND, NULL);
}
if( milliseconds < 24*60*60*1000)
setPriority( 1);
else
doThingNow();
Just like with writing, style guides are nice, but in the end the human eye is more complicated than a few chapters of formal logic.
>>59264115
>falling for b8
>>59260041
I prefer not to be autistic "coder" at all. Do real shit, stupid cunts.
>>59260041if (bool) {
return true
} else {
return false
}
OP is a faggot
>he still thinks less lines makes his code faster
Pajeet pls
>>59264105
Funny, because most c programmers never used the K&R style.
>>59261330
>not using ycm gotodeclaration or equivalent
>>59264931
[citation needed]
>>59260041
I prefer the langauge/community standard. When writing C/C++/C# etc, I'll use the former, when writing Java/JavaScript etc I'll use the latter as they tend to be the language standard, because I'm not an autistic spaz who thinks one is better than the other.
+1 to golang for building that shit into the default tooling
Neither.
>return (hours < 24 && minutes < 60 && seconds < 60) ? true : false
>>59260172
>>59261439
https://nakedsecurity.sophos.com/2014/02/24/anatomy-of-a-goto-fail-apples-ssl-bug-explained-plus-an-unofficial-patch/
>>59264018
>either
>relevant
>>59266782
What does that have to do with not using braces? Some retard inserted an extra line for no reason.
>>59267035
>Some retard inserted an extra line for no reason.
More like
>Some (((retard))) inserted an extra line """"""""""for no reason""""""""""
>>59263620
That looks fucking awful.if (age <= 0 || weight <= 0) {
System.out.println("Please retry and ensure both values are whole numbers.");
} else {
if (age <= 10) {
if (weight < 80)
System.out.println("You must ride the " + rc[0]);
else if(weight >= 80 && weight <=200 )
System.out.println("You must ride the " + rc[1]);
else if (weight > 200)
System.out.println("You must ride the " + rc[2]);
} else if (age <= 20) {
if (weight < 80 )
System.out.println("You must ride the " + rc[3]);
else if(weight >= 80 && weight <=200 )
System.out.println("You must ride the " + rc[4]);
else if(weight > 200)
System.out.println("You must ride the " + rc[5]);
} else {
System.out.println("You must ride the " + rc[6]);
}
}
is much better.
>>59260041
They both compile to the same machine code so take the bogpill and do whatever you want
>>59267035
If th original code wasif ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
{
goto fail;
}
and an additional line was inserted, like this:if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
{
goto fail;
goto fail;
}
it wouldn't have caused a problem. I guess the general case is that a programmer may insert additional lines into an if statement, and may be visually tricked into believing that they are part of the if statement, since they would have the same indentation level, when in fact they are not. I know this is not the case here, and the programmer has most likely just accidentally pressed the duplicate line shortcut.
>>59260058
this
>>59260310
Am I the only one who usesif (condition), but also usesfunction(parameters)? It feels strange when there is a space between the function name and the parentheses.
Arguing over coding styles is stupid. None of them is objectively "more readable" or whatever, all of them have cases when they are ugly and cases when they shine. Name dropping people and organizations like Linus Torvalds and Google, who have entirely different views on coding styles only proves my point further. If one style really was objectively superior, these "smart guys" would all be in agreement.
>>59269670
Isn't that like extremely common? Functions are the only construct where I don't put a space before the opening parenthesis.
>>59270210
I only sawfunction (parameters)so far for some reason, but I guess I just missed it when people usedfunction(parameters)because that just looked natural.
>>59270235
As far as function declaration/definition goes, it's up to preference, but if you call a function like foo (), it'd be pretty weird.
>>59260041
~~~
switch (hours < 24 && minutes < 60 && seconds < 60) {
case 0:
// do stuff
break;
case 1:
// do other stuff
break;
}
~~~
This is useful because then you can do
~~~
switch (a << 1U | b) {
case 1U << 1U | 1U:
// do stuff
break;
case 0U << 1U | 1U:
// do other stuff
break;
case 0U << 1U | 0U:
// do more stuff
//etc..
~~~
which is better for the branch predictor in certain cases.
>>59260098
>premature return is bad form
your stupid
>>59260088
Please adhere to the coding style of the project, this is your second warning this month. Even Mr. Pajeet sticks to the rules.
>Fired.
>KYS
>>59261478
OMG we'll have to start calling it GNU vs. Linux.
>>59260106
>the /g/ way
Please die
>>59260041return hours < 24 && minutes < 60 && seconds < 60
wew
>>59260058
fpbp
>>59271187
You fall for the troll.
>>59260041
if (cond)
{line1;
line2;
line3;}
else
{asd;
asdasd;}
You weak fucking homosexuals, why haven't you killed yourselves yet?
My tabs are two spaces, how does this make you feel?
out of the way, kids
>>59260041if (cond)
{
return true;
}
else // why do you need 3 lines of separation?
{
return false;
}if (cond) {
return true;
} else { // 1 line of separation is too cramped
return false;
}if (cond) {
return true;
}
else { // 2 lines is perfect
return false;
}
does anyone else do this?
>>59260041
Either:if ((hours < 24) && (minutes < 60) && (seconds < 60))
return true;
else
return false;
Or:return ((hours < 24) && (minutes < 60) && (seconds < 60));
Out fucking played.return time->checkValid();
>>59260041
For short expressions:if hours < 24 && minutes < 60 && second < 60 then true
else false
For longer ones:if hours < 24 && minutes < 60 && second < 60 then
true
else
false
>>59260041if (hours < 24 && minutes < 60 && seconds < 60)
return true;
else
return false;
>>59260041
top C#
bottom Java
>>59260041if hours < 24 and minutes < 60 and seconds < 60:
return True
else:
return False
>>59260088
winrar, everybody else btfo
>>59260088
This with parentheses.
>>59260041
>>59260088hours < 24 && minutes < 60 && seconds < 60 ? true : false
now fuck off
>>59273126
> disgusting
> useless ? :
> forgot return
> forgot ;
>>59260265
I use this.
} else {
is big disgusting.
>>59273126
Totally pointless & unnecessary ternary.
>>59271251
as do you, friendo!
>>59260088
>>59260041
>>59263747
>>59263835
>>59263897
>>59271187
>>59272263
>>59272647
>>59272854
>>59273126
Your programs crashed on December 31, 2016 at 23:59:60
>>59273164
>explicit returns in ternary
spotted the dropout
>>59273245
return before the whole expression you fucking degenerate moron
function(argument1, argument2)
if (condition1 or (condition2 and argument1)) then
return true
end
return argument2
end
>>59270374
What about my "stupid"?
>>59260083
>>59260098
What would be the alternative?
>>59273233
>program crashing because something happens to return false instead of true
That sounds like a personal problem to me.
>>59262542
let's remove whitespace entirely, then
The former
#define begin {
#define end }
if (condition)
begin
//something
end
else
begin
//something
end
>>59272854
This. Meaningful whitespace is GOAT
>>59260088
You're fired.
>>59275464
holy shit, nice!!