Skip to content

Instantly share code, notes, and snippets.

@GeePawHill
Last active March 21, 2019 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GeePawHill/0be0ae51d8f7464903dc242d6294b471 to your computer and use it in GitHub Desktop.
Save GeePawHill/0be0ae51d8f7464903dc242d6294b471 to your computer and use it in GitHub Desktop.
longMethod( int y ) {
int x;
x = 0;
// ...
// IRRELEVANCY #0: 87 lines that neither read nor write x
// ...
if( y == 17 ) {
x = 1;
}
// ...
// IRRELEVANCY #1: 87 more lines that neither read nor write x
// ...
if( someVeryInterestingConditionHavingNothingToDoWithX() ) {
doTheThing(x)
}
// ...
// IRRELEVANCY #2: 87 more lines that neither read nor write x
// ...
}
////////////////
// After joining declartion to initialization and moving it as far *down*...
// we can go 87 lines into this method *without* having to remember what X is.
longMethod( int y ) {
// ...
// IRRELEVANCY #0: 87 lines that neither read nor write x
// ...
int x=0
if( y == 17 ) {
x = 1;
}
// ...
// IRRELEVANCY #1: 87 more lines that neither read nor write x
// ...
if( someVeryInterestingConditionHavingNothingToDoWithX() ) {
doTheThing(x)
}
// ...
// IRRELEVANCY #2: 87 more lines that neither read nor write x
// ...
}
////////////////
// After extraction we killed an if and made the single-assign aspect of x obvious
// in languages with immutability, we just made x immutable (const)!
longMethod( int y ) {
// ...
// IRRELEVANCY #0: 87 lines that neither read nor write x
// ...
int x=chooseX(y)
// ...
// IRRELEVANCY #1: 87 more lines that neither read nor write x
// ...
if( someVeryInterestingConditionHavingNothingToDoWithX() ) {
doTheThing(x)
}
// ...
// IRRELEVANCY #2: 87 more lines that neither read nor write x
// ...
}
////////////////
// We keep moving it down, but now it's easy to see we can move it *in*
// Now we've made IRRELEVANCY #1 visibly irrelevant, and IRRELEVANCY #2 visibly irrelevant
longMethod( int y ) {
// ...
// IRRELEVANCY #0: 87 lines that neither read nor write x
// ...
// ...
// IRRELEVANCY #1: 87 more lines that neither read nor write x
// ...
if( someVeryInterestingConditionHavingNothingToDoWithX() ) {
int x = chooseX(y)
doTheThing(x)
}
// ...
// IRRELEVANCY #2: 87 more lines that neither read nor write x
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment