Posts

Showing posts from September, 2013

Flexible Mock

Image
Hey, I've been posting too many abstract shit lately, how about some code? So here's my simple Mock class and tests for it (to show how it works). Class class Mock def initialize(method_name, block = nil ) block||=Proc.new do |*args| args=args.first if args.length==1 yield(args) end self.class.send :define_method, method_name, block end end Examples class TestMock < MiniTest::Unit::TestCase def test_mock fake = Mock.new :some_method do 'value' end assert fake.some_method == 'value' end def test_mock_single_parameter fake = Mock.new :some_method do |p| p*2 end assert_equal fake.some_method(2), 4 end def test_mock_many_parameters fake = Mock.new :some_method do |p1, p2, p3| p1*p2*p3 end assert_equal fake.some_method(2, 3, 4), 24 end def test_lambda_parameter fake = Mock.new :some_method, lambda {|p| p*2} assert_equal fake.some_method(2), 4

Thoughts on programming podcasts

Image
Do you remember any movies or pictures about the "Gold Rush" in America? When thinking about getting knowledge today I imagine exactly that: a man with a sieve filtering out a lot of dirt just to find a tiny golden nugget. It's not like there is a website where you can go and read all the facts that YOU don't know but should. Acquiring knowledge is a work of going through a lot of information and finding missing parts for your puzzle. I probably sound like an obsessed person but that's because I am obsessed. Today I going to talk about one such dirt-gold source, namely podcasts. I've been listening to a lot of these lately and, honestly, 95% is useless crap: things that you do know things that you don't need to know things that you aren't ready to know But every now and then you stumble upon something brilliant so a 5% that's left make all the difference. Following suggestions will help you to get the maximum out of it: There's no u

Software construction == fighting bugs

Image
This summer during obligatory in Russia military training I've read the famous Code Complete book by Steve McConnel. Most of the information was either already known to me or somewhat outdated. But after I turned over the last 900th page and closed the book I felt the "Aha!" moment. Most of the practices you do during software construction are aimed at the eliminating bugs and mistakes: code reviews, pre requirements, planning, refactoring, pair programming, testing and in a way even comments. Let's be honest: building stuff from the blank page is easy, not willing to throw away the project after half a year behind you and a ton of bugs atop of you IS hard. That is why TDD is very practical. At first having to write that extra code feels like a pain in the butt, but the amount of time you save later on debugging is golden. Software project just like many other things isn't a sprint, it's a... camping, so take time to put goodies in you