Asymmetrical Code

Code that is asymmetrical as listed in CodeSmells.

I think the idea is that superfluously asymmetric code is that it can hide other CodeSmells. One such CodeSmell is duplication. If semantically-equivalent pieces of code are written in different styles, how much more difficult is it to spot duplication?

Examples below.


Here's one, transliterated into Java-ish code from SmalltalkBestPracticePatterns:

 out.print(a);
 b.printOn(out);
 out.print(c);
It would be more symmetrical if written like this:
 out.print(a);
 out.print(b);
 out.print(c);
(To do this, the "out" object's print() method would need to call "printOn()" on its input)

This highlights the duplication of the three calls. You can fix this by converting it to something like this:

 foreach o (a, b, c) {
  out.print(o);
 }


Another troubling example of smelly Asymmetric Code:

 a.foo() ;
 b.foo() ;
 ...

a.fooBar() ; b.fooBar() ; ...

a.Snafu() ; b.Snafu() ; ...
A more symmetric / less entropic solution would be:

 a.foo() ;
 a.fooBar() ;
 a.Snafu() ;

b.foo() ; b.fooBar() ; b.Snafu() ;

...
Which can be more easily rolled up into a loop.

[MartinSpamer]

But sometimes the order in which methods are executed matters

Another reason to roll the code up into:

 MetaFoo( a ) ;
 MetaFoo( b ) ;
 ...

MetaFoo( x ) { x.foo() ; x.fooBar() ; x.Snafu() ; }
Thus any apparently order dependence is confined

[MartinSpamer]


A worse example:

 a = A.open() ;
 b(a);

...

b(A a) { frozz(a) ; a.close() ; }


Only one layer of abstraction per method is facilitated with attractive symmetry.

ugly asymmetry -

 ...
 {
  badGuy.draw();
  drawPlayer();
 }

sexy symmetry -

 ...
 {
  drawBadGuy();
  drawPlayer();
 }

[BryanEdds]


See:InstinctAsIntelligence


CategoryCodeSmell


EditText of this page (last edited May 7, 2010) or FindPage with title or text search