Results 1 to 5 of 5

Thread: Reusing Code (in Module)

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2022
    Posts
    33

    Reusing Code (in Module)

    I have a couple of related best practices questions. I know that these are pretty open ended questions, but a few common suggestion or example would be appreciated.

    1. When should I put code in a module for reuse and when should I just rewrite it in the each sub?
    2. Is there any specific code that should not be called from a module? (For example: Is a good idea to put my db connection in a sub, then just call it whenever needed?)



    I am trying to learn to implement good practices, and how to code efficiently. In some cases I am breaking previous bad habits. Any pointers or suggestions would be great.

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    Re: Reusing Code (in Module)

    I see no one has given you an answer. Probably because there isn't one answer.

    Because maybe code that's reused should be put in module or maybe you should create a Class for it. This is also true for your DB connection and related functions. I usually handled my DB objects at the form level but many people are against that. But for large programs I usually went with a Class. I liked storing my connectionstring in the project settings. But I wouldn't call any of this "best practice". I'm a solo programmer and work for myself (or should say was, retired now).

    Now that I've wrote this, I realize it doesn't help you at all. lol

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Reusing Code (in Module)

    Unless you have a need to share a single DB connection across multiple methods, which is rarely the best way to do things, then the DB connection should be in a Using block within a single method.

    As to when to do the rest, if it takes too many arguments to get all the various pieces into the method, then it would be better off not being in a module. How many is too many is up to you.
    My usual boring signature: Nothing

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Reusing Code (in Module)

    Quote Originally Posted by Shaggy Hiker View Post
    Unless you have a need to share a single DB connection across multiple methods, which is rarely the best way to do things, then the DB connection should be in a Using block within a single method.
    This seems to be some of the weirdest advice I've seen in a while.

    It feels like something left over from ASP where it made sense to rely on behind the scenes connection pooling, but even there people often break the rules and defeat the pooling through misuse. Typically they don't realize that all connection strings must be identical to be pooled. Establishing "new" connections willy-nilly throughout a program raises the likelihood of error through subtle differences on the strings.

    Yes, .Net tried to pave over this by forcing connection pooling by default, but you still have to get it right in order for it to work properly.

    It is hard to imagine when this advice does make sense in a desktop application. Maybe you can elaborate on that?

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Reusing Code (in Module)

    Making the connection string identical across an application is trivial. It's just a string, build it one time and only one time, and the issue you mentioned goes away. Yes, it makes use of connection pooling, which could concern people, and concerned me at first, but it's fast and efficient. Open the connection, use it, close it. The Using construct takes care of all the cleanup, even if an exception happens. If all you have to do is ensure that a string is a constant...that's not difficult. The more I think about it, the more different ways I can come up with to ensure that.

    The only time this would be an issue is if you were writing out a connection string freehand every time you created a connection. If you're doing that, yeah it would be bad, but it wouldn't JUST be bad because pooling might not work as well as you'd like.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width