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

is it ok to do this

Name: Anonymous 2007-06-23 13:51 ID:6CyOAcE6

instead of

int f(a) {
if(a == 3) return 1
else return 0;
}

to write

return (a == 3);

? thanks in advance

Name: Anonymous 2007-06-23 13:54 ID:SQp2H7ab

Yes.
It's the first step in the long path of EXPERT PROGRAMMING.

Name: Anonymous 2007-06-23 13:55 ID:SPHWmNLX

depends on language

Name: Anonymous 2007-06-23 14:01 ID:6CyOAcE6

>>2

should i cast it or not?

eg

short *f(a) {
return (short)(a == 3);
}

Name: Anonymous 2007-06-23 14:04 ID:6CyOAcE6

>>3
oh forgot to mention, C.

Name: Anonymous 2007-06-23 14:08 ID:Heaven

>>1,4,5 here

>>4
there's a mistake there, f is supposed to return a short value not a pointer to a short

Name: Anonymous 2007-06-23 14:10 ID:U7byP6EN

You should certainly not cast it, also GTFO go read a book on C faggot

Name: Anonymous 2007-06-23 14:15 ID:Heaven

>>7
then why is NULL defined as ((void*)0)

example,


void *f(int a) {
if(a == 1) return (void*)1;
else return NULL;
}

and

void *f(int a) {
return (a == 1)
}

Name: Anonymous 2007-06-23 14:27 ID:U7byP6EN

>>8
Its not, maybe it is on your implementation but its not defined that way in C.

now, FUCK OFF.

Name: Anonymous 2007-06-23 14:29 ID:taeNzC9J

>>1
Come back when you will have read K&R.

Name: Anonymous 2007-06-23 14:36 ID:T9xsbd5/

>>1

not only it is ok, it's the right way to do it. Anything else is wrong.
Not only that, but suppose you want to return something like

if (a == 3) return 'a';
else return 'b';

it's not the proper way to do it. The proper way is to use the ternary operator, ?:, like this

return a == 3 ? 'a' : 'b';

the same goes for stuff like

if (a == 3) then val = 4;
else val = 10;

you better write

val = (a == 3 ? 4 : 10);

the same goes if you have something like

if (a == 3)
f(a,b,c,d,e,f);
else
f(a,b,c,d,e,g);

just write
f(a,b,c,d,e, a == 3 ? f : g);
it's cleaner, believe me.

Name: Anonymous 2007-06-23 15:20 ID:Heaven

>>11
the right way?
the ternary operator is optional, the language would still work without it.

Name: Anonymous 2007-06-23 15:26 ID:Heaven

NULL is a pointer, not a boolean.

Name: Anonymous 2007-06-23 18:34 ID:+dLb/W63

I hope your daughter gets raped by negroes on cocaine.

Name: Anonymous 2007-06-24 14:09 ID:BAPmN5fc

>>11
The C syntax for the ternary operator is the biggest fail in human history. Do not use it in C, Java or any other language that contain this faggotry.

Name: Anonymous 2007-06-24 14:25 ID:NI/INIVN

Nothing is wrong with the ternary operator yet it is not mandatory. Sometimes ternary is avoided in code that you debug, it makes it easier, and there are other reasons for not using ternary.

But I spam it like hell at every chance I get anyway.

Name: Anonymous 2007-06-24 14:57 ID:3Y93wp6f

>>12
languages would still work with the few brainfucks operators. your point?
>>15
yes, the syntax is a bit awkward. live with it.

Name: Anonymous 2007-06-24 15:02 ID:3Y93wp6f

>>16

awesome id

Name: Anonymous 2007-06-24 16:48 ID:NI/INIVN

>>18 Thank you.

Name: Anonymous 2007-06-24 17:03 ID:lvFW2NVj

>>1
return a == 3;
Everything else is for faggots.

>>11
This man speaks the truth.

>>15
This man is a faggot.

>>17
No, the syntax is not awkward. The syntax is perfect. Live with perfection or GTFO C.

Name: Anonymous 2007-06-24 17:16 ID:U804vBC+

return a-3;
ftw

Name: Anonymous 2007-06-24 17:27 ID:UXeElWgq


