Page 1 of 1

About Question enthuware.ocpjp.v8.2.1565 :

Posted: Sat May 07, 2016 12:37 am
by schchen2000

Code: Select all

public void openSocket(int port){

        assert port > 1000;

        ... 

}
It says
Input params of a public method should not be validated using assertions.
It's not wrong to do assert on the parameter of a public method. I ran it and found no problem.

You are simply suggesting that It's just not a good practice to do so.

Is that correct?

If so, why is it a bad idea?

Thanks.

Schmichael

Re: About Question enthuware.ocpjp.v8.2.1565 :

Posted: Sat May 07, 2016 1:35 am
by admin
That is correct. It is legal but not recommended. Here is a nice explanation:
http://stackoverflow.com/questions/1383 ... ic-methods

Re: About Question enthuware.ocpjp.v8.2.1565 :

Posted: Sat May 07, 2016 2:46 am
by schchen2000
Thanks for your time.

Re: About Question enthuware.ocpjp.v8.2.1565 :

Posted: Sat May 07, 2016 3:27 am
by schchen2000
Same problem but different question
*******************************************
3. Should not be used for side effects. For example:

public boolean doSomething(){
...    

}

public void someMethod(){

assert doSomething();

}

This is not a proper use because here the assertion is used for its side effect of calling of the doSomething() method. The only case where this could be justified is when you are trying to find out whether or not assertions are enabled in your code:

boolean enabled = false;

assert enabled = true;

if(enabled) System.out.println("Assertions are enabled")

else System.out.println("Assertions are disabled")
Could you please tell me what you were trying to explain? I don't think I follow your explanation. Many thanks.

Schmichael

Re: About Question enthuware.ocpjp.v8.2.1565 :

Posted: Sat May 07, 2016 7:52 am
by admin
Can you please tell me which part is not clear?
Paul.

Re: About Question enthuware.ocpjp.v8.2.1565 :

Posted: Sat May 07, 2016 6:22 pm
by schchen2000
This is not a proper use because here the assertion is used for its side effect of calling of the doSomething() method. The only case where this could be justified is when you are trying to find out whether or not assertions are enabled in your code:
The part it says "....the assertion is used for its side effect of calling of the doSomething() method."

What does that part mean? In fact, I really did not follow the explanation.

"....to find out whether or not assertions are enabled in your code"????

How do we enable assertions in our code? To the best of my knowledge, assertions are disabled or enabled from the command prompt when the program is run, i.e. using -ea or lack thereof in order to enable or disable the assertion respectively from the command line on a terminal.

Thanks.

Schmichael

Re: About Question enthuware.ocpjp.v8.2.1565 :

Posted: Sat May 07, 2016 7:49 pm
by admin
schchen2000 wrote:
This is not a proper use because here the assertion is used for its side effect of calling of the doSomething() method. The only case where this could be justified is when you are trying to find out whether or not assertions are enabled in your code:
The part it says "....the assertion is used for its side effect of calling of the doSomething() method."

What does that part mean? In fact, I really did not follow the explanation.
Side effect simply means that you are killing two birds with one stone. You want to achieve one thing by an assert statement but while doing so, you are also achieving something else. This will work only if assertions are enabled. Explained here: https://docs.oracle.com/javase/7/docs/t ... ssert.html
"....to find out whether or not assertions are enabled in your code"????

How do we enable assertions in our code? To the best of my knowledge, assertions are disabled or enabled from the command prompt when the program is run, i.e. using -ea or lack thereof in order to enable or disable the assertion respectively from the command line on a terminal.

Thanks.

Schmichael
It is not talking about enabling or disabling assertions in the code. It is talking about checking whether they are enabled or disabled in the code. They are indeed enabled/disabled from the command line but you can check i.e. find out within your code.

HTH,
Paul.

Re: About Question enthuware.ocpjp.v8.2.1565 :

Posted: Sun May 08, 2016 1:10 am
by schchen2000
Thank you, Paul.

Schmichael

Re: About Question enthuware.ocpjp.v8.2.1565 :

Posted: Sat Jul 28, 2018 7:55 pm
by __JJ__
I understand the side-effects thing but just because you called the method doSomething does not mean it has side-effects.
Any (actually every) method is just refactored code that could exist in-place.So in that sense there is nothing magical or special about what's in a method.
doSomething could be "check the time; this code should not be running after 8am"
That is not a side-effect.
I appreciate that you are toughening us up for the exam, and you know a hell of a lot more than we do about what's actually on it, but , ah.. just saying.

Re: About Question enthuware.ocpjp.v8.2.1565 :

Posted: Sat Jul 28, 2018 11:24 pm
by admin
Invoking a method itself is the side effect of this assertion. That's a problem.
Whether that method checks time or inserts a row in the db is a different matter altogether.