Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Just because something works, does it mean you don't have to refactor?

Tags:

refactoring

I just saw this line of code in the WP codebase.

if ( $user_id = wp_validate_auth_cookie( '', apply_filters( 'auth_redirect_scheme', '' ) ) ) {
}

Um. Yeah. two method calls and an assignment statement in an if statement.

So, my guess is, nobody is refactoring this.

Are there any reasons why?

like image 580
user513788 Avatar asked Dec 28 '22 04:12

user513788


1 Answers

To answer the question in your title:

Just because something works, does it mean you don't have to refactor?

If it did, we'd never refactor. Because refactoring happens on green. That is, it happens when all tests are passing, when - by definition - the code works. If you "refactor" on red, you're not refactoring. You're playing with your code. You're taking risks that you probably shouldn't be taking. Refactoring is improving the design of existing code, without changing its behavior. If your code isn't working, if you don't have tests that demonstrate that it's working, then the changes you make could be changing its behavior.

like image 97
Carl Manaster Avatar answered Jun 09 '23 07:06

Carl Manaster