Sunday morning quiz

 

Hey, goood morning ....


i'm trying to improve some code lines and i want to get reed of some code like this:

   // store current buffer
   int bufferRC = this.getBuffer();
   if ( bufferRC == -1 ) return false;


The method returns -1, 0 and eighter a 1 or a value greater then one. All values equals or greater than 0 is programm related where -1 one specifies a error.  I dont like this blocks because normaly i put in a sperate line with a a comment above it but in this case it explans himself and dont need a comment. But having this in a single row makes the "code style" bit unharmonic. So i am looking for a smart solution. You have any ideas for me ?


This would be cool:

   // define current buffer check
   int bufferRC;

   // get buffer
   if ( ( bufferRC = this.getBuffer() ) == -1 ) return false;


Thanks && Regards

 
Christian Stern: This would be cool:

That is valid code, just bad style. Equal vs equal equal is easily a typo.

int bufferRC = this.getBuffer(); if(bufferRC == -1) return false;
 
whroeder1:

That is valid code, just bad style. Equal vs equal equal is easily a typo.

Multiple expression in same line is equally bad style (no pun intended). 

int bufferRC = this.getBuffer(); 
if(bufferRC == -1) 
   return false;
 

Thank you for the comment. I found out, that this is working perfectly:

// define current buffer check
int bufferRC;

// get buffer
if ( ( bufferRC = this.getBuffer() ) == -1 ) return false;


i like it compact. A good short definition of a method is helpful if you have complex stuff to do. Every extra line make it harder to read months ago or during troubleshooting. So i found out, that using "( )" breakets is like forking and the code inside will be handled first. Same with return handling.


Boring style:

if ( a == 1 ) {
    return true;
}else{
    return false;
}


Nice style:

return ( ( a == 1 ) ? true : false );
Reason: