Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's wrong with my classes?

Submitted these two classes (along with business logic and data access logic) for code review. The reviewer laughed looking at the code and advised me to re-do the classes. He told me to do some research and would not give me any clues. I couldn't find anything wrong. It looks little awkward that "submenu" is base class instead of menu. But my requirement is like that . . . menu is only of two levels (sub-sub-menu will never exist). What's so funny about them? Would be grateful if somebody shows me the path to improve these classes.

[Serializable]
public class Menu: SubMenu
{
    private List<SubMenu> _subMenu;
    public List<SubMenu> SubMenu
    {
        get
        {
            if (_subMenu == null)
                _subMenu = new List<SubMenu>();
            return _subMenu;
        }
        set
        {
            _subMenu = value;
        }
    }
}

[Serializable]
public class SubMenu
{
    public int ID { get; set; }
    public string MenuText { get; set; }
    public string MenuURL { get; set; }
    public string ImageURL { get; set; }
}
like image 657
Romeo Avatar asked Feb 15 '26 13:02

Romeo


1 Answers

The biggest issue here is the professionalism of whoever did your review - you should ask that they be more constructive and helpful and if they still insist in being so rude that I'd take it to a manager.


That said, my guess at what they saw in your design was the fact that you are using inheritance at all. Do you need inheritance here? It seems that you would be better served by having a Menu class which contains a list of SubMenus with no parent-child relationship or perhaps just a single MenuItem object which can contain a list of itself.

I'd prefer the second approach actually, something like this maybe:

[Serializable] 
public class MenuItem
{ 
    public MenuItem()
    {
        MenuItems = new List<MenuItem>();
    }

    public int ID { get; set; } 
    public string MenuText { get; set; } 
    public string MenuURL { get; set; } 
    public string ImageURL { get; set; } 

    // I'm not bothering with the code to protect access to the list
    // in this example but you might want that too...
    public List<MenuItem> MenuItems { get; set; }
} 
like image 183
David Hall Avatar answered Feb 18 '26 03:02

David Hall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!