Home > OS >  How to name classes and structures related to the same entity | Suffixes
How to name classes and structures related to the same entity | Suffixes

Time:04-01


Share your experience, please, what do you do when you want to set the names of class and structure the same noun? Or am I doing something wrong in my logic?
Thank you!

enter image description here

CodePudding user response:

As far as I know it's impossible to give them the same name and it would be a bad practice if it were possible. Personaly, I don't understand really well the need of a struct but I don't have a lot of context to help you with.

If the struct is absolutely necessary I would simply declare it like this

struct CarStruct {
 ...
}

public class Car {
 ...
}

I don't see the need of a struct here if you declare a class for the same type of Object. You could simply declare the brand, YearIssue and MaxSpeed as attributes in your class but, like I said, if you could give more context on the matter, I could figure out if the logic itself makes sense !

Hope this helps !

CodePudding user response:

There is no need to move properties to another struct Car as it is tightly coupled to class Car and it has high cohesion

So, in my view, it would be better if you put code together in one class:

public  class Car
{
    public string Brand;
    public int YearIssue;
    public int MaxSpeed;

    public void Go() { }

    public void Stop() { }
}

If you are using C#, then you can read more about struct and class here.

  • Related