Puzzle: What's wrong with this code

This code will run but could potentially fuck you over:

if (doSomethingImportandAndGetResult(val1) && doSomethingImportantAndGetResult(val2)) {
// ... do some shit
}

10 points to who ever spots the potential problem.

It's too verbose. You should try python desu

Go do your homework somewhere else, OP.

I made it verbose so you could solve the problem. It's example code.

Oh so you mean it's your homework problem then. You need to learn this on your own and not from the Internet senpai

Check what happens when either return void or raise an exception

What is short circuit boolean evaluation

This is first grade stuff

>evaluating in your conditional

Answer to your question:
Java will run the function two times using pass by reference (assuming that val1 and val2 are not basic data types). This is not necessarily bad, but val1 and val2 have changes. basic data type, (i.e. anything using pass by value) are much safer when trying to compare the result of two function calls.

(if this were C i wouldn't bat an eyelash at this code. C master race btw)

Basically, the code isn't wrong its just poorly written.

An aside:
Please don't come to Veeky Forums with your garbage programming homework. This shit is the easiest subject anyone can learn. Pretty much every STEM discipline learns it these days. If you can't figure this simple ass shit out then you don't deserve to be in the class you're in.

t. applied math grad student

Not OP but does it matter that he wrote the "do some shit" part as a comment? It's behind //

second Important function call will not get called if the first one returns false

either use two if statements, or call them outside the if statement and just compare two bools

/thread

This.

I always hate these puzzles because they show a piece of shit code that would be really stupid to use in the wild. I am like, "Why would you even get yourself into this weird predicament?!?"

It's like the world is trying to be overly complicated for the sake of being complicated.

For example, I assume doSomethingImportantAndGetResult uses a shit ton of static variables, is a static method, tries to modify the parameter directly (instead of returning a value), does not have any exception handling despite relying on some subroutine that throws RuntimeExceptions, and probably is only a few lines of beautiful code (but secretly uses 300 really inefficient lines under the hood).

I don't think you can return void and compile your program.

your shitty puzzle is solved,

10 points to this guy

if in the block "do some shit" he references val1 and val2, then yes. it is this combined with boolean logic short circuit that gives the code problems

The answer you're fishing for is short-circuiting.
The real answer is of course you're a fucking faggot using side effects and writing java.

Sometimes the intention is to short circuit.

The first process could depend on the 2nd process returning a success status (i.e. true).

Derp, I wrote that backwards:

The 2nd process could depend on the 1st process returning a success status (i.e. true).

This is not clear as written in the code. In a real programming language side effects would not be hidden in functions like that.

These are methods, not functions and are expected to have side effects, especially if they are called "doSomethingImportandAndGetResult" but return a boolean.

> In a real programming language side effects would not be hidden in functions like that.
;_;

I wish people were better coders

You're thinking of procedures

The difference between
...SomethingImportandAnd...
...SomethingImportantAnd...

ImportanD and ImportanT
Really? You're writing code by typing the whole function name rather than using autocomplete to catch naming errors?
Really? Something important and you aren't immediately testing the first result? No try, catch, finally?
Really? You're compiler didn't catch the error?

Unless you had something else besides the typo in mind, you're a horrible coder.
But I've seen people test for false in the "else" statement of a boolean test.

>But I've seen people test for false in the "else" statement of a boolean test.
it's unnecessary but also more readable

>side effects
Try a grown up language

Java is pass-by-value everywhere and your answer (shared mutable state) is an issue in literally every non-functional language, so I'm not sure why anything would change if it were written in C.

No, Java has only methods

>tries to modify the parameter directly (instead of returning a value)
This is the worst, especially if you're trying to test sections of code.