Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# Can someone simplify this method?/

Arjun Reddy
Ranch Hand
Posts: 629
Hi All,

Can anyone simplify this methid please?/

Thanks.

Michael Dunn
Ranch Hand
Posts: 4632
start again and get your curly braces to match

Arjun Reddy
Ranch Hand
Posts: 629
It was asked to me in an Interview and I freaked out and answered something. I don't rem tho what I put in. Just wanted to know how you guys would solve it.

Thanks.

Michael Dunn
Ranch Hand
Posts: 4632
solve what?

look at this block

Arjun Reddy
Ranch Hand
Posts: 629
Originally posted by Michael Dunn:
solve what?

look at this block

Yes I got confused there too. Guess the answer would be like we can not simplify the method and state the thing you said above as the reason.

Thanks.

Arjun Reddy
Ranch Hand
Posts: 629
Sorry. Got posted twice.
[ December 26, 2008: Message edited by: Arjun Reddy ]

Steve Fahlbusch
Bartender
Posts: 605
7
The code inside the method can be reduced to:

Juva Yuva
Greenhorn
Posts: 17
Originally posted by Steve Fahlbusch:
The code inside the method can be reduced to:

To be more precise

Rob Spoor
Sheriff
Posts: 20667
65
Originally posted by Juva Yuva:
To be more precise

To be even more precise:

No need for the else here. I even set up my Eclipse to warn me if I use unnecessary else blocks.
[ December 26, 2008: Message edited by: Rob Prime ]

Juva Yuva
Greenhorn
Posts: 17
Originally posted by Rob Prime:
To be even more precise:

No need for the else here. I even set up my Eclipse to warn me if I use unnecessary else blocks.

Yeah . But If the code inside the if statement is not "return" statement then if , else if makes difference

Campbell Ritchie
Sheriff
Posts: 50240
79
Rob is correct to omit the "else" after a return.

I am sure Rob has got it right; you can reduce that to a single return statement, something like this:

return fruminous && !uffish && manxome ? BANDERSNATCH : JUBJUB_BIRD;

Rob Spoor
Sheriff
Posts: 20667
65
Actually it's

Steve Fahlbusch
Bartender
Posts: 605
7
i would concur, but then again if this was for an interview i would shy away from the ? : as many folks don't like using this construct.

Harvinder Thakur
Ranch Hand
Posts: 231
Originally posted by Campbell:

return fruminous && !uffish && manxome ? BANDERSNATCH : JUBJUB_BIRD;

I think the solution provided by Ritchie will fail for the case:
fruminous = TRUE && uffish = FALSE && manxome = FALSE
as it would give JUBJUB_BIRD whereas it should give BANDERSNATCH.

I agree with Rob's solution. That's cool Rob. Please tell me. How did you get the solution? How should we approach such questions? :roll: After looking at your solution it seems to be correct. But really i had no clue when i first looked at the question.

Mike Simmons
Ranch Hand
Posts: 3090
14
[Steve]: i would concur, but then again if this was for an interview i would shy away from the ? : as many folks don't like using this construct.

Whereas many others people prefer it; that can go either way. For an interview I would offer both solutions, and make clear that I was willing to code in either style if there was a local style guide to follow. Left to my own devices though, I'd definitely favor the ternary operator.

Mike Simmons
Ranch Hand
Posts: 3090
14
[Harvinder]: I agree with Rob's solution. That's cool Rob. Please tell me. How did you get the solution? How should we approach such questions? After looking at your solution it seems to be correct. But really i had no clue when i first looked at the question.

Obviously I'm not Rob, but one way to approach this would be to notice that since there are only three boolean inputs, there are only eight possible combinations of values. Make a table and see what happens for each value combination:

Once you complete this, it may well be possible to see a simple pattern in the results which is easy to describe with a formula like what Rob gave.

Harvinder Thakur
Ranch Hand
Posts: 231
Thanks Mike for the prompt answer. Earlier i had thought that it was some trick question. But after some time i did end up following the approach suggested by you. I wonder if Rob did it that way or not?

Rob Spoor
Sheriff
Posts: 20667
65
I did but with a little different layout I learned during an electro course in University. It uses some form of matrix, but it's kinda hard to explain without the book that I learned it from.

The only advantage that layout adds is the ease of grouping "cells" together. For instance, if every case where frumious is false yields true, it's very easy to see.

It gets hard at 5 or more booleans though.

Campbell Ritchie
Sheriff
Posts: 50240
79
I was just having a quick look at the code; I obviously should have done it formally.

Mike Simmons
Ranch Hand
Posts: 3090
14
Rob: I assume you're thinking of a Karnaugh map (or alternately a Veitch diagram, or KV map). Those were fun.
[ December 26, 2008: Message edited by: Mike Simmons ]

Rob Spoor
Sheriff
Posts: 20667
65
Those were the ones yes. Funny how the name gets completely forgotten but the technique stays, even after 10 years.

Jeez, I started university 10 years ago already... I'm getting old...

Mike Simmons
Ranch Hand
Posts: 3090
14

ramya narayanan
Ranch Hand
Posts: 338
Rob need a slight clarification.
In this code there is a condition :

In this case , taking manxome value as false it should return JUBJUB_BIRD
But with your suggestion it will return : BANDERSNATCH
Have you noticed it.
Regards.

Rob Spoor
Sheriff
Posts: 20667
65
But have you checked the values for frumious and uffish?

At that point you already know that frumious is true (the entire first if statement returns JUBJUB_BIRD). You also know that either uffish is true or manxone is true (second if statement).

Given that manxone is false (otherwise you won't get inside that if statement), uffish must be true. And therefore !frumious || uffish yields true and JUBJUB_BIRD is returned.

ramya narayanan
Ranch Hand
Posts: 338
Great Thinking Rob & now I've understood.
Thanks & keep going
My Best Regards!

Harvinder Thakur
Ranch Hand
Posts: 231
thanks Rob and Mark once again for your valuable inputs

Rob Spoor
Sheriff
Posts: 20667
65
Don't you mean Mike? I know Mark V has a lot of useful posts, but not in this thread

Harvinder Thakur
Ranch Hand
Posts: 231
OOps...it is Mike indeed...Thanks again for correcting me

Arjun Reddy
Ranch Hand
Posts: 629