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 3402900
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:11:40+00:00 2026-05-18T05:11:40+00:00

Problem Shared state is in certain cases a bad thing. I am in a

  • 0

Problem

Shared state is in certain cases a bad thing. I am in a project right now where pretty much all methods perform some sort of state change. I believe that’s a problem. On one hand, I can’t reuse the methods because depending on the current system state the new system state after the method call will be different. On the other hand, testing is pretty hard or near impossible in this project.

Question

So I was wondering what techniques are there to prevent shared state or to refactor such an application so there are less inter-dependencies.

One way would be to program in a more functional programming way. What other best practices are there and in which situations are they most appropriate?

Code examples would be very much appreciated if they help explaining the technique.

Example

Bad

ModuleA

Public Sub DoSomething
    Range("A2").Value = Worksheets("Sheet2").Range(cellReference).Value + 10
End Sub

ModuleB

Global cellReference = "B22"

Better (because no reference to global variable and no hard-coded sheet and cell references)

ModuleA

Public Sub DoSomething(resultCell, sourceSheet, sourceCell)
       Range(resultCell).Value = Worksheets(sourceSheet).Range(sourceCell).Value + 10
End Sub

ModuleB

deleted

Explanation

So, I guess the techniques applied here were:

  • Remove hard-coded values and pass them in as parameters
  • Remove references to global variables and pass them in as parameters

I know, these are basic best practices one should follow anyway. But perhaps there are some specifically helping to prevent shared state. I think Erik Meijer said that if you have shared state there is generally something wrong (I’m not sure whether I quoted him quite correctly).

Environment

By the way, this is an Excel VBA project with tons of legacy code developed by people without a software engineering background. The answers don’t have to be geared towards this particular setup, though.

  • 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-18T05:11:41+00:00Added an answer on May 18, 2026 at 5:11 am

    I would refactor massively to remove as many global variables as I can, that would be the first step.
    As a second step, some refactoring of hard-code and making properties files, could be usefull.

    Later you could analyze methods, to find code duplicate (it is quite usual to have lots of duplicates on this kind of software…) And maybe, with the knowledge adquired of the app, you might be able to go even further and use some state machine..
    (you said you’d accept it it a gave it as answer)

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

Sidebar

Related Questions

Problem is that onkeydown=doSearch gives error that can not find dosearch function which is
I am having a small problem geting to some data within my MVC C#
The setup: 2 web servers and a seperate state server I have two production
I.e. transferring the state from one object to another object, which shares some (but
Ok so im having a bit of an issue here. basically Im reading shared
I work on mvc.net application. Client is going to move production on a cluster
I have a UDP based application that is implemented using a thread pool. Messages
Per a question I posted yesterday , our website's DNS structure has changed to
Recalling this post enumerating several problems of using singletons and having seen several examples

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.