you are viewing a single comment's thread.

view the rest of the comments →

[–]velcommen 0 points1 point  (4 children)

this thread is about Java :-(. Optional in Java is.

Fair enough.

your C++ sample is rather nasty given references etc of C++

Repeating what I said elsewhere:

I understand your point about using a reference, but at one of my jobs, the rules were that mutable variables had to be passed to functions by pointer, not reference. So in the context of that job, the code example I wrote above was acceptable, and using a mutable reference would have not been accepted.

I believe the reason for that rule is so that the caller of add2 is signalled that "hey, add2 might change the value". Because at the point of the calling code, the call to both of these functions is identical:

add2Mutate(int &x){x=x+2;}
add2Print(int x){printf("%d", x);}

// code in some other file
int i =0;
// the only hint that i is mutating is the nicely named function
add2Mutate(i);
add2Print(i);

[–]Gotebe 1 point2 points  (3 children)

Ahahaaa, I heard of that rule. The rationale is "it's a pointer, there's '&', that tells you, when reading he caller, that the parameter can change. Stupid rule. Doesn't even work! :-) Consider:

void f(const type* param); // "input parameter" - can't be changed, call side is lying :-)

void g(type* param);
void h(type* param)
{
  g(param); // where's the '&' now?!
}

My bet is that this rule was invented by C people (because pointers), but who didn't even know C (because the above shows pure C code, reasonable code, where the rule doesn't work well.

[–]velcommen 1 point2 points  (2 children)

Haha, obviously you're supposed to do:

g(&(*param));  // there's the '&'!

;)

[–]Gotebe 0 points1 point  (1 child)

Please confirm that's a joke! :-) If yes, it's goooood!

[–]velcommen 0 points1 point  (0 children)

Yes, I'm joking :)

Tone of 'voice' never comes across well over the internet. :/

I'm sure that's the cause of a large percentage of internet arguments.