Gendarme Rule: Avoid Code Duplication In Silbing Classes

18 views
Skip to first unread message

Néstor Salceda

unread,
Aug 17, 2007, 12:16:54 PM8/17/07
to mono-soc-2007
Title: Avoid Code Duplicated In Sibling Classes.

Description: This rule is other scenario for the Code Duplicated smell.
The rule should check that doesn't exist code duplicated in sibling
sublclasses.

References:

http://sis36.berkeley.edu/projects/streek/agile/bad-smells-in-code.html#Duplicated+Code

Examples:

Bad:

public class Banner {
public virtual void Print ()
{
}
}

public class ColorBanner : Banner {
public override void Print ()
{
//Instructions for put the color.
Console.WriteLine ("Welcome to the Foo Code");
Console.WriteLine ("Assembly version: {0}", assembly.GetName
().Version);
}
}

public class SimpleBanner : Banner {
public override void Print ()
{
Console.WriteLine ("Welcome to the Foo Code");
Console.WriteLine ("Assembly version: {0}", assembly.GetName
().Version);
}
}

Good: (by example)

public class Banner {
public virtual void Print ()
{
}

protected void PrintHeader ()
{
Console.WriteLine ("Welcome to the Foo Code");
Console.WriteLine ("Assembly version: {0}", assembly.GetName
().Version);
}
}

public class ColorBanner : Banner {
public override void Print ()
{
//Code for put the color
PrintHeader ();
}
}

public class SimpleBanner : Banner {
public override void Print ()
{
PrintHeader ();
}
}

There are a lot of code for the Avoid Code Duplicated In Same Class
rule, I will extract some common code and adapt for this case. I
believe that write this rule quicly.

Any comments?

Néstor.

Sebastien

unread,
Aug 20, 2007, 9:10:11 AM8/20/07
to mono-soc-2007

On Aug 17, 12:16 pm, Néstor Salceda <nestor.salc...@gmail.com> wrote:
> Title: Avoid Code Duplicated In Sibling Classes.
>
> Description: This rule is other scenario for the Code Duplicated smell.
> The rule should check that doesn't exist code duplicated in sibling
> sublclasses.
>
> References:
>

> http://sis36.berkeley.edu/projects/streek/agile/bad-smells-in-code.ht...

In the "good example" doesn't the identical "override Print" calling
"PrintHeader" constitute duplicate code too ?
(while still better than the first case).

Removing all duplicate code would produce something like:

public class Banner {
public virtual void Print ()
{

Console.WriteLine ("Welcome to the Foo Code");
Console.WriteLine ("Assembly version: {0}",
assembly.GetName
().Version);
}
}

public class ColorBanner : Banner {
}

public class SimpleBanner : Banner {
}

Which is still "bad" (but for some other rules) since there's nothing
in ColorBanner or SimpleBanner that requires them to exist (in this
sample).

Néstor Salceda

unread,
Aug 20, 2007, 9:25:58 AM8/20/07
to mono-s...@googlegroups.com
El lun, 20-08-2007 a las 13:10 +0000, Sebastien escribió:
>
> In the "good example" doesn't the identical "override Print" calling
> "PrintHeader" constitute duplicate code too ?
> (while still better than the first case).

Umm, yes, this is true. Your solution is better. In the example there
aren't code enough for show it, I apologize for that.

You can see the code I have for the tests in the svn link:

http://mono-soc-2007.googlecode.com/svn/trunk/nestor/rules/Gendarme.Rules.Smells/Test/AvoidCodeDuplicatedInSiblingClassesTest.cs


> Removing all duplicate code would produce something like:
>
> public class Banner {
> public virtual void Print ()
> {
> Console.WriteLine ("Welcome to the Foo Code");
> Console.WriteLine ("Assembly version: {0}",
> assembly.GetName
> ().Version);
> }
> }
>
> public class ColorBanner : Banner {
> }
>
> public class SimpleBanner : Banner {
> }
>
> Which is still "bad" (but for some other rules) since there's nothing
> in ColorBanner or SimpleBanner that requires them to exist (in this
> sample).

Yes, I assumed that ColorBanner and SimpleBanner contains more code and
exists a reason for maintain these classes.

Thanks for the comment Sebastien.

Néstor.

Néstor Salceda

unread,
Aug 20, 2007, 10:25:19 AM8/20/07
to mono-s...@googlegroups.com
Hello,

Well, I have finished the last rule. As usual, you can see.

Tests:

http://mono-soc-2007.googlecode.com/svn/trunk/nestor/rules/Gendarme.Rules.Smells/Test/AvoidCodeDuplicatedInSiblingClassesTest.cs

Code:

http://mono-soc-2007.googlecode.com/svn/trunk/nestor/rules/Gendarme.Rules.Smells/AvoidCodeDuplicatedInSiblingClassesRule.cs

I have extracted a class from the existing rule
(AvoidCodeDuplicatedInSameClass), the code is in the following link.

http://mono-soc-2007.googlecode.com/svn/trunk/nestor/rules/Gendarme.Rules.Smells/CodeDuplicatedLocator.cs

And I have a last question:

I have also put the class Expression in the file Expression.cs and the
ExpressionFillerVisitor in the file ExpressionFillerVisitor.cs but these
classes (and CodeDuplicatedLocator) are internal classes. Is correct
with the coding guidelines put these classes in separated files?

Any comments?

Néstor.

Sebastien

unread,
Aug 20, 2007, 11:20:53 PM8/20/07
to mono-soc-2007
> And I have a last question:
>
> I have also put the class Expression in the file Expression.cs and the
> ExpressionFillerVisitor in the file ExpressionFillerVisitor.cs but these
> classes (and CodeDuplicatedLocator) are internal classes. Is correct
> with the coding guidelines put these classes in separated files?

Yes. In fact it's already the case for a few rules (which share code
between them inside the same assembly).
As long as the other filenames don't end with Rule.cs, I don't think
this will confuse anyone.

Good work :)
Sebastien

Néstor Salceda

unread,
Aug 22, 2007, 9:58:41 AM8/22/07
to mono-s...@googlegroups.com

Okey, I take note. Tomorrow I will post a new rule :)

> Good work :)

Thanks.

Néstor.


Reply all
Reply to author
Forward
0 new messages