Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

General programming question - Constants class

Options
  • 15-10-2011 9:18pm
    #1
    Registered Users Posts: 7,863 ✭✭✭


    Not a big deal, but something i was thinking about.
    Recently, I ran into some problems with trying to access variables/functions across different classes. I figured I could make them static, or just public, but that would obviously break the encapsulation etc etc.

    Since some of these variables were static anyway, eg URLs, I decided to round them up and put them into a "constants" class where I could access them like Constants.URL or Constants.generalMethod().

    My question is, is this good practice or should I figure out some way to include them in each class? With some stuff I'd rather only have them defined once so this seems the best way to me.

    Cheers.


Comments

  • Registered Users Posts: 2,023 ✭✭✭Colonel Panic


    I think it's good practice to group constants into classes or structures, I do it all over the place!

    Regarding how visible they are... Use your judgement. If they need to be effectively global, so be it, you only run into problems with global state when it's not preinitialized co start data IMO.


  • Registered Users Posts: 3,945 ✭✭✭Anima


    Definitely grouping them together in the same class is a good idea. Don't really know what you're doing but in general that would be the thing to do.


  • Registered Users Posts: 26,574 ✭✭✭✭Creamy Goodness


    in my opinion i don't really think it's a great idea, it just doesn't sit right in my brain.

    a class/object to me is a group of variables and functions that are logically grouped, and not just random variables lobbed in with accessor functions.

    now i'm not saying the class approach isn't a good idea, just what I'd do is have a .lib that would have the defines for the variables and just include that lib.


  • Registered Users Posts: 15,065 ✭✭✭✭Malice


    It's hard to know exactly what you're doing as situations can differ and my advice could be completely wrong but I would have a look at creating a base class containing stuff like URL and inheriting from it where required. That way URL isn't available to classes that don't need to know anything about it.


  • Registered Users Posts: 2,023 ✭✭✭Colonel Panic


    Hmmm, I think that's an abuse of inheritance, it's the road to over complicated inheritance trees. Composition is much better but is not suitable here.

    If it were C++, I'd just but all the constants grouped by use in a namespace and reference them from the implementation keeping he header clean. For C# I would declare a static internal class with all the constants, or a public static class if it's shared constant data.


  • Advertisement
  • Registered Users Posts: 11,979 ✭✭✭✭Giblet


    Component or Composition modelling can lead to abuse of messaging and interfaces to accomplish the same thing too either. What actually are your requirements? There's nothing wrong with writing static helper classes which do the same thing in any given context, either for retrieving external configuration data (which would be then cached) or for generating stuff like URLs. Having a big list of static consts for configuration data can be unwieldy, especially in an ever growing application.


  • Registered Users Posts: 7,863 ✭✭✭The_B_Man


    I actually posted this thread to get a discussion going but since a few have asked, I'll outline why I used the Constants class.
    I was using it for my lotto app (see sig) and ran into a few obstacles.
    I've a few classes that all connect to my server at certain times. Instead of creating x amount of strings to store the URL, I just made a "public static String URL".
    Also, I store the number of lotto balls that are in the irish lotto and euromillions, as well as details like the database table names. If I'm using a different class per game (ie lotto vs euromillions) then I have the choice of creating extra variables or storing them centrally. Since theres differences in the lotto and euromillions, I can't really re-use some of the database code so that has to be moved.
    There's other things as well, like the screen dimensions. Depending on the size of the phone itself, I want to change the padding. I'd imaging the number of pixels used to pad a tablet, would be a lot different than that which you'd use for a tiny little phone. Doing it this way i can call it like "padding=Constants.padding*5" and it'll look the same across all devices.
    I've also a simple little method that converts a string[] to a string. I couldnt find any other class it would fit in, so I just put it in the Constants class as a public static method.

    Now I'm not saying all this is best practices, and I'm not saying I'm an expert (far from it, I'm actually still a student), but if anyone is offering free advice, I'll be more than happy to take it. I'd also be really interested in how more "seasoned" coders would handle these obstacles, so at least I'll know in future.

    Cheers.


  • Registered Users Posts: 2,023 ✭✭✭Colonel Panic


    Hmmm, I see what you mean now. A kitchen sink style class is something i would try to avoid. Have a look at the single responsibility principle.

    For something I'm working on at the moment, a tool for uploading data via a REST based api, I have a class called EndPoints that is just a load of static strings that I use all over the place. They don't necessaily belong to any class, but it is useful to group them all together.

    Likewise I have another class that groups together some static utility functions related to boilerplate for filling in UI elements with data from this REST api. But putting it all in one class makes no sense.

    To be honest though, it often pays to just make these mistakes for yourself!


  • Closed Accounts Posts: 2,930 ✭✭✭COYW


    For something I'm working on at the moment, a tool for uploading data via a REST based api, I have a class called EndPoints that is just a load of static strings that I use all over the place. They don't necessaily belong to any class, but it is useful to group them all together.

    Likewise, I nearly always create an internal class named Constants, with static variables, in the namespace that I use throughout. I don't like the idea of declaring the same constant in X different classes in a namespace.


  • Registered Users Posts: 7,863 ✭✭✭The_B_Man


    Well that would also make sense for the obvious reason that you can change something once, and it takes affect throughout the whole program!


  • Advertisement
  • Registered Users Posts: 15,065 ✭✭✭✭Malice


    COYW wrote: »
    declaring the same constant in X different classes in a namespace.
    Definitely don't do that!


Advertisement