Writing subjects in present and past is hard

Started by
6 comments, last by Finalspace 5 years, 5 months ago

I wanted a class which produces a string which is like: Aquiring, Aquired, Initializing, Initialized, Building, Built, Copy, Copied etc.

Just to write "less" code... i thought... why did i do that, i have no idea... maybe because i can???


AddLog(SubjectBuilder.Build("C", false, "file A to B"));
AddLog(SubjectBuilder.Build("C", true, "file A to B successfully"));

 

Anyway this is the result:


    /// <summary>
    /// Simple class for genering a message using a subject as prefix
    /// </summary>
    public static class SubjectBuilder
    {
        private static readonly Dictionary<char, string> _subjectLetterMap = new Dictionary<char, string>() {
            { 'A', "Aquire" },
            { 'B', "Build" },
            { 'I', "Initialize" },
            { 'G', "Generate" },
            { 'D', "Destroy" },
            { 'M', "Make" },
            { 'C', "Copy" },
            { 'P', "Publish" }
        };

        class SubjectRule
        {
            public bool CurCut { get; set; } = true;
            public string Cur { get; set; } = "ing";
            public bool PastCut { get; set; } = false;
            public string Past { get; set; } = "d";
        }

        private static readonly Dictionary<char, SubjectRule> _rules = new Dictionary<char, SubjectRule>()
        {
            { 'e', new SubjectRule() },
            { 'd', new SubjectRule() { CurCut = false, PastCut = true, Past = "t" } },
            { 'y', new SubjectRule() { CurCut = false, PastCut = true, Past = "ied" } },
            { 'h', new SubjectRule() { CurCut = false, PastCut = false, Past = "ed" } }
        };

        /// <summary>
        /// Returns a simple string starting with a subject - like create, generate, copy.
        /// The subject is modified to satisfy the past or current time (creat-ing, creat-ed, etc.)
        /// </summary>
        /// <param name="firstLetterOrSubject">Subject or just the first letter of the subject</param>
        /// <param name="past">Is it now or in the past</param>
        /// <param name="text">The actual text</param>
        /// <returns>Builded message startin with the subject</returns>
        public static string Build(string firstLetterOrSubject, bool past, string text)
        {
            StringBuilder s = new StringBuilder();
            string subject = null;
            if (!string.IsNullOrEmpty(firstLetterOrSubject))
            {
                if (firstLetterOrSubject.Length == 1)
                {
                    // Single letter - lookup into the subject table
                    char upperCh = firstLetterOrSubject.ToUpper()[0];
                    if (_subjectLetterMap.ContainsKey(upperCh))
                        subject = _subjectLetterMap[upperCh];
                }
                else
                    subject = firstLetterOrSubject;
            }
            if (!string.IsNullOrEmpty(subject))
            {
                char lastChar = subject.Substring(subject.Length - 1)[0];
                SubjectRule rule = _rules.ContainsKey(lastChar) ? _rules[lastChar] : new SubjectRule();
                if (past)
                {
                    if (rule.PastCut)
                        s.Append(subject.Substring(0, subject.Length - 1));
                    else
                        s.Append(subject);
                    s.Append(rule.Past);
                }
                else
                {
                    if (rule.CurCut)
                        s.Append(subject.Substring(0, subject.Length - 1));
                    else
                        s.Append(subject);
                    s.Append(rule.Cur);
                }
                s.Append(" ");
            }
            s.Append(text);
            return s.ToString();
        }
    }

 

And yes this is used in a productive every day used application...

Advertisement

"Make" becomes "Maked", "Destroy" becomes "Destroied" -- or did I miss something?

I'm amazed you got to the step where you actually tried doing it. ?

I believe the true reason for you to do this was boredom combined with a bit of an itch xD
I totally feel you, man :D

Really i have no idea why i did that, but hey its still used in a production tool i made, used to deploy updates to our applications.

But this tool is used by developers only, so even when something would be wrong about the messages being written - nobody except me would notice it :D

 

Ah wait, i know why i did it: I was tired of writing "Copy A to B" vs "Copied A to B successfully" vs "Failed to copy A to B" all the time. So i ended up writing that "thing". This saved me a few words of typing. Also this had the advantage to use it for other operations as well, like "Initialize", "Create", "Make", "Destroy", etc.

What about:

  • Current operation: Copy A to B.
  • Operation failed: Copy A to B.
  • Operation succeeded: Copy A to B.
  • Confirm operation: Copy A to B

No problem with irregular verb conjugation at all :) Also works for localisation into languages where verb tenses aren't formed similarly to English, etc.

On 10/25/2018 at 9:31 AM, pcmaster said:

What about:

  • Current operation: Copy A to B.
  • Operation failed: Copy A to B.
  • Operation succeeded: Copy A to B.
  • Confirm operation: Copy A to B

No problem with irregular verb conjugation at all :) Also works for localisation into languages where verb tenses aren't formed similarly to English, etc.

Thats neat. Thanks for the tip.

This topic is closed to new replies.

Advertisement