30 September 2013

Flexible Mock

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
  end

  def test_access_to_local_variable
    p1=1
    fake = Mock.new :some_method do
      p1+=1
    end
    fake.some_method
    assert_equal p1, 2
  end
end

The problem with other mock libraries is that there's too much magic going on and syntax is tricky

    mock.expect :method, 'return', [:params]
    ...
    mock.verify
    # or
    mock.should_receive(:method).with("A", 1, 3).once
In more complex examples it gets hard to express what you want and remember syntax. I prefer more control, that's why my decision is to create object with custom method. In this method you can do whatever you want.

Another good thing of this approach is property of Proc to maintain context where it was called (look at the last example). This eliminates any need for internal verification via "mock.verify" since you can do it outside.

p.s. syntax highlighting I used can be found at github: highlight.js

26 September 2013

Thoughts on programming podcasts

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 use if you aren't paying attention, so save them for "activities" like waiting in line for a doctor, public transport, driving, cooking and etc.
You should make notes of what you've learned. It has the following benefits:

  1. the very activity of trying to summarize information makes you understand it better;
  2. I find it motivating when you can go back and look at the gist you've written;
  3. retrieving information from your memory noticeably facilitates further memory consolidaton.

Finally I will throw some podcasts I listened to:

  1. Changelog - news and discussions on hot topics
  2. Coderpath - stories about programming life
  3. Giant robots smashing into other gians robots - not particulary useful, but fun
  4. InfoQ - not really a podcast because often it's hard to follow idea without provided PDF slides by hand. Otherwise really good website, covers lot of topics
  5. JavaPosse - topic titles sound good, but terrible audio quality
  6. Pragmatic bookshelf - authors about their books
  7. Ruby Rogues - my favourite, best podcast on Ruby
  8. RubyShow - dense with useful info
  9. Ruby5 - short news
  10. SE Radio - very different topics, more enterprise than agile focused
  11. The Dev Show - fuuuuun
ps sorry but I really suck at placing commas in English =(

25 September 2013

Software construction == fighting bugs

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 your backpack, you have a long journey ahead of you.

27 August 2013

Who is the bad guy?

Following started as a comment, but at some point I lost it, and it became as big as a blog post.
Inspired by Secretary of State John Kerry: Use of Chemical Weapons in Syria 'Undeniable'
Damn politics...

To me it looks screams like another revolution made by USA in order to change uncooperating government. They give rebels weapons and now they frame Assad and use their powerful media to present it in a fitting color.

We've already seen such thing in Georgian-Abkhazian conflict, where murderous georgians backed up by NATO weapons invaded Abhkazia. After Russia intervened to stop the agressor US propaganda showed it's true face, saying that Russians were killing Georgians.

I vividly remember video that a few years ago enraged me and my fellow countryman.
It was both sad and funny to look at a girl with her aunt, who were victims, invited for an interview to Fox News(*). All US media was saying that Russia attacked Georgia and was showing burned city, saying it was Georgian city bombed by Russians. When in fact it was Abhkazian city bombed by Georgians. Girl's aunt only agreed to this interview knowing it was live so it could not be censored. And when the time came she said thank you Russians who saved US, it was Georgian who were killing people - thing contradictory to what media was saying. And after this words unusualy long commercial break happened.

I know it's not all black and white, but more and more it looks like the bad guys are not the Muslims or whatever, but the evil plotting US government. They surely have a grasp on media, you can't deny that.

... and yeah, it's a frickin Russian flag on my avatar... So it's biased. But in no way do I support my own government, simply proud of my history, and IMHO it's the lesser of two evils

(*) Interview
Aunt afterwards describing what US media was presenting (with ENG subtitles)

22 August 2013

Private testing

If you do testing, then, like me, at one time you probably came up with a question "How do I test private methods?". The tricky part about private testing in static languages like java, is that you can't access private methods from outside.

There are couple of ways to test private methods. You can do it through reflection, you can easily find libraries which ease the syntactic pain. Or you can make it package-private, this way you can use them in your tests, but users of your library still can't see it (though it can turn your own package into a tangled mess).

And both things are WRONG! And many people know it. Go to stack overflow and you will see lot of upvoted results, how it is "implementation detail, don't test it".

In my opinion, you should test this kind of methods. If you closed your IDE/Editor of choice and opened Google, then chances are it's important enough to have a unit test. Do you smell it? I certainly do... And it's coming from your code!

If you want to test it - then it's important, if it's important, than why isn't it in your public API? Because class users don't care about, it's not class's responsibility. If it's not your class responsibility then go create a new one. Now you can test it all you want and then refer to it from the original class. Noisy teenager moved out from parents and has a separate life, the end.

At first glance people who use Ruby, Python and other dynamic languages without a strong notion of privacy are happily neglected to this problem. You can use private methods as easy as the ones that intended to be public. That's actually very scary. In Java on every method creation you have to decide whether it's private or public. If you don't have to do this you might end up with a bunch of monster classes (been there, done that, not proud of it).

21 August 2013

Technology whirlwind

Ok, I've had this on my mind and up my ass for some time.

Today around you there's too many technologies, programming languages and frameworks! I remember my school days and passion about Pascal and Delphi, all the assignments in information technology class, the way I would optimize these little quizzes... A lot of great memories.

But then I went to university, got familiar with C#, and things started boggling me. Is it used in real life apps? What's beyond C#? Where can I find work and what skills do I need? And really at that time for there was no answer to the question "What do I do next?".

And so my mind kind of stranded off programming. University classes were useless and had not a single hint of real life applicability. So there's was no use in studying hard. All I did is studied just enough pass next exam and then play computer games and football.

However, as I got lazy and dispassionate, another thing really have blossomed - The Internet! After serious football injury there was a lot of time to contemplate and what I've stumbled upon really changed the flow of my life. Suddenly, I was in a land of opportunity!

At first I got familiar with Java and it looked like every company wanted a Java developer: Netcracker, Haulmont, EPAM, Magento. Another ones offered mobile iOS and PHP jobs (never was into that kind of stuff). About half a year later I familiarized myself with Ruby and RoR by completing 2 online MOOC courses from EDX, and oh... my... God... it blew my head right off! Than I started practicing TDD and a little bit of Python. Than I got passionate about Scala. Got a better at HTML, CSS and JS... and...

There's so... many... freakin'... technologies! With the availability that Internet provides you can learn any of them. Every technology comes with a different style, different community and a viable awesome career path.

And it gets better worse every year. Right now I work as a Java dev, but after graduation I will want that to change.

We are in technology Whirlwind, so dizzy and captivating it is.

Hello Blog

Greetings, guys and gals(who am I kidding?), guess what, now I have a blog!

As of now now it looks kind of ugly and... empty. But it seems you can fully customize it (even javascript).
On the other hand I'm too lazy and empty is good. There's Russian saying:

"Conciseness is a sister of talent"
...on this note this post shall end.