A little concept puzzle (involving refactoring)

S

Stefan Ram

Here is a little puzzle about the definition of concepts:

/The behavior/ of a program I call what can be observed
from the outside by running the program.

/A refactor/ I call a modification of the source code of
a program that does not change the behavior of the program.

Now, let's test these concepts!

public class Main
{ public static void main( final java.lang.String[] args )
{ final double value = java.lang.Math.random();
java.lang.System.out.println( value ); }}

0.8262472065326088

Now apply the modification
»renameLocalVariable("value","number")«:

public class Main
{ public static void main( final java.lang.String[] args )
{ final double number = java.lang.Math.random();
java.lang.System.out.println( number ); }}

0.4774192830734339

The behavior has changed - so that was not a refactor!

Should we see it this way or modify our definitions?

PS: Here's a quotation from the book »Refactoring« by Martin
Fowler at al.:

»Refactoring is the process of changing a software
system in such a way that it does not alter the external
behavior of the code yet improves its internal structure.«
 
J

Joerg Meier

The behavior has changed - so that was not a refactor!

The behaviour has not changed: the behaviour is that it prints out a random
number. This behaviour is consistent over many calls and does not change
with the refactoring. By your definition, my Firefoxes behaviour is changed
all the time without even recompiling it! And following that logic, it is
impossible to ever refactor an NTP client!

Clearly, that definition makes no sense. The real issue here is that your
program is not written in a way that allows for proper unit testing, and as
a result, you can not confirm changes in behaviour because of your
inability to mock proper input.

The real solution is to use a supplied Random that can be initialized with
a fixed seed for mocking/testing, and then refactoring can be defined as
getting the exact same output (if you so wished).

Liebe Gruesse,
Joerg
 
E

Eric Sosman

Here is a little puzzle about the definition of concepts:

/The behavior/ of a program I call what can be observed
from the outside by running the program.

Here's where you need to be more specific, I think. For
example: A printed stack trace can be "observed from the outside,"
and the number of stack frames and the line numbers they show are
part of the "behavior."
 
A

Arne Vajhøj

Here is a little puzzle about the definition of concepts:

/The behavior/ of a program I call what can be observed
from the outside by running the program.

/A refactor/ I call a modification of the source code of
a program that does not change the behavior of the program.

Now, let's test these concepts!

public class Main
{ public static void main( final java.lang.String[] args )
{ final double value = java.lang.Math.random();
java.lang.System.out.println( value ); }}

0.8262472065326088

Now apply the modification
»renameLocalVariable("value","number")«:

public class Main
{ public static void main( final java.lang.String[] args )
{ final double number = java.lang.Math.random();
java.lang.System.out.println( number ); }}

0.4774192830734339

The behavior has changed - so that was not a refactor!

Should we see it this way or modify our definitions?

PS: Here's a quotation from the book »Refactoring« by Martin
Fowler at al.:

»Refactoring is the process of changing a software
system in such a way that it does not alter the external
behavior of the code yet improves its internal structure.«

You did not change the behavior of the program.

Behavior is really:
output = behavior(input)

You changed the input (the default seed of Random) and got
a different output.

The behavior is the same as you would get the same output
with the same input.

Arne
 
A

Arne Vajhøj

Here is a little puzzle about the definition of concepts:

/The behavior/ of a program I call what can be observed
from the outside by running the program.

/A refactor/ I call a modification of the source code of
a program that does not change the behavior of the program.

Now, let's test these concepts!

public class Main
{ public static void main( final java.lang.String[] args )
{ final double value = java.lang.Math.random();
java.lang.System.out.println( value ); }}

0.8262472065326088

Now apply the modification
»renameLocalVariable("value","number")«:

public class Main
{ public static void main( final java.lang.String[] args )
{ final double number = java.lang.Math.random();
java.lang.System.out.println( number ); }}

0.4774192830734339

The behavior has changed - so that was not a refactor!

Should we see it this way or modify our definitions?

PS: Here's a quotation from the book »Refactoring« by Martin
Fowler at al.:

»Refactoring is the process of changing a software
system in such a way that it does not alter the external
behavior of the code yet improves its internal structure.«

You did not change the behavior of the program.

Behavior is really:
output = behavior(input)

You changed the input (the default seed of Random) and got
a different output.

The behavior is the same as you would get the same output
with the same input.

This leads to the point that if you want to write unit testable
code, then you should use the constructors that takes the seed
as argument to enable you to unit test that your refactoring
did not change behavior.

Arne
 
A

Arved Sandstrom

Here is a little puzzle about the definition of concepts:

