2015/05/28

Why TDD is important to a newbie programmer

I've been reading a little bit about Test Driven Development, as well as its' drawbacks in the last little while, but I didn't really understand what the debate meant to me.  I often write code, and wait until it's too complex to trust new modifications, before bothering to write tests.  In Ruby, the convention is that we don't test private methods, so I work really hard to make as many methods private as possible, so that I don't have to break up my flow to write tests.

Recently, I was working on a refactor, on a script to generate DNS entries for company switches.  The way DNS currently works is, for every IP on the server, we make a forward and reverse entry for that IP in dns.  So, if you do a traceroute, the switch name will show up.  But I was asked to instead change DNS, so that each IP address would report the name of the port.  So, for a comparison, here's a simplified example of the old DNS entries for a switch

sw1 IN A 10.0.0.1  #switch1 management port
sw1 IN A 10.0.0.2 #switch1 Gi0/1
sw1 IN A 10.0.0.3 #switch2 Gi1/2

doing a traceroute would report the same 'switch' name, no matter which port the routing went through.

now, it reads something like this:

sw1 IN A 10.0.0.1
sw1-gi0-1 IN A 10.0.0.2
sw1-gi0-2 IN A 10.0.0.3

The corresponding reverse lookups are now more informative, because the network engineer can see, right in the traceroute, which port is being traversed.  It saves them the step of logging into a switch, to match up IP's with ports by hand, and can save them a lot of time troubleshooting.

I only recently adopted TDD, but today, I decided, without really thinking about it, to just crank out the changes I wanted to make.

So here's what I did:

  • I added the functionality
  • I edited my tests
  • I ran them, and they passed


It was awesome, and a rare experience, to have tests pass on the first try.  But the very fact that I didn't see a 'failure' before I made the code changes immediately worried me.  These changes were actually a pretty big deal; changing DNS entries across an enterprise is a big deal.  So I edited the test to fail, reran it, saw it fail, then edited it back to pass, reran it, and saw it pass.  And I was back to feeling confident.

So, what was making me anxious at that point?  Just the fact that I'd violated the dogma that TDD is the best way to develop?  Would I have actually gained anything, if I'd written the test before writing the code, or was I just being a paranoid?  I thought about it a bit, and came up with a few reasons for why I would have been happier with my code, if I'd:

  • Written the tests first
  • Ran them, just to see them fail
  • Edited my code
  • run the tests to see them pass
I think it's the first time I actually understood the point of that first, somewhat redundant-looking step.  Here's my reasoning:
  1. If I add a test, then run it, and it passes automatically, then I get the chance to reevaluate why.  If TDD does nothing more than save me from spending an hour troubleshooting a problem, only to find out that the reason I can't fix it is that I've been editing the wrong file, or running the wrong script, that's a huge gain.  Been there multiple times, done that multiple times, didn't like it.
  2. If I start by writing tests, I have to open my existing tests, and at least read enough of them to find a place to add my new tests.  This refreshes my memory on how the code works, which reduces the chances of accidentally duplicating functionality I already have.
  3. It slows me down, and gives me a chance think about what I'm doing.  This means I don't have to rewrite my method several times, just to fix my initial, knee-jerk solution.
So, given this, why don't we all religiously adopt TDD development?  Why do people like David Heinemeier Hannson deliver keynote speeches about why TDD isn't the end-all, be-all of testing?  Why does he liken TDD to fad diets?  I'm still pretty new to coding, but I want to take a whack at this.  First, I think that David wasn't actually decrying testing itself, at least not as much as it seems he was.  During his talk, he spent a lot of time on caveats.  His point, which I think was universally misunderstood, was that we shouldn't follow a TDD philosophy as dogma, and expect that it will save our butts in every case.  He specifically said that there IS a place for TDD, and for unit testing.  But, especially in Rails, testing from the front-end, using tools like Capybara, can get you much better results, by testing, empirically, that the application works from the end-user perspective.  Unit testing each method, in four different ways, is very different from running an end-to-end transaction, from the point of view of the browser.  For a webapp, TDD is sometimes impossible, or at least, unproductive.  How do you create a test that browses to your website, adds items to a cart, and checks out, before you write any code?  It's not useful, at least at this macro level.  That, I think, was David's point.

