Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 715647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:12:38+00:00 2026-05-14T05:12:38+00:00

Suppose you had code like this: _READERS = None _WRITERS = None def Init(num_readers,

  • 0

Suppose you had code like this:

_READERS = None
_WRITERS = None

def Init(num_readers, reader_params, num_writers, writer_params, ...args...):
  ...logic...
  _READERS = new ReaderPool(num_readers, reader_params)
  _WRITERS = new WriterPool(num_writers, writer_params)
  ...more logic...

class Doer:
  def __init__(...args...):
    ...
  def Read(self, ...args...):
    c = _READERS.get()
    try:
      ...work with conn
    finally:
      _READERS.put(c)
  def Writer(...):
    ...similar to Read()...

To me, this is a bad pattern to follow, some cons:

  1. Doers can be created without its preconditions being satisfied
  2. The code isn’t easily testable because ConnPool can’t be directly mocked out.
  3. Init has to be called right the first time. If its changed so it can be called multiple times, extra logic has to be added to check if variables are already defined, and lots of NULL values have to be passed around to skip re-initializing.
  4. In the event of threads, the above becomes more complicated by adding locking
  5. Globals aren’t being used to communicate state (which isn’t strictly bad, but a code smell)

On the other hand, some pros:

  1. its very convenient to call Init(5, "user/pass", 2, "user/pass")
  2. It simple and “clean”

Personally, I think the cons outweigh the pros, that is, testability and assured preconditions outweigh simplicity and convenience.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:12:38+00:00Added an answer on May 14, 2026 at 5:12 am

    It seems to me that the single problem with this example is the use of global state. So don’t do that.

    Seriously- the concerns you’re worried about are solved by passing the appropriate context to the Doer when it comes up. In some cases, “appropriate context” may be a couple of plain arguments (e.g. a list of readers and a list of writers) or it might be a more complicated aggregate object (the “connection manager” which may have connections added and removed external to anyone who’s referencing it).

    To explicitly address your cons:

    1. if Doer has preconditions, validate them. If they aren’t met, raise an exception.
    2. (Solved if ConnPool is passed as a parameter to ctor or worker function.)
    3. Make Init create the thing you’ll pass to Doer, rather than creating global data. For mockery, pass the class to be built as well? Basically, use a factory of some sort.
    4. You only need to worry about thread safety in your state if it’s shared. If each thread has its own connection manager (for instance), then there’s nothing to lock at this level.
    5. (Solved if you don’t use globals, natch.)

    So- it’s not particularly less convenient to do:

    class ConnPool:
       def __init__(self, numReaders, readerParams, numWriters, writerParams):
          (your InitFunction with a bunch of self. prepending)
    
    class Doer:
       def __init__(self, connPool, ...):
          if not preconditions:
             raise DoerPreconditionsNotMetError()
          self.connections = connPool
    
       def Read(self):
          readers, writers = self.connections._READERS, self.connections._WRITERS
          ...
    

    So, I don’t know. I don’t see that as particularly less concise or less readable than your example. (Alternatively, you could pass the connection manager to the Read function, that’ll be up to your requirements obviously.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose you had this: def wipeProduct(hash, nameToDelete) hash.each do |i| key = i[0] productName
I've had trouble writing this code. I'm supposed to make a function that can
Suppose I had a WCF service that I have coded up, like Clemens Vasters's
Suppose I had the following function: function alertMesg() { alert(This ok function alerts message!);
Suppose I create a file for writing like this: std::ofstream my_file(filename, std::ios_base::out | std::ios_base::trunc);
Suppose I have a function like this: public void AddEntry(Entry entry) { if (entry.Size
I have a code like this: try { using (TransactionScope scope = new TransactionScope())
I had a code (lets suppose a simple chat application), I used Tcp with
Suppose I have some simple struct like this: public struct WeightedInt { public int
I've seen second one in another's code and I suppose this length comparison have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.