Thursday, April 19, 2018

Being More Agile

The slow growing product team at Milyli is currently making the transition to full agile. For the most part, this has been a smooth process. As owners, we believe in agile and trust that the team will do their best to get to the features as fast as they can and that by having less process we will be getting more features at higher quality faster. No one asks, 'Yeah, but what features will be delivered eight sprints from now?'

The hardest adjustment has been for developers. As engineers that believe in delivering quality that have been working in a more waterfally environment, mostly due to demands of customers in a very traditional market, it can be difficult to say that a story is done when the UI is just an ugly row of numbers. Even when that data proves the system is doing what it's supposed to.

It's important to remind the team that an ugly UI is OK. The team wrote the code that implemented the feature as designed. The feature has a full suite of automatic tests, unit, integration and UI when feasible and applicable. We could deliver that story if necessary or desirable for some reason. 

We didn't polish the UI because we wanted the stakeholders to validate the behavior and maybe give some ideas for the UI now that there's a straw man to look at. If we had also completed the UI and the behavior was wrong, we'd likely need to redo the UI as well. 

We will polish the UI in the next iteration. It might not be perfect even then. It might not be perfect before the users see it. But we can get feedback fast, act on it immediately, and have a fully tested deliverable ready in no more than two weeks time.

And that's what agile is about.

Sunday, April 8, 2018

Unit Tests and SOLID Code

One of the realizations that we had at our office over the past few years is that SOLID code is easier to unit test. If code is easier to unit test, we will a higher level of unit test coverage. So a high unit test coverage number is decent indicator that our code follows the SOLID principles. Not foolproof, but decent.

Tests taking too long to write because you need to mock too many dependencies? You're code is probably doing too much and you're not following the Single Responsibility principle.

Tests taking too long to write because you have to factor in too many cases from base classes? You're code is doing too much because you're not following the Open Closed principle.

Tests taking too long to write because you need to test too many exceptional states? You might be adding too many exceptions to sub-classes by possibly ignoring the Liskov Substitution principle.

Too many tests needed to cover a class effectively? Follow the Interface Segregation principle and have more classes that do fewer things.

Can't inject mocks into your tests easily? You really messed up that Dependency Inversion part of SOLID.

Not sure how universal this is, but it seems to hold for quite a few architectural styles. As always, this is my 2¢. You're mileage may vary.

Wednesday, March 21, 2018

Comment Your Code

Most code is for developers, not for computers. Computers have always been perfectly capable of reading long streams of ones and zeros, performing work on those stream, then spitting out results as new streams of ones and zeros. Sure, computers have gotten faster since those streams were stored exclusively on paper punch cards. And sure, we now have new ways of providing input and receiving results. But, most computers still operate in the same fundamental way. Computers don't need C#, Ruby, JavaScript, SQL, JSON, compilers, interpreters, monitors, keyboards, mice or VR. People and developers do.

Most developers don't garner any meaning from reading a bunch of ones and zeros. Most people need abstractions in order to understand any significantly complex idea. And, people make more assumptions than they realize. These and similar human failings are why the vast majority of software language features exist.

To get around the lack of understanding of binary information, developers use higher level, abstract languages to create software. A good number of developers think their code is perfectly readable on its own. Even if that were the case, the only way to prevent others from making incorrect assumptions is to provide more context than the method calls themselves. This means commenting your code in plain [language of your choice] to describe the responsibility of each member.

Similar to how unit tests provide a second set of code to ensure correctness, providing another description in addition to the code itself will help prevent misunderstandings about how to use and maintain your code. Some people go so far as to claim that unit tests are an adequate replacement for comments. Bollocks. If your code doesn't take advantage of the popup guidance that intelligent code completion tools (IntelliSense to us Visual Studio fans) provide to instruct others on correct usage, you're not using the best tool available.

