How fast should Unit tests be?

March 21, 2014 Leave a comment

Well, fast. Damn fast, if you ask me. I have ~630 unit tests running in somewhere around 90 seconds. You can’t even imagine how slow it feels when you are trying to run them frequently. But when build time alone takes about 6 minutes, how fast your ut runs is negligible :(

And then there comes Greg Young with his idea of fast unit tests:

So I will keep trying to get to that level in my project. Hopefully so are you.

Categories: Development Tags: , , ,

Limit your abstractions

December 15, 2013 Leave a comment

“You can solve every problem with another level of indirection” they say. But don’t be fooled! Full quote goes something like: “You can solve every problem with another level of indirection, except for the problem of too many levels of indirection”. And keep that in mind when writing your code.

I’m currently working on refactoring quite important piece of code in big, complex project. Fun thing to do, I enjoy it tremendously. But I’m new to the project and I’m lost in the code. Thankfully I got some sequence diagrams and sequence diagram along with long explanation what this part of system does and why. Then I looked at the code, trying to debug it few times to know where I am and what I’m working with.

What I saw there looked nothing like the diagrams I got. After few hours (days?) I could find some relation between code and description I received but it was way to hard. Even though the system is complex and functionality of this part is not exactly piece of cake, the whole thing had so much code and so many different layers that it is sill nearly impossible for me to tell where am I and what the code does in this place.

Interfaces are cool. Extracting functionality to small, well defined classes is what I like. But once you start jumping between five or six different classes to complete one small action you start feeling that something is definitely not right. At the moment I have opened something like 20 or 30 different classes to go through the workflow, and this is just preparation for actual calculations happening in different place (thankfully this is the part I’m not going to touch this time).

It may feel better to extract class from one piece of code. But I’ve noticed that the same type, Report in this case, is represented by four or five different classes with slightly different naming and functionality. But it is still exactly the same report in exactly the same system! Why not just settle for one, well defined object? Need to get something from database – jump between three or four objects before you will get to actual query.

Flatten your abstractions. Extract classes where needed, but don’t hide everything behind interfaces when not needed. Those are your domain objects, there’s no harm in your main object knowing about exact implementation of one of sub objects, those two are tightly coupled anyway. Sometimes we need to get our hands dirty in all those pesky details. That’s OK, it needs to be done. Extract methods to make it easier to understand. Make code short and concise. But fight the urge to extract every single detail into new class hidden behind interface because it needs to be testable. That’s not how tests are supposed to look most of the time.

So when to abstract things into interface, different class, different layer altogether? I’m not sure myself. Right now I’m just following my guts, making mistakes in fixing them in future when I see that something is not right. One day, I believe, I will master this to create perfect code, but not today and not tomorrow. But I’m getting there, you can be sure!

Categories: Development Tags:

Learn your shortcuts

December 13, 2013 Leave a comment

How much time do you spend in editor working with code? Debugging it? Hopefully a lot. How much time do you spend working with your keyboard? Hopefully most. Mouses are evil. It takes to much time to move hand to mouse, select option, click, move hand back.

But there’s a better way! Learn shortcuts! Visual studio have lots of them. Resharper too. Web browser? Sure does. Total Commander? Windows? Vim? Shortcuts all the way.

I always tried to learn how to do things with keyboard rather than mouse for more efficiency. Some time ago I started writing stuff in Vim. That’s when I learned mouse is mostly useless when writing stuff. Now I’m back to good ol’ Visual studio with Resharper and I usually don’t even think about touching mouse. And I cringe when someone’s searching menu with mouse or (God forbid) clicks right mouse button to do something.

First – shortcuts are simple. So simple that after few usages, your fingers will remember how to do them without you thinking about what exactly needs to be pressed. Ctrl+Shift+B for build is as natural for me, as Ctrl+S ever was. Ctrl+U+R, Ctrl+U+U and Ctrl+U+L are so natural for me that I had to check this shortcut right now because I didn’t remember it. My fingers do – when I’m in VS they know what to do. F9, F10, F11 – you all know them in debugging I hope. What about Ctrl+F10 and Shift+F11?

But I don’t know all the shortcuts. When and how do I learn them? I go with simple rule of thumb – if I’m doing something once – that’s cool, I can click it. Second time – well, ok – I will go with you one more time, you evil mouse! Third time? You won’t trick me, I’ll google the shortcut and will google it again and again until I’ll teach my fingers how to do it properly. No more pointing and clicking.

And my hands are better on keyboard as well, holding mouse is fine only when I’m shooting at those filthy zombies. Left 4 Dead – here I come!

Categories: Development Tags:

Casting to type variable – generic without Generics

November 23, 2013 Leave a comment

Sometimes you cannot write simple generic code like Method(). When is that? Well – in unit tests for example. Imagine you have test that needs to run multiple times – each time for different type. Solution I’ve witnessed in production code is to write separate test for every type and copy-paste the code around. Terrible, terrible solution!

Another way could be to create separate tests, but extract common code to external method – now we can use generics – and just call that method. Much better, but still – having dozen methods just to call other one? There must be a better way!

And surely there is – data driven tests. [TestCase] in NUnit, [Theory] with for example [InlineData] in XUnit or even poor [DataSource] in MSTest. But it cannot pass generics, test method cannot be generic.

So I looked why exactly this test requires generic type. It all comes to this

    T result = (T)variable;
catch (Exception)

So just casting to see if object is of given type. I didn’t want to mess with test logic to much, just to reduce code quantity. So how do you write casting code without generics and knowing just the type (because we can of course get Type variable as parameter)? Simple as that:

object result = Convert.ChangeType(variable, type);

Is it perfect? Of course not – it returns object because it does not know the type it’s casting to. It might not be all that useful then. But in my case – it fits perfectly. Throws the same exceptions if casting is not possible, verifies if object can be assigned to variable of given type as the old code did. And now I can just have one test method with dozen test cases in it!

Categories: Development Tags: , , ,

Don’t be afraid of changes

November 16, 2013 Leave a comment

Early this year I changed companies and started working for a bank. They promised me working with highly specialized software, global standard in its category. And they promised me .Net platform. First they delivered, second they failed for more than 9 months now.

Now I do complain a lot. But at the same time I am afraid of acting on what I feel. So I tried explaining it to myself that this is just temporary, they will update their software, we will go forward. Now I finally see things as they are – change is no coming any time soon. And its my life and no one will take care of it better the myself.

I’ve raised my issue to people above me. And starting from next week I’ll be back at .Net project, WPF, WCF, tasks – you name it. It wasn’t hard. All it took was a bit of honesty about the situation and idea on where I would lie to be and everyone was happy to help.

Don’t sit quietly in uncomfortable situation, take care of your professional life and be honest about your work. The world is your oyster!

Categories: Generic

Documentation matters

November 5, 2013 Leave a comment

Agile. Lean. Extreme Programming. Yadda yadda yadda. Over and over again. Day after day.

The thing is, business likes to think agile is whatever suits them at the moment. Be it changes in requirements every week, lack of proper testing or defined process of work. Or lack of documentation. Why would you need one? Code is self documenting. We have power point presentations. We have sharepoint!

But at one point you are going to work with one, huge environment. Different applications. Most of which you have never heard about. And someone reports a bug. Data in system Ypsilon is incorrect! Fix it ASAP!

I took my time trying to figure out what is this Ypsilon unicorn I’ve never seen before. No idea. Called few guys, got some answers but nothing that would put me any closer to fix. But I knew that Alpha system I was working with most of the time was producing csv file with data (yup, 21st century and we’re still sharing data using csv with bad formatting and FTP). But the code there wasn’t touched in 3 years. So how it suddenly broke when October started?

But everyone was convinced that Alpha system was broken. Fun part was when they were telling me what data was expected in file. “Column A needs to have 111!”. Next day: “Column A needs to have 543!”. Later: “Column A needs to be 111! Don’t change it!”. They didn’t know themselves.

Week passed when they were trying to figure out what’s wrong with data in the first place and what’s the real error they are experiencing. Today few guys finally sit down, looked at the data they are getting, looked at few systems and figured: it’s not Alpha’s fault! It’s Gamma that’s wrong! Yup, you got it. I barely knew Gamma system exists, even less what it does.

How could this time be saved? With documentation. First – global architecture of the system. What systems are working together. How they are sharing data. WHAT they are sharing. Second – documentation of interfaces. If system produces output that’s being consumed by other systems it needs to be documented well. We can’t leave it to code. Code changes. Not everyone has access to code of every system. This leads to other developers making assumptions based on names of columns (if those are named in the first place) which can be misleading. And those assumptions may be based on small subset of data and not right in long game.

Have we had documentation finding bug could take one day. Not seven. Or we could not have bug in first place.

Lack of documentation is not agile. It is just stupid.

Categories: Development

Spot an error: Simple.Data.PostgreSql

October 11, 2013 Leave a comment

Take a look at the code below and spot an error:

if (resultRequired)
    return insertData.Select(row => ExecuteInsert(cmd, insertColumns, row, onError)).ToList();
    insertData.Select(row => ExecuteInsert(cmd, insertColumns, row, onError));
    return null;

Got it? At first I didn’t. Debugged once but it ‘appeared’ to be ok so haven’t noticed the issue. Yet, insert did not happened, data did not reach database. Debugged once again – and quick glance over the code was enough to see it. Of course my code took else path as I didn’t need results of insert for anything. And there is select, but it’s never used – ergo it’s never even triggered. That’s one of nice features of linq – it will only run query if you actually need the result. But on the other hand – it is pretty easy to sometimes forget you’re not firing the query and it won’t do any of those function calls you set up in Select call.

Adding ToList() at the end should fix the issue. Time to do some pull requests on Github!

Edit: and so I did – fixed, wrote some unit tests to make sure it will work in future and posted pull request on GitHub. This is my first contribution to Open source. And I hope it won’t be the last!


Get every new post delivered to your Inbox.