Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


#ActualSeraphLance

Posted 24 September 2013 - 04:56 PM

Now I realize that having global state (particularly of the mutable variety) is a bad idea, but I've had difficulties in the past getting around it for some applications.

 

Some time ago I had this problem with logging, but I've gotten around to the idea of just using a free function, because logging doesn't really require any kind of persistent state.  Now I have a new problem, and it's logging's ornery younger brother:  debug printing.

 

Here's what I'm using now:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace Foo
{
    public static class DebugPrinter
    {
        private static StringBuilder debugString = new StringBuilder();
        private static List<string> idList = new List<string>();

        [Conditional("DEBUG")]
        public static void RegisterString(String str, String id)
        {
            if (!idList.Contains(id))
            {
                debugString.Append(str).AppendLine();
                idList.Add(id);
            }
        }

        [Conditional("DEBUG")]
        public static void Flush()
        {
            debugString.Clear();
            idList.Clear();
        }
        [Conditional("DEBUG")]
        public static string getString()
        {
            return debugString.ToString();
        }
    }
}

Usage is pretty clear:  whatever class wants to write to it calls registerString(passing an id, because I'm using the sharpDX toolkit and update and draw aren't synchronized with each other) and the reader calls getString, followed by flush, then renders the string to the screen.

 

 

Now, if you feel the urge to stab me in the eye for this, I fully accept that.  It's probably the nastiest thing in my whole code base.  I just can't think of a way around it.  I can't use a free function because I need to mantain state between draw calls.  I could make some heavyweight scaffolding around deep dependency-passing down my state hierarchy.

 

I'm aware that it's kind of one of those leading sorts of questions(how do I keep all this shared global state without any shared global state), but it's also a design problem I don't know any other solution for.


#1SeraphLance

Posted 24 September 2013 - 04:53 PM

Now I realize that having global state (particularly of the mutable variety) is a bad idea, but I've had difficulties in the past getting around it for some applications.

 

Some time ago I had this problem with logging, but I've gotten around to the idea of just using a free function, because logging doesn't really require any kind of persistent state.  Now I have a new problem, and it's logging's ornery younger brother:  debug printing.

 

Here's what I'm using now:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace Foo
{
    public static class DebugPrinter
    {
        private static StringBuilder debugString = new StringBuilder();
        private static List<string> idList = new List<string>();

        [Conditional("DEBUG")]
        public static void RegisterString(String str, String id)
        {
            if (!idList.Contains(id))
            {
                debugString.Append(str).AppendLine();
                idList.Add(id);
            }
        }

        [Conditional("DEBUG")]
        public static void Flush()
        {
            debugString.Clear();
            idList.Clear();
        }
        [Conditional("DEBUG")]
        public static string getString()
        {
            return debugString.ToString();
        }
    }
}

Usage is pretty clear:  whatever class wants to write to it calls registerString(passing an id, because I'm using the sharpDX toolkit and update and draw aren't synchronized with each other) and the reader calls getString, followed by flush.

 

 

Now, if you feel the urge to stab me in the eye for this, I fully accept that.  It's probably the nastiest thing in my whole code base.  I just can't think of a way around it.  I can't use a free function because I need to mantain state between draw calls.  I could make some heavyweight scaffolding around deep dependency-passing down my state hierarchy.

 

I'm aware that it's kind of one of those leading sorts of questions(how do I keep all this shared global state without any shared global state), but it's also a design problem I don't know any other solution for.


PARTNERS