Other developers claim that commenting code takes too much time. But if an engineer can't type a description of the responsibility of a class, interface, property or method in under a 15 seconds, they either don't understand its responsibilities or they need to learn to type faster. Either is a deficiency in any engineer's professional skill set. Those extra seconds of typing may add up, but they are a tiny percentage of the time that should already be spent on designing, writing and testing that code. That time is also miniscule compared to the amount of time that will be prevented maintaining the documented code and fixing bugs when uncommented code is inevitably misused.

Thursday, November 18, 2010

Development Database Setup

Hello Everyone,

If you're working on an application of any significant complexity, one of the tasks developers must deal with are database set up scripts.  One trend that I've noticed over the years is that projects where developers maintain a set of scripts that setup the databases from scratch tend to go more smoothly.  To be clear, this process includes tearing down any existing database, rebuilding the database to the most recent release version, loading good test data, and finally running all change scripts for the next release.  Though those last two steps can be reversible depending on the project.

Example the first.  I have a system.  Yaay!  I need to make a change to the database.  It's a relatively complex change involving creating a new table including keys and indexes, copying data from an existing table to the new one, and removing columns on the old table. Booo!  After I make all these changes and have the scripts in place, I need to test that I did everything correctly.  On the plus side, I have a thorough rebuild process in place.  I can click a button and the whole system is rebuilt, I find problems and fix them myself and I know without bothering any other developers that the scripts are good.  Yaay!

Example the second.  I have a system.  Yaay!  I need to make a change to the database.  It's a relatively complex... yada yada yada... I need to test that I did everything correctly  Booo! Unfortunately, databases are not torn down and rebuilt as part of the setup process, they are only modified.  What do I do to easily test my script.  I can undo the changes by hand and rerun the scripts to make sure, or I can check in the changes, hope for the best and let the next developer find any problems and repeat the cycle with yet another developer.  Which one do you think ends up happening more often?  Booo!

This may not be a huge issue that comes up a lot, but when I see emails going out a few times a month with issues lasting a couple of days because things are done the latter, it reminds me of how many things that developers already need to keep track of and this is just another disturbance.  In the end the amount of work to make the changes is the same, but having a complete rebuild is a way to minimize disturbances and keep developers developing.  

If starting a new project, I highly recommend the complete rebuild approach because the hardest possible task would be overcoming momentum and switching from one process to the other.

Thursday, June 17, 2010

IQueryable and Repositories

One approach for coding repositories that I have seen a couple of times recently is that of exposing IQueryable collections through the repository interfaces. On the one hand this allows an extremely convenient way of exposing a flexible data source to your business layer or controllers. However, it also breaks the Single Responsibility Principle thus creating less reusable and testable solutions.

By allowing your business layer to define queries within its own methods, those methods are taking on two responsibilities, performing data access and performing logic on the results of that data operation. It's all well and good to complain about not hitting some high ideal, but what affect does this have in the real world?

First, if you happen to reuse the same data query in multiple parts of your application, you would have multiple places that you would then need to make changes and test. DRY this isn't.

Second is the testability of this code. In the terms of this afore mentioned white paper, you would need to write your tests for your business layer or controller in such a way that the state is correct as well as the interactions, for each method. As the paper says, testing the state aspect requires a larger set of data. Why would you want to write that extra code for every method in your business layer / controller that uses the same query? If you don't test each one, how do you know they work? How do you know when they break?

The simple solution is to keep your queries in the repositories behind the repository interfaces. Test the state of each one thoroughly and then you only need to test the interactions in your business layer. Yes it requires an extra function call in your business layer, another method in your repository interface and another function declaration in the repository. And while that might be a lot of words, it's only four or five more lines of code for each query (including brackets on their own line in C#.) And if you reuse your queries, you get those extra lines back anyway.

I will certainly concede that no one paradigm fits all of the software problems out there. Maybe what I suggest is overkill. Maybe it's another example of a programmer too set in his ways. But by exposing IQueryable collections through the repository interface, we're ignoring a number of the principles of coding that have been making our lives easier and our code better for a while now.

After a second read through of the article, K. specifically states that enumerables could be returned instead of queryables. Not that this is the only publication I have read where queryables were suggested. Let's just say that now all four of my regular readers know where I stand on the subject.