java - Is it bad practice to have boolean setters to see whether they executed succesfully? -


is bad practice write setters booleans check if set correctly?

for example, following code set whether frame on top , return true if set decided. bad practice this, or should leave no return type?

    public boolean setalwaysontop(boolean alwaysontop) {         frame.setalwaysontop(alwaysontop);         return frame.isalwaysontop() == alwaysontop;     } 

bear in mind in example have getter when want check value when not attempting set it:

    public boolean isalwaysontop() {     return frame.isalwaysontop(); } 

thanks in advance. if more information please feel free ask , provide it.

edit:

it's wondering if useful because

if(setalwaysontop(true))   

instead of using void this:

  setalwaysontop(true);    

adding of additional conditions check api work not idea in general. frame.setalwaysontop must work. if don't trust api - cover unit tests. if tests fail should think work around solution - find working version of software, report bug or fix issue if have access code base.

such additional check can make sense if know existing problem , can not fixed now. in case raise custom exception setalwaysontop method (because know exceptional case), log error , perform sanity actions.

public void setalwaysontop(boolean alwaysontop) throws uimodificationexcepion {     frame.setalwaysontop(alwaysontop);     // due existing bug ... not updated cases     if (frame.isalwaysontop() != alwaysontop) {         throw new uimodificationexception("unable change 'always on top' property");     } } 

client code

try {     setalwaysontop(true); } catch (exception e) {     log.warn("could not update on top", e);     // stuff } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -