1

I am recently working on a class that represents a table:

public class Table
{
    public StudentsOfTableList StudentsOfTable;
    public Point PositionUpperLeftCorner;
    public Size TableSize;
    public readonly Guid TableID;

    public Table()
    ...
    public static bool AreIdentical(Table table1,Table table2)
    ...
    public static bool Table1InFrontOfTable2(Table table1, Table table2)
            ...
}

As you can see here, this class is actually a data structure. I have some code later that compares the TableIDs to see if two tables are identical, and I really want to refactor the code so that there is a method in the Table class that determines if two tables are identical. However, as Clean Code the book says, a class with public fields and behaviors are hybrids, and those hybrids are bad code. Because of that, I didn't add a instance method to the class that checks if the tables are identical, rather I added a static method that compares two tables. Will this static method turn the class into a hybrid?

Blip
  • 1,158
  • 1
  • 14
  • 35
  • This is really urgent, I have to pause my work and wait for the answer!!Thank you very much if you plan to answer – Blip Nov 30 '13 at 16:14
  • 3
    You should use public properties most of the time. A constant is considered ok as a public field. – Silvermind Nov 30 '13 at 16:22
  • Thanks. Can properties replace getter and setter methods? – Blip Nov 30 '13 at 16:28
  • Yes and no, simple set and get methods can be replaced, long running and complex set and get methods should remain public methods. There is more to it, but it basically comes down to that. – Silvermind Nov 30 '13 at 16:54
  • Why should I use properties most of the time, since I don't need to control the the getting and setting of the fields in this case? – Blip Nov 30 '13 at 17:04
  • 1
    Perhaps this can shed some light on the matter: http://msdn.microsoft.com/en-us/library/ms182141.aspx – Silvermind Nov 30 '13 at 18:45
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/42249/discussion-between-victorprograss-and-silvermind) – Blip Nov 30 '13 at 19:09

1 Answers1

0

Take a look at IEqualityComparer. Based on what you have written it seems the best solution. There is not reason for Table to know how to compare it to other table. If you really want this semantics inside the Table class - check GetHashCode and Equals methods. If you choose the second solution - make sure to override both GetHashCode and Equals.

Community
  • 1
  • 1
Aleksei Poliakov
  • 1,322
  • 1
  • 14
  • 27