Prefer early exit/break to large conditional blocks.

Some of these are large and contain smaller unrelated refactors.
This commit is contained in:
John Hood
2016-11-07 00:52:19 -05:00
parent b1a6f7c144
commit 1ae23b4dcc
4 changed files with 176 additions and 178 deletions
+4 -4
View File
@@ -314,7 +314,9 @@ bool STMClient::process_user_input( int fd )
NetworkType &net = *network;
if ( !net.shutdown_in_progress() ) {
if ( net.shutdown_in_progress() ) {
return true;
}
overlays.get_prediction_engine().set_local_frame_sent( net.get_sent_state_last() );
/* Don't predict for bulk data. */
@@ -336,9 +338,8 @@ bool STMClient::process_user_input( int fd )
overlays.get_notification_engine().set_notification_string( wstring( L"Exiting on user request..." ), true );
net.start_shutdown();
return true;
} else {
return false;
}
return false;
} else if ( the_byte == 0x1a ) { /* Suspend sequence is escape_key Ctrl-Z */
/* Restore terminal and terminal-driver state */
swrite( STDOUT_FILENO, display.close().c_str() );
@@ -390,7 +391,6 @@ bool STMClient::process_user_input( int fd )
net.get_current_state().push_back( Parser::UserByte( the_byte ) );
}
}
return true;
}
+5 -7
View File
@@ -90,7 +90,10 @@ Validity ConditionalOverlayCell::get_validity( const Framebuffer &fb, int row,
const Cell &current = *( fb.get_cell( row, col ) );
/* see if it hasn't been updated yet */
if ( late_ack >= expiration_frame ) {
if ( late_ack < expiration_frame ) {
return Pending;
}
if ( unknown ) {
return CorrectNoCredit;
}
@@ -107,16 +110,11 @@ Validity ConditionalOverlayCell::get_validity( const Framebuffer &fb, int row,
}
if ( it == original_contents.end() ) {
return Correct;
} else {
}
return CorrectNoCredit;
}
} else {
return IncorrectOrExpired;
}
}
return Pending;
}
Validity ConditionalCursorMove::get_validity( const Framebuffer &fb,
uint64_t early_ack __attribute((unused)),
+7 -8
View File
@@ -523,7 +523,9 @@ string Connection::recv_one( int sock_to_recv, bool nonblocking )
dos_assert( p.direction == (server ? TO_SERVER : TO_CLIENT) ); /* prevent malicious playback to sender */
if ( p.seq >= expected_receiver_seq ) { /* don't use out-of-order packets for timestamp or targeting */
if ( p.seq < expected_receiver_seq ) { /* don't use (but do return) out-of-order packets for timestamp or targeting */
return p.payload;
}
expected_receiver_seq = p.seq + 1; /* this is security-sensitive because a replay attack could otherwise
screw up the timestamp and targeting */
@@ -564,9 +566,9 @@ string Connection::recv_one( int sock_to_recv, bool nonblocking )
has_remote_addr = true;
last_heard = timestamp();
if ( server ) { /* only client can roam */
if ( remote_addr_len != header.msg_namelen ||
memcmp( &remote_addr, &packet_remote_addr, remote_addr_len ) != 0 ) {
if ( server && /* only client can roam */
( remote_addr_len != header.msg_namelen ||
memcmp( &remote_addr, &packet_remote_addr, remote_addr_len ) != 0 ) ) {
remote_addr = packet_remote_addr;
remote_addr_len = header.msg_namelen;
char host[ NI_MAXHOST ], serv[ NI_MAXSERV ];
@@ -579,10 +581,7 @@ string Connection::recv_one( int sock_to_recv, bool nonblocking )
fprintf( stderr, "Server now attached to client at %s:%s\n",
host, serv );
}
}
}
return p.payload; /* we do return out-of-order or duplicated packets to caller */
return p.payload;
}
std::string Connection::port( void ) const
+8 -7
View File
@@ -181,7 +181,9 @@ std::string Display::new_frame( bool initialized, const Framebuffer &last, const
for ( int row = 0; row < f.ds.get_height(); row++ ) {
const Row *new_row = f.get_row( 0 );
const Row *old_row = &*rows.at( row );
if ( new_row == old_row || *new_row == *old_row ) {
if ( ! ( new_row == old_row || *new_row == *old_row ) ) {
continue;
}
/* if row 0, we're looking at ourselves and probably didn't scroll */
if ( row == 0 ) {
break;
@@ -204,7 +206,6 @@ std::string Display::new_frame( bool initialized, const Framebuffer &last, const
break;
}
}
if ( scroll_height ) {
frame_y = scroll_height;
@@ -458,8 +459,10 @@ bool Display::put_row( bool initialized, FrameState &frame, const Framebuffer &f
}
}
if ( wrote_last_cell
&& (frame_y < f.ds.get_height() - 1) ) {
if ( ! ( wrote_last_cell
&& (frame_y < f.ds.get_height() - 1) ) ) {
return false;
}
/* To hint that a word-select should group the end of one line
with the beginning of the next, we let the real cursor
actually wrap around in cases where it wrapped around for us. */
@@ -468,13 +471,11 @@ bool Display::put_row( bool initialized, FrameState &frame, const Framebuffer &f
frame.cursor_x = 0;
frame.cursor_y++;
return true;
} else {
}
/* Resort to CR/LF and update our cursor. */
frame.append( "\r\n" );
frame.cursor_x = 0;
frame.cursor_y++;
}
}
return false;
}