xor  eax,eax
cmp  dword ptr [a],3
sete al

Name: Anonymous 2007-06-24 18:05 ID:Heaven

>>21
Except OP is returning 1 if a IS equal to 3.

return !(a-3);


Also sage.

Name: Anonymous 2007-06-24 19:24 ID:lvFW2NVj

You are not an EXPERT ENTERPRISE PROGRAMMER.

Comparison cmpAEq3 = new Comparison(new ComparisonOperator(OperatorSignature.getInstance().getSignatureForString("==")));
cmpAEq.bindLeft(a);
cmpAEq.bindRight(Number(3));
FunctionResult res = new FunctionResult(new BooleanResult(cmpAEq.performComparison().toBoolean()))
return FunctionResult.getValue();

Name: Anonymous 2007-06-24 19:43 ID:Oqser/3/

>>24
do it in XML THEN you can claim TRUE ENTERPRISE.

Name: Anonymous 2007-06-25 9:09 ID:JNnJp7D1

>>25
XML? We don't want to reinvent the wheel here. Use MathML, then you're thinking business logic.

Name: Anonymous 2007-06-25 19:06 ID:A5dFBmqk

ternary operator is good for avoiding duplication of code

Name: Anonymous 2007-06-26 8:22 ID:8Ffse2mk

XML is like violence.  If it doesn't work, use more.

Name: Anonymous 2007-06-26 8:42 ID:Heaven

you know what'd be nice? if you could do this:
a?=b:c

Name: Anonymous 2007-06-26 8:43 ID:c5nsYvcr

true

Name: Anonymous 2007-06-26 9:02 ID:LnR8zRru

false

Name: Anonymous 2007-06-26 10:17 ID:dFYg3IDF

>>29
and what the fuck is that supposed to mean?
if a true assign it the value of b else c?
you can do it, but you have to use one more byte, oh god ONE MORE BYTE IN THE SOURCE!
a=a?b:c

Name: Anonymous 2007-06-26 10:46 ID:x1pe7n5g

Maaaaacrooooo

Name: Anonymous 2007-06-26 10:55 ID:hsgZIkL4

C lacks proper macros :(

But at least you can
#define _if(c, t, f) ((c) ? (t) : (f))

Name: Anonymous 2007-06-26 11:19 ID:H9Np5te9

>>32

not only that, but why would you want to do that?

Name: Anonymous 2007-06-26 12:04 ID:Heaven

>>34
#define _if(c, t, f) ((c) ? (t) : (f))

stop abusing the preprocessor with your bullshit.

Name: Anonymous 2007-06-26 12:50 ID:H9Np5te9

bitches don't know about my enterprise preprocessor abuse

#define LOOPFDSET_ITER_INIT_GETNEXT_GETSOCKET(iter, init, getnext, getsocket) for(iter = init; iter != NULL; iter = iter getnext) { if(iter getsocket != -1) { FD_SET(iter getsocket, &fdesc); maxDescrID = ((iter getsocket > maxDescrID) ? iter getsocket : maxDescrID); } }

LOOPFDSET_ITER_INIT_GETNEXT_GETSOCKET(iC, cellList, ->next, ->socket)
LOOPFDSET_ITER_INIT_GETNEXT_GETSOCKET(iQC, pendingCells.head, ->next, ->socket)
LOOPFDSET_ITER_INIT_GETNEXT_GETSOCKET(srv, srvlst, ->next, ->fd)

Name: Anonymous 2007-06-26 12:53 ID:hsgZIkL4

>>36
I wunt #defmacro waah waah waah

Name: Anonymous 2007-06-26 12:58 ID:h1D/O9nF

>>32
some_ungodly_long_javaesque_identifier=some_ungodly_long_javaesque_identifier?true_value:false_value;

Name: Anonymous 2007-06-26 13:14 ID:H9Np5te9

>>39
You'll see, if you have

a=a?b:c
then 'a' has to be a boolean. And b and c have to be booleans as well. furthermore, they have to be variables, elsewhere they won't have any sense

a=a?true:false

is the identity.

a=a?false:true

is the negation (I don't know if java bools have an imperative negation method)

a=a?b:true

is equivalent to !a || b, again, an imperative or method handles it.

and so on.




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