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

Ternary Operator

Name: Anonymous 2010-01-08 8:07

ALL HAIL!

Name: Anonymous 2010-01-08 8:16

pig disgusting.
only used by amateurs who're trying to be fancy, except on rare occasions.

Name: Anonymous 2010-01-08 8:24

>>2
Butthurt fag, you mad?

Name: Anonymous 2010-01-08 8:28

>>3
back to /b/, please.

Name: Anonymous 2010-01-08 9:21

>>2
Yeah because ?: is sooooo fancy amirite

Name: Anonymous 2010-01-08 9:24

Name: Anonymous 2010-01-08 9:32

It should have been the default behaviour of the if statement. There should have never been a need to make a ternary operator. There should be no real statements, almost everything should be an expression. if (a) b c should itself return the value. A value which you could further use. If the value is unused, there is no harm, the compiler just frees those resources up. Languages which knew how to do it right, just had almost everything returning a value(think Lisp, ML, but there are many others that do it right), and then you could wire the outputs directly. While I do use the ternary operator in C less than if for aesthetic reasons, even if it has the functionality if should have always had.  Its syntax looks uglier than the if statement. Default IF behaviour impedes functional dataflow.

Name: Anonymous 2010-01-08 10:18

>>5
What's "?:"? We are talking about return 1 if n < 3 else fibs(n-1) + fibs(n-2)!

Name: Anonymous 2010-01-08 10:48

FIBS MY FACT

Name: Anonymous 2010-01-08 11:15

>>8
The Tenary operator can follow the structure:
variable = conditional ? A : B

which is virtually the same as:

if(conditional)
{
   variable = A
}
else
{
   variable = B
}

Name: Anonymous 2010-01-08 12:25

>>7
GTFO, this isn't Haskell. Go back to your academic ivory tower and let real programmers get some work done.

Name: Anonymous 2010-01-08 12:48

>>11
Hi, been a long time lurker on this board.. but I feel I have to speak out at this moment.

Never on the 4Chan programming board have I seen someone be so horrible and with no provocation, utterly appalling.

That was uncalled for and just plain rude.

Name: Anonymous 2010-01-08 12:51

>>11
I don't write Haskell, and I do plenty of work for real, sometimes dirty work myself. I'm not going to argue with you, as you have clearly not programmed in a language where almost everything is an expression.

Name: Anonymous 2010-01-08 13:17

>>11
LOL!!!!!!!!!!!

Name: Anonymous 2010-01-08 13:33

The ternary operator has its uses, that's why it was built into C and kept in Java. Only to be used it the equivalent if statement is less elegant,and never, ever nest them.

Name: Anonymous 2010-01-08 14:03

and never, ever nest them.
( ≖‿≖)

Name: Anonymous 2010-01-08 15:06

>>15
Nice dogmatic view, Jedi scum.

Name: Anonymous 2010-01-08 16:41

I think this thread needs a program written entirely in nested ternary operators

Name: Anonymous 2010-01-08 17:23

>>18
Don't say his name three times! FrozenVoid will appear.

Name: Anonymous 2010-01-08 17:25

>>15
Only to be used it the equivalent if statement is less elegant
In other words, use it whenever possible.

Name: Anonymous 2010-01-08 17:52

>>10
>virtually the same
Sometimes it is but sometimes the ternary operator can cost you a temporary variable or causes strange conversions.
http://dis.4chan.org/read/prog/1239221504

Name: Anonymous 2010-01-08 18:51


echo 'an' . ( ! $plural ? 'us' : 'ii');



echo 'an';
if( ! $plural)
{
    echo 'us';
}
else
{
    echo 'ii';
}

Name: Anonymous 2010-01-08 18:54

>>22
The second is way, way clearer. Learn to program.

Name: Anonymous 2010-01-08 19:00


FROZENVOID QUALITY!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE *f;
char s[512];
int x(int n, char *m) {
  !strlen(m) ? exit(n) : fprintf(stderr,"%s\n",m); exit(n);
}
int r(void) {
  fgets(s,sizeof(s),f) ? printf("%s",s) : x(0,""); r();
}
int main(int argc, char **argv) {
  argv[1]?(f=fopen(argv[1],"r"))?r():x(1,"fopen() failed"):x(1,"usage: tcat FILE");
}

Name: Anonymous 2010-01-08 19:05

>>22
( ! $
what the fuck is this shit

Name: Anonymous 2010-01-08 19:08

>>25
i assume its some kind of toy language

Name: Anonymous 2010-01-08 19:10

>>24
Lacks #include "void.h"

Name: Anonymous 2010-01-08 19:29

>>23
How can you seriously make the argument that a program requiring you to decode nine lines of code is clearer than a single short line of code that replaces repeated function calls with a few constants? Either this is some kind of knee-jerk reaction to the use of the conditional operator, or you just need to learn to program. Either way, IHBT.

Name: Anonymous 2010-01-08 21:10

>>27
Lacks //handles all the common functions,#defines,#ifdefs and #includes

Name: Anonymous 2010-01-08 21:15

>>29
Lacks my $anus

Name: Anonymous 2010-01-08 22:01

>>28
One is more abstract and has a higher information density. The other is spread out more reducing the cognition required to interpret it.

Name: Anonymous 2010-01-08 22:13

>>7
I agree.  I actually created a better approach to the if ... else ... block statements in C.  It makes more sense logically, and is much more functional in flow.

flag = cond ? 1 : 0;
for (i = flag; i == 1; i++)
{
   // if condition logic...
}
for (i = flag; i == 0; i++)
{
   // else condition logic...
}

This approach is superior because it doesn't divert the logic flow like if else blocks, and only uses the ternary operator.  The loops make a lot more sense then conditional code, because they basically say "run it for as many times as it is true or false".

Name: Anonymous 2010-01-08 22:16

>>31
That's patently false. The information density is precisely the same. Both blocks of code complete the same task. No matter how you quantify "density," there is another definition that will just as certainly show that pro to be a con; there is no clear winner because the two blocks are functionally equivalent.

As far as "spreading out," the ternary operation could also be written

echo 'an';
!$plural ?
    echo 'us':
    echo 'ii';

This is a matter of taste. Some people prefer

if(!$plural) {
    echo 'us';
} else {
    echo 'ii';
}

To each their own.

The only reason you find the ternary operator as written to require more "cognition" is because you're not used to seeing it. if/else conditionals are encountered much more frequently because it's more flexible. But for matters of simple if then else one liners, it makes sense to use a simpler notation. Just as we use contractions instead of writing do not, could not, should not every time. These are common problems that benefit from simpler solutions.

Name: Anonymous 2010-01-08 23:50

>>31
One has more room to hide mistakes or dirty tricks, making it necessary to carefully read the whole thing, while there's nothing unexpected that could happen with a variable name and two constants. The situation is even worse when the if statements in question set a variable: anything could happen between the if statements and that variable's use. Underusing the ternary operator is bad style for the same reason that overusing goto is bad style.

Name: Anonymous 2010-01-09 0:10

>>34
advocating imperative style
comparing functional style to goto usage


...however it's true that lambda is the ultimate goto.

Name: Anonymous 2010-01-09 1:08

>>35
I'm not advocating imperative style... and I'm not comparing functional style to goto use.

Name: Anonymous 2010-01-09 15:03

Bumping the only sensemaking thread on /prog/

Name: Anonymous 2010-01-09 16:25

DOUBLE BUMP for great justice!!!

Name: Anonymous 2010-01-09 17:52

theres an emplentation of ternatry operation in scheme?

Name: Kao-Moji 2010-01-09 18:01

About two or three days ago someone started a thread about a website called javabat.com where a user could solve small Java exercises in order to learn the language.

I have decided to swing on some of these exercises with nested ternary operations and finals.

public final boolean posNeg(final int a, final int b, final boolean negative) {
 return negative ? a < 0 && b < 0 : a < 0 && b > 0 || a > 0 && b < 0;
}


public final int strCount(final String str, final String sub) {
 return str.length() == 0 ? 0 : str.startsWith(sub) ? 1 + strCount(str.substring(sub.length()),sub) : strCount(str.substring(1),sub);
}


public final int count11(final String str) {
 return str.length() == 0 ? 0 : str.startsWith("11") ? 1 + count11(str.substring(2)) : count11(str.substring(1));
}


public final int countAbc(final String str) {
 return str.length() < 3 ? 0 : str.startsWith("abc") || str.startsWith("aba") ? 1 + countAbc(str.substring(1)) : countAbc(str.substring(1));
}


public final int countX(final String str) {
 return (str.length() == 0) ? 0 : (str.startsWith("x")) ? 1 + countX(str.substring(1)) : countX(str.substring(1));
}


public final int redTicket(final int a, final int b, final int c) {
 return a + b + c == 6 ? 10 : a == b && a == c && b == c ? 5 : a != b && c != a ? 1 : 0;
}


public final int blackjack(int a, int b) {
 if (b > a) { b ^= a; a ^= b; b ^= a; }
 return a > 21 ? b > 21 ? 0 : b : a;
}

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