Best practices...?



  • <FONT face=Arial>Okay so I'm writing this program that validates data and then writes it to a database.  In some situations, a language code (like "en","ru", etc) can be specified and needs to be validated.  Does it make more sense to declare the language codes as a global array at the beginning of the program or to return the language codes as an array from a function?  In the first solution, the array (of about 800+ elements) is always loaded into memory even if it doesn't get used.  In the second solution the array is only loaded into memory when it's needed (which would be once or never per execution of the program).  Am I even making sense?</FONT>

    <FONT face=Arial>Thanks...</FONT>


  • ♿ (Parody)

    If the language codes are static and known at compile-time (which I would immagine), I would make a singleton helper class (kinda OO way of globals) or an enumeration of some sort that had these languages.

    Returning from a method seems to imply that they are dynamic and can change with the context.

    How many lang codes are there?



  • <FONT face=Arial>There's something like 800+ language codes.  Can you use singleton classes in asp.net?  The program itself is an asp.net (not my choice, believe me) web service...</FONT>



  • Yes, you can use singletons inside of ASP.Net. How exactly do you
    validate this language code? Is it parsed within the text itself?



    How to make a singleton:

    Inside the class you want to make a singleton, create a static reference to that class.

    Create a protected or private constructor for that class.

    Create a public static method that returns the singleton class.

    Here's where things vary a bit. Some programmers like to create methods
    that are used specifically for creating, using, and destroying the
    singleton instance. I prefer to create it when I first use it, but
    there is no silver bullet for it. Use your judgement here.



    Example:

    class Singleton

    {

        private static Singleton _instance;

        private Singleton() {}

        public static GetInstance() {

           if (_instance != null)

           {

              _instance = new Singleton();

           }

           return _instance;

        }

    }





    All of your other methods can be non static, but you have static access to everything.

    Example:

    Singleton.GetInstance().DoSomething();



    If your ASP.Net code uses the VB version of .net, you can do that as well, but I'm not as familiar with the syntax.




  • I don't know if I quite follow what you are trying to do, but my
    kneejerk reaction to your post is, "global variables are bad." I don't
    know what you mean by "vaidate" or what sort of data you have, or what
    language you're writing this in, but it seems like you should be able
    to avoid a global array and handle everything withing a function or
    object.



  • Sheesh. Question I'd ask is, what's easier for you? Are your hardware
    constraints such that an 800 element array significantly impacts
    performance? How often are you accessing it? Does your programme run
    once and then end?



    Personally, while I'm not familiar with ASP.net, I would tend to load
    it from a text file as needed, and then garbage collect it when done if
    I was worried about memory, but as I said, I don't know ASP, so I'm not
    sure how ASP would handle a disk read, or if it has a gc (surely it
    does.)






  • @zanidor said:

    I don't know if I quite follow what you are trying to do, but my
    kneejerk reaction to your post is, "global variables are bad." I don't
    know what you mean by "vaidate" or what sort of data you have, or what
    language you're writing this in, but it seems like you should be able
    to avoid a global array and handle everything withing a function or
    object.




    [i]Damn, can't edit....[/i]



    Also, globals aren't all bad. Unless you're using them to store
    config/state and/or multiple functions/objects are modifying them. Then
    they're very, very, Bad. (Or they're set solely from within a function
    at some random point that's four function calls deep...)



    Nothing wrong with broadly scoped variables if used correctly.


Log in to reply