/The behavior/ of a program I call what can be observed
from the outside by running the program.

/A refactor/ I call a modification of the source code of
a program that does not change the behavior of the program.

Now, let's test these concepts!

public class Main
{ public static void main( final java.lang.String[] args )
{ final double value = java.lang.Math.random();
java.lang.System.out.println( value ); }}

0.8262472065326088

Now apply the modification
»renameLocalVariable("value","number")«:

public class Main
{ public static void main( final java.lang.String[] args )
{ final double number = java.lang.Math.random();
java.lang.System.out.println( number ); }}

0.4774192830734339

The behavior has changed - so that was not a refactor!

Should we see it this way or modify our definitions?

PS: Here's a quotation from the book »Refactoring« by Martin
Fowler at al.:

»Refactoring is the process of changing a software
system in such a way that it does not alter the external
behavior of the code yet improves its internal structure.«
I'm a little cog in a big machine, so my opinion carries little weight.
But that Fowlerian usurpation of the definition of a useful word to suit
his own opinions is not something I am all that happy about. It's an
overly constraining term, it's impractical, and it doesn't conform to
real life. What's a system? What's external behaviour? The entire list
of refactoring techniques is heavy on changing interfaces, at every
level of an application.

Don't get me wrong, I recognize the value of the espoused techniques.
But the definition is impractical. The extent to which it is ignored, in
the real world, is not an example of "an exception that proves the
rule", it's more like the rule is badly flawed.

I don't think Martin Fowler is an idiot, nor that we as software
developers shouldn't at least pay some attention to his musings. I don't
ignore what the GOF guys say either, or what Josh Bloch had to say, or
what Scott Meyers or Herb Sutter or Andrei Alexandrescu have to say, or
what Brian Goetz says, or Thomas Erl, etc etc etc.

But let's keep some perspective here. My apologies to Martin Fowler for
using him as an example. The man is a few years younger than I am, and
he started programming - according to his bio - well after me (or a
bunch of guys I went to school with, for that matter). Barely a decade
after he started FT work he wrote his first book. I'll give him credit,
he's articulate and he's obviously not stupid. Is he infallible like the
Pope? I daresay no more so than a dozen IT guys I can think of off the
top of my head who have more real world experience than he does...just
saying. But nobody's heard of these other guys because they ain't
writing books or going to conferences.

How many people here were experienced programmers when the original GoF
book came out? If you were like me, you may have read that book and
exclaimed to yourself, Boy, I've been doing half that shit all along but
it sure is nice to see my thinking validated. Now, it was fantastic that
those guys wrote that book, but it's seriously irksome to listen to all
the younger programmers who think that Gamma, Helm, Johnson and
Vlissides actually *invented* the patterns.

I have rather more basic terminology to describe what Fowler labelled as
"refactoring". Back in the day - believe it or not software
practitioners were this enlightened in the '60's and '70's even - we
called it maintenance. That term covers everything he is talking about.

AHS

When a true genius appears, you can know him by this sign:
that all the dunces are in a confederacy against him.
-- Jonathan Swift
 
A

Andreas Leitgeb

Stefan Ram said:
public class Main
{ public static void main( final java.lang.String[] args )
{ final double value = java.lang.Math.random();
java.lang.System.out.println( value ); }}
0.8262472065326088

There's already something wrong before the refactoring...
I cannot reproduce this result ;-)


PS: ok, I admit: I didn't even try...
 
D

Daniel Pitts

Here is a little puzzle about the definition of concepts:

/The behavior/ of a program I call what can be observed
from the outside by running the program.

/A refactor/ I call a modification of the source code of
a program that does not change the behavior of the program.

Now, let's test these concepts!

public class Main
{ public static void main( final java.lang.String[] args )
{ final double value = java.lang.Math.random();
java.lang.System.out.println( value ); }}

0.8262472065326088

Now apply the modification
»renameLocalVariable("value","number")«:

public class Main
{ public static void main( final java.lang.String[] args )
{ final double number = java.lang.Math.random();
java.lang.System.out.println( number ); }}

0.4774192830734339

The behavior has changed - so that was not a refactor!

Should we see it this way or modify our definitions?

PS: Here's a quotation from the book »Refactoring« by Martin
Fowler at al.:

»Refactoring is the process of changing a software
system in such a way that it does not alter the external
behavior of the code yet improves its internal structure.«
Behavior and outcome are not the same thing. You're preconditions are
different, and therefor your test is invalid.

Math.random() explicitly uses
http://docs.oracle.com/javase/7/docs/api/java/util/Random.html#Random(),
which implicitly takes "time" as a precondition.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top