This takes me to my final observation; the biggest barrier for me, when building code in a TDD way, is the sheer effort it takes to write those tests.  I have to pick a testing framework, learn that framework, learn to write code that's easy to test, and constantly switch between editing tests and editing actual code.  For me, the tradeoff was a huge loss of productivity.

But I found a way to fix that.

This is the good part, by the way.  I essentially wrote this whole post, in order to write the following paragraph.  I write it in first person, describing what I did, because I'm a newbie, and don't feel I have the authority to tell people that they should do these things.  But you probably should.

So, I made a macro in my text editor that generates the standard testing template for my test suite, with only a few button presses.  Also, I learned enough about Rake to run tests from there with only one command, and created a couple of aliases at the command line, so that I could run tests with less typing.  I learned how to create tabbed displays in my text editor, so that I could view both my test file, and the code being tested, without switching back and forth.  I learned how to split my screen, so that I could have a command line, and my text editor instantly available, and always in the same spots.  So, now, I can change focus from tests, to code, to command line, with only a few key presses.  It's so easy, I don't even have to think about it.  And I created a bookmark in my browser, so that I could easily reference manuals related to my test suite, and to my current work.  And, over time, I've accidentally memorized about three syntactical bits of my test suite.  That's all it took to 'master' testing.  Three things.

Keep in mind, I didn't do any of this with the 'goal' of being able to do faster test-driven development.  It was a natural progression, guided by my desire to spend less time fighting with my text editor, or the Unix command line.  Mastering the tools we spend most of our time in carries self-evident, intrinsic rewards.

By accidentally eliminating these barriers, I've come to a place where the only tradeoff to doing TDD is the cost of actually thinking through tests, looking at what the class already does, considering what my new method should return, and coming up with a method name, before I actually create that method.  And, thinking about code from a testing-first perspective forces me to follow the simplest path, which leads to cleaner, more flexible code.  These tradeoffs, for me, carry their own benefits.


2010/04/19

solar panel - a brief update

I haven't posted on this in a while, but I'm going to at least post some preliminary numbers; my solar panel is built (mostly) and placed in the back yard (in a temporary location) and is generating between 80-100 watts of electricity per day, after it passes through the Grid Tie controller. I've calculated that it should generate 180 watts, and indeed, when I measure on the DC side of the Grid Tie Inverter, I am seeing 180 watts, so it appears around 30% of the power is being lost in the conversion to AC. This is a bit annoying to say the least, and drives the price up per watt by quite a bit.

Current expenditures
99.00 - Grid Tie Inverter
21.00 - Wooden frame
245.00 - solar cells
===============
365.00 - Total


peak power production: 120 watts
$3.04
average watts produced per day: 600
time to break even: 16 years

so, at this point, building a solar panel on my own is actually not worth doing, mostly because of the amount of electricity I'm losing in the Grid Tie Inverter. If 30% weren't being lost in conversion, we would be doing ok, but with that happening, the cost per watt is drastically affected. I'm going to check out what is going on there; the equation actually gets better if I add more solar panels, since there is a fixed cost for the Grid Tie Inverter, and I'm wondering if the efficiency will improve a bit with more panels connected.

2010/03/31

soldering work station notes

Hi, today I'm going to make some notes on my workstation area, so anyone trying to follow along at home won't make the same mistakes I did. First, we'll talk about strategy a bit, and prep work; this is important, in order to limit the time the soldering gun is actually on, causing a burn risk in your house.

Wire use strategy
I actually cut out 3"x6" card stock, and taped a bunch of paper tabbing wire to it, in a dry run, and made the following determinations for my 6 cell by 6 cell configuration, there will be 6 cells with positive wires sticking out half an inch, and 6 cells with negative wires sticking out half an inch. That left 24 cells with a 'normal' wiring, so before I laid even one cell on the panel, I would need the following for each panel

