CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2005
    Posts
    114

    Red face Classes that Generate Fluent and Nonsensical Poetry.

    http://gist.github.com/347070

    I've managed to write a simple set of classes that facilitate generating verse in iambic patameter. All the the output will flow perfectly, but unfortunately none of it will either make sense or be grammatically correct. If anyone would be kind enough, I'm asking you to take a look at my code and tell me what you think of it.

    I'm 18 and have been here and there learning and experimenting with programming since I was much younger. My dream has been for a long time to become a professional programmer. I know the code I've written is trivial, but hopefully I'm able (especially by the grace of God) to work for a company one day. So can anyone please assess my code and my ability according to the code I've written, and tell me what I need to do to achieve my dream. I'm already in college, and hopefully I will soon be taking computer programming courses.

    Nevertheless, programming for me is not just something I wait to do until I've been educated. I enjoy programming, so I do it now, even when it sometimes pulls my mind off of the prerequisite work I should be doing. That's not good though. I know I need to focus more on my work. But I still love to program. I'd like to hear your feedback on this, and on my code and abilities. I'll accept criticism, but I'm hoping you'll correct me kindly

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    Re: Classes that Generate Fluent and Nonsensical Poetry.

    Your "PartOfSpeech TranslatePart(string part)" method can be reduced to a much more simple method:


    Code:
    PartOfSpeech TranslatePart(string part)
    {
        if (Enum.IsDefined (typeof (PartOfSpeed), part))
            return (PartOfSpeed) Enum.Parse (typeof (PartOfSpeed), part, true);
        return PartOfSpeech.Unknown;
    }
    That will do a case insensitive parse of your string and give you the corresponding enum type. If the string is not part of the enum, you'll get Unknown.

    You do a few funny things like calling ElementAt instead of directly accessing the element at the index:

    Code:
    Word currentWord = this.ElementAt(i);
    // should be
    Word currentWord = this [i];
    The difference being that you're invoking Linq extension methods which operate on IEnumerable. This means that to get the element at position [i], the LINQ method might do something like:

    Code:
    public T ElementAt (int index) {
        int count = 0;
        foreach (var v in this) {
            if (count == index)
                return v;
             count ++;
        }
    }
    Similarly for your usage of the LINQ method "Count ()" instead of directly using the List<T> property 'Count'.

    Most of your 'MatchesXXX' methods can be rewritten to be like this:

    Code:
    public bool MatchesText(string[] texts)
    {
        return Array.Contains<string> (texts, Text);
    }
    Alternatively if 'texts' were a List<string> instead of a string[] you could just directly call: "texts.Contains (Text)" and remove that method entirely. Or just change your method signature to accept an IList<string>. Both string[] and List<string> implement IList<string>, so you can access your string[] through the IList<string> interface and gain easy access to methods like IndexOf and Contains.

    For example it's perfectly legal to declare your method like this and pass a string[] as the parameter:
    Code:
    public WordList ThoseWithStress(IList<string> stresses)
    {
        WordList newList = new WordList();
        for (int i = 0; i < this.Count; i++)
        {
            if (stresses.Contains (this [i]))
                newList.Add(new Word(this [i]));
        }
    
        return newList;
    }
    ...
    ...
    ThoseWithStresses (new string[] { "u" });
    You use turnary when you don't have to in a few places, for example:
    Code:
    return NumberOfSyllables > 0 ? true : false;
    can be simply:
    Code:
    return NumberOfSyllables > 0;
    The UpdateRandomizer function is completely unnecessary You can't make things *more* random by creating a new Random object. The only thing you'd be likely to do would be to make things *less* random Just remove that code and your app will instantly become better and shorter!

    Other than that, things look more pretty decent. I'd have no major complaint about it, most of the issues I have are just minor ones.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

Tags for this Thread

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

Featured