Just copied from an old email.
This was something I wrote in response.
Below would be a full working bit of code. It's rather pointlessly simple but it demonstrates my "way".
require 'minitest/autorun' class Whatever def foo( string ) return string + '!' end # def foo( string ) end # class Whatever class Test_Whatever < MiniTest::Unit::TestCase def setup() @o = Whatever.new end # def setup() def test_foo() assert_equal( ( 'yay!' ), ( @o.foo( 'yay' ) ), ) end # def test_foo() end # class Test_Whatever < MiniTest::Unit::TestCase
I've taken extreme measures to make my code more readable. While I have exceptionally lucid times, I often have a very hard time "fitting a program into my head".
The real problem arises when I create magical code on one day and then on some other day I am completely unable to comprehend it.
To help, I've been
- Breaking my problems down into sometimes embarrassingly-small pieces
- .. Each with its own test
- .. Usually with somewhat redundant comments.
- I've also thrown away regular alignment conventions.
- Ignoring performance improvements, preferring clearer and even redundant code. (lots of brackets, stuff on separate lines, trailing commas, etc)
Using a very small subset of Ruby's functionality. Doing things with brute force and which are far less pretty than possible.
In some cases I might even coerce code into something monstrous like this:
def test_foo() assert_equal( ( 'yay!' ), ( @o.foo( 'yay' ) ), ) end
The difference to me is that a bunch of programming that initially took me a very long time has now taken me a very short time to rewrite from scratch. Think years to weeks. The end result is vastly superior in every way to me. Faster, clearer, fully tested. Most importantly I'm enjoying myself!
I know that this means that if I ever manage to function with regular style conventions I will be forced to re-rewrite everything to follow them. If it came to that, I would be pleased to do it.. I love that kind of grunt work. =)
I know I'm learning bad habits, my code will be "unreadable" to others, I may not be able to read the source of others very well, and there are probably lots of other issues.. but I needed a new way to approach programming. It's either that or I just give up.
For very long methods, it's useful to have a comment at its final
end. When that method is expanded it can take up several screens and I can get lost easily. It's hidden when the code is collapsed though, so it's not generally in the way.