Process to pass from problem to code. How did you learn?

后端 未结 13 1750
离开以前
离开以前 2020-11-30 09:40

I\'m teaching/helping a student to program.

I remember the following process always helped me when I started; It looks pretty intuitive and I wonder if someone else

相关标签:
13条回答
  • 2020-11-30 09:48

    Yes.. well TDD did't existed ( or was not that popular ) when I began. Would be TDD the way to go to pass from problem description to code?... Is not that a little bit advanced? I mean, when a "future" developer hardly understand what a programming language is, wouldn't it be counterproductive?

    What about hamcrest the make the transition from algorithm to code.

    0 讨论(0)
  • 2020-11-30 09:49

    I go via the test-driven approach.

    1. I write down (on paper or plain text editor) a list of tests or specification that would satisfy the needs of the problem.

    - simple calculations (no discounts and concessions) with:
        - single item
        - two items
        - maximum number of items that doesn't have a discount
    - calculate for discounts based on number of items
        - buying 10 items gives you a 5% discount
        - buying 15 items gives you a 7% discount
        - etc.
    - calculate based on hourly rates
        - calculate morning rates
        - calculate afternoon rates
        - calculate evening rates
        - calculate midnight rates
    - calculate based on buyer's age
        - children
        - adults
        - seniors
    - calculate based on combinations
        - buying 10 items in the afternoon
    

    2. Look for the items that I think would be the easiest to implement and write a test for it. E.g single items looks easy

    The sample using Nunit and C#.

    [Test] public void SingleItems()
    {
        Assert.AreEqual(5, GetPrice(5, 1));
    }
    

    Implement that using:

    public decimal GetPrice(decimal amount, int quantity)
    {
        return amount * quantity; // easy!
    }
    

    Then move on to the two items.

    [Test]
    public void TwoItemsItems()
    {
        Assert.AreEqual(10, GetPrice(5, 2));
    }
    

    The implementation still passes the test so move on to the next test.

    3. Be always on the lookout for duplication and remove it. You are done when all the tests pass and you can no longer think of any test.

    This doesn't guarantee that you will create the most efficient algorithm, but as long as you know what to test for and it all passes, it will guarantee that you are getting the right answers.

    0 讨论(0)
  • 2020-11-30 09:49

    I think there are about a dozen different heuristics I know of when it comes to programming and so I tend to go through the list at times with what I'm trying to do. At the start, it is important to know what is the desired end result and then try to work backwards to find it.

    I remember an Algorithms class covering some of these ways like:

    • Reduce it to a known problem or trivial problem
    • Divide and conquer (MergeSort being a classic example here)
    • Use Data Structures that have the right functions (HeapSort being an example here)
    • Recursion (Knowing trivial solutions and being able to reduce to those)
    • Dynamic programming

    Organizing a solution as well as testing it for odd situations, e.g. if someone thinks L should be a number, are what I'd usually use to test out the idea in pseudo code before writing it up.

    Design patterns can be a handy set of tools to use for specific cases like where an Adapter is needed or organizing things into a state or strategy solution.

    0 讨论(0)
  • 2020-11-30 09:50

    Wishful thinking is probably the most important tool to solve complex problems. When in doubt, assume that a function exists to solve your problem (create a stub, at first). You'll come back to it later to expand it.

    0 讨论(0)
  • 2020-11-30 09:51

    A good book for beginners looking for a process: Test Driven Development: By Example

    0 讨论(0)
  • 2020-11-30 09:58

    when learning programming I don't think TDD is helpful. TDD is good later on when you have some concept of what programming is about, but for starters, having an environment where you write code and see the results in the quickest possible turn around time is the most important thing.

    I'd go from problem statement to code instantly. Hack it around. Help the student see different ways of composing software / structuring algorithms. Teach the student to change their minds and rework the code. Try and teach a little bit about code aesthetics.

    Once they can hack around code.... then introduce the idea of formal restructuring in terms of refactoring. Then introduce the idea of TDD as a way to make the process a bit more robust. But only once they are feeling comfortable in manipulating code to do what they want. Being able to specify tests is then somewhat easier at that stage. The reason is that TDD is about Design. When learning you don't really care so much about design but about what you can do, what toys do you have to play with, how do they work, how do you combine them together. Once you have a sense of that, then you want to think about design and thats when TDD really kicks in.

    From there I'd start introducing micro patterns leading into design patterns

    0 讨论(0)
提交回复
热议问题