6 cells wired on just the negative side, with tabbed wires sticking out just long enough to connect to the bus (roughly 3 3/8 in).
6 cells wired on the positive side with tabbed wires sticking out 1/2 inch, and 'regular length' wires on the negative side. (roughly 2 7/16 in)
24 cells with 'regular length' wires on the positive side. (roughly 6 1/16 in)

I wanted to do both panels at once, so multiply the above number by 2, and pre-cut all my tabs
24x3 3/8" tabbed wires
24x2 7/16" tabbed wires
96x6 1/16" tabbed wires

Once that was done, I set up my work area with a flux pen, solder and the cheapest 30 watt soldering gun that is sold at Radio Shack. The tabbed wire is pre-coated with solder, and over time as my technique got better, I was able to complete the connection without using my roll of solder, but in the beginning, a little solder helped a lot.

MISTAKES I MADE
1. Always use the flux pen, otherwise the wires will pop off really easily.
2. I used a felt-covered table, and cracked a cell pressing down too hard, as the cell flexed inward. I switched to a flat table, covered with a thin cloth to prevent scratches, and that worked really well.

doing all the work of tabbing the cells took about 10 hours, spread across 3 days. All that's left is to attach the cells to each other within the solar panel, which I have yet to modify, since the 3-inch by 6-inch measurement was inaccurate. I plan on doing that tomorrow evening (meeting with the district manager of USSD this evening, on continued plans to become a sensei -- they typically charge $8000 for 'instructor school'), completing the solar panel Friday evening, and actually exposing it to the sun on Saturday..

2010/03/30

Here's a link to a nice diy solar website

Hi. http://www.hotsolarenergyfacts.com/green-energy/simple-steps-to-powering-your-residence-with-home-made-solar-power. This is a nice 'overview' site, that pretty much covers what I'm doing. I think that some pictures would be helpful, but besides that, it's a pretty nice description.

2010/03/29

pics of solar panel elements

I don't have the grid-tie inverter or plexiglass to cover the panels, but here's the rest of the components. Where I thought it was useful, I put a measuring device, such as a ruler or a t-square, so you could see the dimensions of the objects.


Here is the tabbing wire, which I solder to the back of each cell.

Here's the front/back of the same cell, you can see how I have soldered the tabbed wire to 6 points on the back. When they are attached, the wires will go from the back(positive side) of this cell to the front (negative side) of the cell below. They will end up in a grid of 6 by 6, for 36 cells, measuring roughly 19 inches by 38 inches.


Below is a stack of 35 cells, so you can see the thickness. the stack behind it only contains 24 cells, but they are all tabbed, increasing the thickness.















Here's the 'raw' panel, which will be changing a bit, but you can see that it's build to hold 3 solar arrays at 6x6 cells, of 37"x19", roughly 37"x61" total.




So that's all but the kill a watt power measuring device, which will be included in the next post, as well as the grid-tie inverter.

Solar Cells +other stuff arrived

Hey, so my solar cells showed up at the end of last week, and the first thing I did was confirm the measurement. Bad news, they are labelled as '3x6 inches', but they aren't. The size is actually 3 and 3/16 by 5 7/8. I actually pre-built the panel to be the correct size, based on an accurate cell size, so needless to say, some re-engineering is taking place. Besides that, everything is going swimmingly; The cells I bought came 'untabbed', meaning no tabbed wire has been attached to them, so I began soldering late on Sunday. After about 5 hours, I'd say I'm half done with the tabbing efforts, and expect to be able to place the cells sometime around the end of the week (after the redesign of the solar panel itself). Also, the cells themselves are super-thin; to give you an idea of how thin, a stack of 40 cells is about 1/2 inch in height. So the math says the cell is each cell is 1/80 of an inch in fractions, or .0125 inches in decimals. Soldering them together is definitely a difficult task, and I'm a little worried that some of them are flawed, as just touching the soldering gun to one caused it to crack from the heat.

