Re: how to make a variable based on array length and member value
- From: Ian Hobson <ian.hobson@xxxxxxxxxxxx>
- Date: Sun, 28 Jun 2009 09:22:21 -0500
Catsquotl wrote:
HiBuild up a suite of tests and the code in parallel, like this.
I have an array full_array[] of an arbitrary number of transaction
objects.
within the transaction is a Date member..
From the array I have created a new array years[].uniq! based on the
Date.year members
what id like to do is take the new arrays members and use them as
names for variables..
Say the array holds members for 2007, 2008, 2009
i`d like to iterate through the full_array and map all members from
2008 in an array.
my options are creating an array based on years.length where each
member holds all transactions from a specific year.
or iterate through the members of years and create an array based on
the value or index of years..
I can`t seem to think of the logic to accomplish this..
Any ideas?
1) Think of a very simple case that isn't yet covered by your code.
Start with a very simple cases, and proceed to edge cases and
pathological cases.
2) Write a new test to run your code and test the result, and run it. It
will fail, because you HAVE NOT YET WRITTEN THE CODE. (If it doesn't
fail, you have made a mistake somewhere).
3) Alter your code to pass the new test and all of the old ones. Only
when this is the case, may you proceed to stage 4.
4) Improve you code in some small way - Remove duplicate code, repair a
bad choice of variable names etc.
5) After each improvement, Rerun all the tests. If any fail, you
introduced an error. Fix before proceeding.
6) When the all pass and your code is nice and clean, save code (and
tests) to the version control system.
7) If the code is not yet complete, go to step 1.
The key is to go in really tiny baby steps at stages 1 and 4, and to
push stage 4 until you have really nice code you would be proud to show
anyone. Note - you are testing for anything that might break/be broken,
not aiming for 100% code coverage.
You will find that defining the tests will clarify your thinking about
what is required. Stage 4 means the code remains clean and easy to
modify. The test suite will stop you introducing errors into your code
later.
When you are done, you have nice code, that you can demonstrate is correct.
Regards
Ian
.
- Follow-Ups:
- References:
- how to make a variable based on array length and member value
- From: Catsquotl
- how to make a variable based on array length and member value
- Prev by Date: Legacy code and aliases to user input
- Next by Date: Re: Attr Methods and object setters
- Previous by thread: how to make a variable based on array length and member value
- Next by thread: Re: how to make a variable based on array length and member value
- Index(es):
Relevant Pages
|