4chan archive /g/ (index)
similar threads
2012-10-02 03:39 28004756 Anonymous (question.jpg 327x348 15kB)
1 min later 28004770 Anonymous
>>28004756
give me curly braces any day
2 min later 28004778 Anonymous
If no curly braces are required, why put them?
2 min later 28004779 Anonymous
>>28004756
Neither!
x = (x == 0) ? 1 : x;
3 min later 28004799 Anonymous
>>28004779
Fucking beautiful.
/thread.
3 min later 28004804 Anonymous
>>28004779
ew.
Masterrace:
if (x == 0) {x = 1}
4 min later 28004815 Anonymous
>>28004778
Clarity and ease of reading
If you need more than one statement being executed for the if
etc
4 min later 28004817 Anonymous
>>28004778
Because some people think it becomes less readable and more of a clusterfuck if you have a bunch of them stuck on top of each other. It's easier to tell where the if begins and ends this way.
>>28004779
I don't even understand how that works.
4 min later 28004822 Anonymous
>>28004778
Because this isn't Python, boy.
5 min later 28004833 Anonymous
>>28004817
x = (condition) ? (return this if true) : (else return this);
x = (y > 5) ? "greater" : "smaller";
5 min later 28004834 Anonymous
I go like
if(someshit)
x=1;
6 min later 28004839 Anonymous
>>28004756
always need more curly braces
>>28004779
What language is that?
6 min later 28004841 Anonymous
>>28004833
Oh. I've never used ?s, thanks.
6 min later 28004842 Anonymous
This thread again.
Le Deja Vu face.
7 min later 28004856 Anonymous
>>28004778
This.
I mean this is the commonly cited reason as to why not:
if (x == 0)
x = 1;
y = 7; // whoops I am an idiot this always executes regardless of the indentation level
But honestly how retarded do you have to be? Has anyone actually ever done this?
7 min later 28004858 Anonymous
>>28004842
Well, I learn something new every time it's posted.
7 min later 28004861 Anonymous
>>28004779
You're a retard
x = x == 0;
8 min later 28004869 Anonymous
>>28004839
Any language with C-like expressions.
8 min later 28004878 Anonymous
>>28004861
That doesn't do the same thing at all. The value of x should be preserved if x != 0.
8 min later 28004879 Anonymous
>>28004841
http://en.wikipedia.org/wiki/%3F:
9 min later 28004884 Anonymous
If one statement only, no curly braces. If one (but maybe more could be put in), then curly braces. Also, space between if and (, ) and {
9 min later 28004889 Anonymous
>>28004779
What do you call that?
9 min later 28004895 Anonymous
>>28004878
Oh, I'm the retard. Never mind.
10 min later 28004901 Anonymous
>>28004856
No, I mean fucking looking at it. It hurts my eyes. It's so obvious.
10 min later 28004903 Anonymous
>>28004889
Ternary
11 min later 28004914 Anonymous
x=~x&1
11 min later 28004917 Anonymous
>>28004903
Yeah, that's the name of the "?" but what's the name of the whole thing?
12 min later 28004931 Anonymous
>>28004917
A conditional assignment
13 min later 28004936 Anonymous
>>28004917
Ternary
13 min later 28004940 Anonymous
>>28004914
See >>28004878
13 min later 28004946 Anonymous
>>28004917
Assignment of a ternary operation.
14 min later 28004952 Anonymous
>>28004946
>>28004936
>>28004931
>>28004903
Thanks
14 min later 28004958 Wolenber
>>28004779
>Not using a language that can shorthand that even further.
x ?== 0 ? 1;
15 min later 28004960 Anonymous
>>28004861
This also presupposes that 1 equals true.
16 min later 28004974 Anonymous
>>28004914
That would set x to 1 for a fuck ton of different values, not 0 only.
If x has any of the following values
0000 0010
0000 0110
0000 0100
0000 1100
0000 1110
0000 1000
...
would all case x to be set to 1 after the assignment.
17 min later 28004991 Anonymous
>>28004960
The only C syntax language where this isn't true, would be Java. And lets face it, nobody should use Java.
17 min later 28005000 Anonymous
x+=((!x)*(x==0));
18 min later 28005005 Anonymous
if(x==0)
{
x=1;
}
18 min later 28005010 Anonymous
>>28004960
Is that so wrong? It's true of C.
[~]$ vim true.c
[~]$ cat true.c
#include <stdio.h>
int main()
{
printf("%d\n", 6 == 2 * 3);
return 0;
}
[~]$ gcc true.c -o true
[~]$ ./true
1
18 min later 28005015 Anonymous
x = 1 if x.zero?
Plebs.
19 min later 28005021 Anonymous
I can't believe anyone uses
if (){
do;
}
instead of...
if ()
{
do;
}
or just...
if ()
do;
Braces go on the next line. It just reads better.
19 min later 28005033 Anonymous
Best solution:
x+=(x==0);
21 min later 28005053 Anonymous
>>28004974
What if that's OP's private static final will?
21 min later 28005054 Anonymous
>>28005033
That one is neat and only slightly less clear than the standard OP solution.
21 min later 28005057 Anonymous
>>28004958
The competition isn't for the 'shortest', it's simply for the 'best'. The idea is to take clarity and other factors into account.
With >>28004779
it's clear what's going on, there's even an explicit else clause.
21 min later 28005061 Anonymous
>>28005033
This is not the most readable solution.
It's likely that every correct solution in this thread optimises to the same code.
21 min later 28005064 Anonymous
switch (x) {
case 0:
x = 1;
break;
default:
System.out.printf("I have sucked %d dicks!\n", x);
break;
}
22 min later 28005069 Anonymous
>>28005000
Do you even redundancy?
Essentially, you could have just written
x += x == 0
or
x += !x
As
(!x) * (x == 0)
will ALWAYS be either 1 * 1 or 0 * 0
22 min later 28005074 Anonymous
>>28005053
>>28005064
Java pls go.
22 min later 28005086 Anonymous
>>28005021
Enjoy wasting valuable space. I for one prefer opening braces on the same braces, it reads just as well and the indentation is more than enough to identify the code block.
25 min later 28005122 Anonymous
while (x == false) {
x = 1;
break;
}
25 min later 28005127 Anonymous
>>28005086
> saving bytes
> 2012
> Terabyte-era
26 min later 28005150 Anonymous
!x && (x = 1);
28 min later 28005167 Anonymous
>>28005127
>not saving bytes
>any era
28 min later 28005170 Anonymous
>>28005127
It's nothing to do with bytes, it screen real estate, it's nicer to be able to see more code at once.
29 min later 28005198 Anonymous
>>28005033
>>28005150
>>28004779
These are the best.
>>28004861
This if you don't have to preserve the value of x, or x is always 0 or 1.
31 min later 28005211 Anonymous
>>28005150
>>28005122
>>28005069
>>28005033
>>28005000
Who the fuck told you niggers that we're talking about booleans?
31 min later 28005217 Anonymous
>>28005150
Except for the ternary, I find this to be the most clear (in terms of readability).
32 min later 28005231 Anonymous
>>28005211
Do you even C?
You know that !x evaluates to true iff x has the value of 0. And that it evaluates to false for any other value.
32 min later 28005237 Anonymous
(if (eq x 0)
(setf x 1))
32 min later 28005238 Anonymous
try {
if (1/x == 0);
} catch (ArithmeticException e) {
x = (int) 1.42;
}
33 min later 28005254 Anonymous
>>28005170
Finally, someone who is not an idiot. It's been proven by research that being able to see the entire function/algorithm helps you understand it and figure things out/find bugs faster.
34 min later 28005264 Anonymous (plebres.png 1440x2560 410kB)
>>28005170
>2012
>coding on a netbook
35 min later 28005289 Anonymous
>>28005264
>using the comic sans of monospace fonts
36 min later 28005303 Anonymous
>>28005238
jmp_buf e;
void sighndlr(int i)
{
longjmp(e, i);
}
// ... somewhere inside a function
signal(SIGFPE, sighndlr);
if (setjmp(e) == SIGFPE) {
x = 1;
} else {
1 / x;
}
36 min later 28005310 Anonymous
>>28005264
>Portrait catleap
Nice.
But still, when you start getting into thousands of lines of code, it really does make a difference to see all you can at once without scrolling.
36 min later 28005311 Anonymous
if
(
x == 0
){
x = 1
}
38 min later 28005339 Anonymous
>>28005170
> this is what undergraduates actually believe
38 min later 28005345 Anonymous
>>28005311
WRONG!
if
(
x == 0
)
{
x = 1
}
curley braces go on NEW lines.
38 min later 28005350 Anonymous
>>28005303
Fucking this.
40 min later 28005382 Anonymous
JavaScript master race.
function ZeroToOne(n)
{
switch (n)
{
case 0:
return 1;
default:
return n;
}
}
x = [x].map(ZeroToOne)[0];
41 min later 28005388 Anonymous
>these codes
Enjoy your obfuscated code OP
42 min later 28005410 Anonymous
>>28005264
What's that font?
43 min later 28005418 Anonymous
I use to that shit on new lines, got annoying and harder to figure out what was what.
public void writeToFile(String input) throws IOException {
BufferedWriter fileOutput = new BufferedWriter(new FileWriter(writeFile));
try {
fileOutput.write(input);
} finally {
if (fileOutput != null) {
fileOutput.close();
}
}
}
44 min later 28005428 Anonymous
>>28005410
{
"caret_style": "phase",
"color_scheme": "Packages/Phix Color Scheme/Phix Dark.tmTheme",
"draw_centered": true,
"draw_indent_guides": false,
"fold_buttons": false,
"font_face": "consolas",
"font_size": 11,
"ignored_packages":
[
"Vintage"
],
"margin": 0,
"overlay_scroll_bars": "enabled",
"rulers":
[
0,
140
],
"show_tab_close_buttons": false,
"tab_size": 4,
"theme": "Soda Dark.sublime-theme",
"wrap_width": 140
//"draw_indent_guides": false,
//"fold_buttons": false
}
48 min later 28005476 Anonymous
int y = 0;
while (x) {
do {
x ^= y;
y = (x^y) & y;
y <<= 1;
} while (y);
} else
x |= 1;
50 min later 28005499 Anonymous
>>28004778
Readability.
Consistency.
Maintainability.
One of the biggest gotchas with the first version is that down the road you or someone else might end up moving that "x = 1;" to the next row and then later on just add another indented row after it (or before it).
The brackets create a clear unambiguous limit of the scope of the if statement (in human eyes, your compiler obviously doesn't give a shit but if all that mattered was how the computer parsed things we'd just be feeding computers opcodes directly).
51 min later 28005524 Anonymous
>>28005476
>while ... else
I meant if, not while
51 min later 28005525 Anonymous
>>28005499
We have discussed this. >>28004856 >>28004901
You'd really have to be a special brand of completely fucking retarded to make that mistake.
52 min later 28005533 Anonymous
>>28005525
Indian contractors.
52 min later 28005537 Anonymous
>>28005525
You've clearly never worked in any programming team larger than a handful of people.
52 min later 28005539 Anonymous
>>28005021
Compact control readability style ftw
57 min later 28005614 Anonymous
>>28005254
[citation needed]
57 min later 28005616 Anonymous
>>28005345
Neat
58 min later 28005617 Anonymous
>>28005525
1. No, just overworked/stressed/distracted (all common for real adult developers writing Real CodeĀ® as opposed to teenagers cobbling together little snippets of C and bragging about their coding skillz online).
2. Ok, so you somehow never make mistakes when you've got two different deadlines and that annoying sales exec calling you about shit he should be calling the helpdesk about every thirty minutes. Hell, even the fact that you only got five hours of sleep last night and you're currently on hour twelve of coding doesn't cause you to make mistakes. Fine, let's accept that fantasy. So, what about your coworkers? What about that junior dev who's bound to get stuck doing maintenance on your code once 1.0 is deployed/released? And what do you think happens when he accidentally introduces a showstopper bug thanks to your "efficiency" just as you're bogged down with 1.5 or 2.0 trying to finish all the new stuff before the testing deadline? Guess who just got to drop everything to spend an hour finding the bug...
1 hours later 28005717 Anonymous
>>28005617
I can't even count how many times I've heard the 'what if shit happens' argument/rant. No matter how overworked/tired/stupid/junior/indian co-workers can be, you're not going to convince me that someone will make suck a basic mistake.
Even if they do (and the project uses Allman style braces, which it should,) you'd just scroll through the code, spot a construct that just shouldn't exist in C-style languages and fix it immediately.
Maybe if you were so tired you thought you were writing Python...
1 hours later 28005727 Anonymous
>>28005717
*such
1 hours later 28005816 Anonymous
>>28005717
>...you'd just scroll through the code...
The current codebase I'm working on has production code as old as 20 years, in three different languages and over 1 MLOC of code (not counting UI templates in two different markup languages).
Did I mention that this code has been maintained by four completely different teams at different points in time?
How about the fact that one of the languages it is written in is only used for legacy core modules loaded by a "module loader module" which contains some downright ugly tricks and hacks to load these legacy modules as core modules (since technically modules can't really be dynamically loaded as core modules)?
Oh, and I've seen these things happen. It's an easy mistake if you're a human being, no matter how well-trained and talented you are.
1 hours later 28005913 Anonymous
>>28005476
>curly brackets not lining up
>commands on curly bracket lines
>while....else
pig disgusting
1 hours later 28005929 Anonymous
>>28005913
*woosh*
1 hours later 28005948 Anonymous
>>28005816
Neat. So overzealous software dev practices are necessary when maintaining something so obscenely bloated.
I guess I dodged a bullet. The place where I'm working has a similarly large project, but I'm only going to be required to work with the brand new C# rewrite. I hope to never work on something that large, and frankly these practices are never necessary for smaller projects.
1 hours later 28005966 Anonymous
>>28005948
I hope for your sake you never have to expand due to success. Bad or quick-fix coding practices almost always bite you in the ass if you have a half decent idea and end up expanding or hiring.
1 hours later 28006052 Anonymous
>>28005966
>Bad or quick-fix coding practices
Nope. It's braces on an if block. Couldn't possibly care less. I'm just going to appreciate those one or two lines of increased screen real estate.
1 hours later 28006081 Anonymous
>>28006052
>So overzealous software dev practices are necessary
is not the same as
>It's braces on an if block.
either clarify what you mean at the time of statement or don't change your position when confronted.
1 hours later 28006110 Anonymous
>>28006081
This entire thread is about braces on an if block.
1 hours later 28006117 Anonymous
ITT:
>Oh, so you put curly braces / don't put curly braces after your one-liner if-conditions, YOU ARE THE CANCER, YOU SHOULD DIE, STOP PROGRAMMING, YOU'RE A TERRIBLE PROGRAMMER!!!!!!!!!!!
Seriously?
1 hours later 28006149 Anonymous
So long as you're consistent I don't care.
1 hours later 28006172 Anonymous
>>28006117
Welcome to computer programing, where something as trivial as formatting preferences start holy wars.
1 hours later 28006217 Anonymous
>>28006110
braces on an if block hardly classify as 'overzelous software practices'.
1 hours later 28006228 Anonymous
if(x == 0)
x = 1;
1 hours later 28006234 Anonymous
>>28006172
Welcome to life, where every trivial difference in any opinion ever causes holy wars.
1 hours later 28006301 Anonymous
>>28006228
x = x == 0 ? 1 : x;
or
x == 0 && (x = 1);
You can even replace
x == 0
with
!x
if you like.
Or, if you want the shortest form possible:
x += !x;
2 hours later 28006535 Anonymous
>>28006301
booleans are not integers.
it's about clarity and neatness, not how few characters you need to type.
2 hours later 28006552 Anonymous
>>28006535
>booleans are not integers
They are in C and all C-like languages except Java.
2 hours later 28006576 Anonymous
>>28006552
*except anything running on the JVM, or the hundreds of lesser known languages with similar syntax.
2 hours later 28006604 Hupo
x=x?x:1
But to answer the actual question, I generally always use braces, unless I'm writing code for some project with different style guidelines. Or if I'm writing intentionally short code for fun.
2 hours later 28006639 Anonymous (themostinterestingman.jpg 430x539 37kB)
I don't always use curly braces,
but when I do, I use it for the sake of readability and maintainability.
2 hours later 28006958 Anonymous
>>28004804
wat
2 hours later 28007064 Anonymous
True master race:
x = 1 if x is 0
2 hours later 28007133 Hupo
>>28007064
>yoda logic
Why would anyone voluntarily do this shit?
2 hours later 28007250 Anonymous
>>28007064
>>28007133
whoa whoa, what language is THAT?
2 hours later 28007268 Anonymous
>>28007250
coffeescript
2.679 0.104