I also have been able to mess around with the 'Kill a Watt' power reader ($9.99+shipping and handling on ebay seems to be a typical lower-end price), and came up with some interesting numbers. My laptop takes up about 40 watts of electricity, while the desktop is at around 150 watts. I haven't done the math, but perhaps from an electricity-usage standpoint, laptops could be cheaper than desktops. Some stats from around the house:

Microwave uses 1300 watts
Vacuum uses about 1000-1200, depending on whether the brush is running.
Wife's computer uses about 130 watts.
I can't measure the clothes dryer, as it's a 240 volt appliance, but the heating coil has the number '5400 watts' on the side, which is probably a good clue.

I also took some readings on the various items with standby lights in the house, such as the phone charging stand, the charger for AA and AAA batteries for the kids' various video game controllers. None of the devices with red lights on them took enough power to actually register on my meter, so less than 1 watt. I actually found one that was labelled ( the kids' bathroom led night light), and it said .3 watts. Multiplying that by 24 hours, 30 days, it consumes 216 watts a month. there are around 20 devices with little lights on the front, so assuming they all use this amount, I'm consuming about 2 kilowatt-hours of electricity per month running standby lights. At 10 cents a kilowatt-hour, that's roughly 40 cents. So that's good to know, and it puts my mind at ease on what power is being consumed by what objects. It's strange, because when I touch the block of a power supply on these items, it's typically hot to the touch, which makes me think 'huge energy use', but it turns out this is just a perception, not a reality.

At this point, it seems that the most efficient thing to do would be to abandon the solar photovoltaic array, and put up a clothesline in the back yard, so that we could stop running the dryer so much. It costs us 54 cents in electricity every time we run the thing for an hour.


Anyway, the next 3 days will be busy, between karate class, work load, and more karate (I got invited to try out the karate teaching school for a couple of hours on Tuesday, which I'm super-pumped about). I'm hopefully going to finish the pre-soldering after karate on these days, and get the panels mounted on Thursday, or at least that's my hope, so there may not be another post until Friday, as there won't be much to add. Maybe I'll do a 'pictures' post tonight, to show the components.

2010/03/25

Solar energy -- the math

Here's a link that explains the math, and keeps me from just plagiarizing what is already out there.

greenecon.net

OK, so as my last post revealed, I'm going to attempt to create solar panels from scratch. This is kind of a neat engineering problem, where I have to address the following questions:

1. How do I protect the solar cells while still exposing them to the sky? What about hail? How much reinforcement do I need if 2 feet of snow falls on them?

2. How do I attach them to my roof? Or would ground-level poles be better?

3. How big should the final system be?

4. What is the best wattage/voltage for the panels to run at?

5. how big should the panels be?

6. How much heat are they going to generate, and if I build the panels from wood, will I end up starting a fire on my roof?

7. Is it better to put more panels up, mounted flat to the roof, or to use less panels, and mount them at a sharper angle? What kind of extra reinforcement will I need to deal with wind, if I mount the panels at an angle.

So, to help answer these questions, I have looked at a lot of websites, watched a bunch of youtube videos, and really haven't come up with a lot of answers. The main thing is to avoid over-engineering this project, and paying way more than I should. So what I've done is ordered a enough supplies on Ebay to build a single panel, at 129 watts, and 18 volts, along with a simple grid-tie inverter that plugs directly into a wall socket, and a 'kill a watt' power meter to monitor what amount of electricity is generated. The shopping list so far?

$99.99 for a 300 watt grid tie inverter, rated for an input of 14-28 volts
$138 for 72 1.8watt, 0.5 volt solar cells plus miscellaneous parts to attach them to each other
$21.99 for a sheet of plywood to mount the panels to.

So, the math on the solar panels is like this:
I'm building 2 mini-panels of 36 cells connected in parallel, which gives us 18 volts (0.5*36), and 64.8 watts (1.8*36) I can then connect those 2 panels in series, to keep the volts steady, but push the amps up, essentially to stay under the 28 volt limit on the inverter.

I'll add some pics once they arrive, and once the panel is built, so that its construction will become clearer. The construction of the actual panels will probably require about 50 hours of soldering/building time, so I'm imagining getting the panel into operation about mid-April.