RSS
Facebook
Twitter

Friday, January 16, 2015

Azure IoT Hackathon

Yesterday (technically today) I participated in an Microsoft Azure IoT Hackton at IronSource offices.

When I heard that several CodeValue consultants were asked to come and I knew I asked if I could tag along.

B7Y8SkYCUAAIfWu

Microsoft organized a great event – good venue, food (Pizza!) and very talented developers.

10915136_10152776995923768_4074683655461051494_nEach team had Raspberry PI (Model B+), Arduino (Yun) and/or Tessel and needed to create an innovative solution using an Azure backend – with not a lot of time to spare.

We CodeValuees got to meet talented and enthusiastic developers and to help them to utilize these new cool “IoT” devices. And let me tell you - my three years worth of high school electronics classes really helped.

We continued hacking until midnight then each team got to show what they’ve created:

  • an application that provides directions to the closest free toilet a.k.a “Smart bathrooms” (2Seek) B7bTIz2CQAEX3vZ
  • Dog tracker
  • parking spot finders (1st place)
  • and many more that I didn’t had a chance to see.
  • B7dBKibCEAAN23e (1)

In-between working with other teams we even managed to hack our own solution – meet the new CodeValue parking gate controller (Park-Value?) with permission management applications for the web, desktop, IOS and android:

IMG-20150116-WA0003

It was an awesome event – we got to write code in JS, Python, something that looks like C (Arduino) and even C# (actually only Ariel). We’ve played with several controllers and connect resistors to other components (did I mentioned high school electronics :-) ).

 

I definitely plan to continue hacking using Raspberry PI/Arduino/Tessel/<other> perhaps even blog about it.

 

Until then – Happy coding…

Tuesday, December 23, 2014

The four species of Agile

SukkotI’ve been a professional software developer since 2002 and worked for several software companies over the years - some practiced SCRUM, some used Kanban, some had a tailored made methodology and some didn’t have any methodology in place.

When starting with Agile a few years ago I found out that

“All waterfall companies are alike; each agile company is agile in its own way.”

Over the years I learnt to classify each company’s methodology using two factors:

  • Ceremonies – whether the company follows the methodology practices – iteration planning, retrospective, and/or daily standups.
  • Intent – does the company understand the agile values, strive for continuous improvement, quick feedback and embrace change (among other things).

And so I bring before you the four types of agile companies that I had the pleasure (in most cases) working for in the past:

Neither ceremonies nor intent (Aravah)

I got to work for several companies that had no visible software development process - or had a process which made no sense.

It doesn’t necessarily means that those companies didn’t had a project management “methodology” – in one case we had a whole division “planning” and “tracking” the various projects. Unfortunately the beautiful chats and predictions never affected us developers, and other than reporting (read: lying about) project hours it didn’t bother us much.

It was chaos incarnated – no one knew what was going on. Some of us were overworked while others would drink coffee all day. After finishing each task I had to ask my manager what to do next and from time to time we’ve shipped a product.

And so we’ve worked hard on fixing bugs and implementing new features without real understanding of the overall picture.

Ceremonies without intent (Hadass)

After a few years I got to work for Uber company Ltd. (if you know me you might know which company I‘m referring to). We’ve created a new shiny project that just had to succeed (at least until it got cancelled), and as such we needed to use the latest and greatest of everything - including a new methodology called SCRUM.

Consultants have been hired, courses have been taught, developers and managers were trained and a lot of money has been spent to make sure that the project would be a great success.

I was hired a few months after that and when I got there all that was left was the bills and a daily meeting that took between 30min to 1 hour – which for no particular reason we had to do while standing.

My educated guess is that the company refused to change its ways and so all that was left were the ceremonies: the daily meeting, planning that was done first by the team, then by managers and finally not at all and retrospective that happened three times during the year I worked there.

Intent without ceremonies (Lulav)

In one of the last companies I worked for before becoming a consultant was a team that knew about agile – the manager had experienced SCRUM before and wanted to us it to build software. He had a clear understanding of what he needed and wanted and how to achieve it. He had SCRUM custom made to fit his vision and while some of the benefits were lost on the way the underline values were always there.

Since the team was willing we were able to add daily meetings, retrospective and backlog and while the company never really wanted our iterations we were able to build a methodology very similar to ScrumBan that made sense for us.

We had TDD, task boards (plural) and clear priorities and we managed to create value for our customers – fast while reducing technical debt.

It was not always simple or easy but we all had the same goal and so were able to find tools and methodologies that worked for us.

Ceremonies and intent (Etrog)

This is where I became an agile addict – this utopian company decided to go with SCRUM and managed to stick to that decision.

When we noticed that SCRUM didn’t make sense for customer support – we’ve used Kanban for opened tickets.

After doing SCRUM by the book for more than a year we knew where we need to adjust our methodology and what we needed to keep on doing (retrospective) we’ve experimented with different iteration lengths, we had XP practices (pair programming, CI, TDD) and continuous deployment.

We managed to be productive and effective, we had management who was willing to provide us time to learn and much needed feedback.

Etrog_at_the_Market_of_the_4_Species_at_Bnei_BrakHadassim2

Summary

There you have it – four types of companies based on my (limited) experience – some were successful and some failed miserably.

If there is one thing I learnt is that you have to have intent and understanding in order to succeed with agile – I got to use agile in waterfall companies and I also so bad agile (and “no agile) practiced even when someone up the corporate chain decided that the company is an agile company – the decision rarely left the board meeting if the developers (read: those who write the code) were not on educated or in some cases even part of the process.

Sunday, December 21, 2014

Take care when using comma in C++

A friend had a weird bug while working in C++ last week. As sometimes it’s required he had to convert a string to an integer and instead of writing:
auto result = (SomeEnum) (stoi(s, nullptr, 10));
He wrote this:
auto result = (SomeEnum) (stoi(s, nullptr), 10);
Such a simple case of misplaced parenthesis caused the result to equal 10 instead of whatever s was.
In order to understand what the hell happened here you have to understand how the comma operator works in C++.
In C++ a comma is more than just a separator – it has its own rules and can cause grief if used incorrectly.
For example consider the following code:
auto whoami = ("111", nullptr, SomeFunc(), 42);
Can you tell what would the value be?
Strangely enough (or not) the result will be an integer “42”.
The reason for this strange behavior is that the comma operator works by evaluating each statement from right to left discarding the result other than the last one.
So keep in mind that SomeFunc was executed even if we didn’t get back its return value.
The same goes for the following (simpler) version:
int someInt = (1, 2, 3);
In this case someInt would equal 3.
And you can go complete crazy in a way only suitable for job interviews (from hell):
int someInt = (i = 100, i+= 20, --i);
In conclusion – be careful when using commas because you might not like what you’ll get.

Happy coding…

Monday, December 15, 2014

New Speaking engagement - DevWeek 2015

Now that I’m back from NDC London I have a chance to plan my upcoming speaking engagements – and it’s going to be a few busy months.
I’ve already blogged about my plans  for the next few months but since then I found out that I’ll be coming back to London (always a pleasure) in March for DevWeek.
image
The agenda looks amazing, good speakers and good talks and I’m happy to have three sessions – one for each day:

Navigating the TDD alphabet soup

TDD, BDD, ATDD are all methodologies that enable incremental design that is suitable for Agile environments. It seems that every day a new xDD methodology is born with the promise to be better than what came before. Should you use behavior-driven tests or plain old unit tests? Which methodology is better? And how exactly would it benefit the development life cycle?
In this session, we’ll sort out the various methodologies – explaining where they came from, the tools they use, and discussing how and when to use each one. Here we will once and for all answer the question as to whether or not there’s one “DD” to rule them all.

Battle of the .NET mocking frameworks

Writing unit tests is hard, isn’t it? You need an entire set of tools just to start. One of the crucial decisions when building this set is picking up a mocking framework. But BEWARE! What you choose has the ability to make you or break you! In this session, This talk will cover the capabilities and functionality of the leading frameworks, showing the good and the bad of the different options (both free and commercial), and making them battle to the death!

Unit testing patterns for concurrent code

Getting started with unit testing is not hard, the only problem is that most programs are more than a simple calculator with two parameters and a return value that is easy to verify. Writing unit tests for multi-threaded code is harder still. In this session, I will demonstrate useful patterns that he has discovered over the years, and that have helped him to test multi-threaded and asynchronous code and enabled the creation of deterministic, simple and robust unit tests. He will also point out the pitfalls to avoid.

Hope to see you there!

Sunday, November 23, 2014

Unit testing concurrent code using custom TaskScheduler

Today I had a need to test an action that runs inside a Task:
public class ClassToTest
{
    private readonly IMessageBus _messageBus;
    private CancellationTokenSource _cancellationTokenSource;
    public event EventHandler OnNewMessage;

    public ClassToTest(IMessageBus messageBus)
    {
        _messageBus = messageBus;
    }

    public void Start()
    {
        _cancellationTokenSource = new CancellationTokenSource();
        Task.Run(() =>
        {
            var message = _messageBus.GetNextMessage();

            // Do work

            if (OnNewMessage != null)
            {
                OnNewMessage(this, EventArgs.Empty);
            }

        }, _cancellationTokenSource.Token);
    }

    public void Stop()
    {
        if (_cancellationTokenSource != null)
        {
            _cancellationTokenSource.Cancel();
            _cancellationTokenSource = null;
        }
    }
}
When faced with similar a code a developer has a “tiny” problem – how to force the code inside Task.Run to execute before the end of the test is reached.
And so we can write the following test that would fail - most of the times:
[TestMethod]
public void BadTest()
{
    var fakeMessageBus = A.Fake<IMessageBus>();
    var wasCalled = false;

    var cut = new ClassToTest(fakeMessageBus);
    cut.OnNewMessage += (sender, args) => wasCalled = true;

    cut.Start();

    Assert.IsTrue(wasCalled);
}

Why you should not use Thread.Sleep

The trivial fix would be to use Thread.Sleep (or similar Task.Delay) to make sure that the action would run before we reach the end of the test. Unfortunately adding this sleep will only cause the test would pass most of the times.
[TestMethod]
public void WorseTest()
{
    var fakeMessageBus = A.Fake&lt;IMessageBus&gt;();
    var wasCalled = false;

    var cut = new ClassToTest(fakeMessageBus);
    cut.OnNewMessage += (sender, args) =&gt; wasCalled = true;

    cut.Start();
    Thread.Sleep(1000);

    Assert.IsTrue(wasCalled);
}
The problem is that by adding a sleep we managed to achieve an inconsistent test while also making sure that the test would run a staggering 1 second – if that doesn’t sound like a lot think how much time 60 of such tests would run, what about 1000 tests each taking a single second?
In any case creating a test that could fail or pass inconsistently is a huge problem – it only takes a few such failures to convince your team to stop paying attention to that test – because it’s the test that always fail and soon enough they’ll stop paying attention to the continuous integration results and all of your tests would become useless (at least for alerting the team when a bug is introduced).

Creating a custom TaskScheduler

Looking for a solution I found a good post by Kjetil Klaussen where he suggests creating a new TaskScheduler that would cause the task to run in the same thread as the test thus causing the test to be predictable and simple:
public class CurrentThreadTaskScheduler : TaskScheduler
{
    protected override void QueueTask(Task task)
    {
        TryExecuteTask(task);
    }

    protected override bool TryExecuteTaskInline(
       Task task,
       bool taskWasPreviouslyQueued)
    {
        return TryExecuteTask(task);
    }

    protected override IEnumerable<Task> GetScheduledTasks()
    {
        return Enumerable.Empty<Task>();
    }

    public override int MaximumConcurrencyLevel { get { return 1; } }
}
Now we need to pass that scheduler to the executing task either by using a wrapper object or dependency injection.

Changing the current TaskScheduler

The problem is that you need to change your production code which is not always simple just like in my case where the action I needed to run was 5 calls deep and I didn’t want to pass the new TaskScheduler to each class on the calling stack.
I’ve started to look of a way to replace the current task scheduler.  Tamir D had a better idea - run the entire test inside a Task with CurrentThreadTaskScheduler.
Following his advice I’ve changed the Start method:
public void Start()
{
    _cancellationTokenSource = new CancellationTokenSource();
    Task.Factory.StartNew(() =>
    {
        var message = _messageBus.GetNextMessage();

        // Do work

        if (OnNewMessage != null)
        {
            OnNewMessage(this, EventArgs.Empty);
        }

    }, _cancellationTokenSource.Token, 
    TaskCreationOptions.None, 
    TaskScheduler.Current);
}
And now I could write the following test that would pass every single time.
[TestMethod]
public void GoodTest()
{
    var fakeMessageBus = A.Fake<IMessageBus>();
    var wasCalled = false;

    Task.Factory.StartNew(() =>
    {
        var cut = new ClassToTest(fakeMessageBus);
        cut.OnNewMessage += (sender, args) => wasCalled = true;

        cut.Start();
    }, CancellationToken.None, 
    TaskCreationOptions.None, 
    new CurrentThreadTaskScheduler());

    Assert.IsTrue(wasCalled);
}
Another benefit is that I could also test for negative conditions (something didn’t happen) since now all of the code runs synchronously.

Want to learn more?

There are many other ways to unit test concurrent code - if you’re interested in learning more ways to unit test concurrent code – I’ll be speaking about this topic at upcoming NDC London next week.

Until then - Happy coding…

Tuesday, November 18, 2014

Agile practitioners conference 5Whys challenge

If you’ve been following my on Twitter (or read this blog) you might know I’m part of the organizing committee of the upcoming Agile Practitioners conference.

banner11
In the last couple of weeks we’ve asked each speaker to answer five simple questions (a.k.a 5Whys) and got some interesting results:
Reading these made we want to write my own answers (technically I had to since I’ll be speaking at the conference as well) and as a reader of this blog you get to read them before everybody else.

Why should people come to the conference?

It is a great opportunity to meet other professionals who are passionate about being agile and want to learn more. In the past I had interesting conversations with conference attendees and learnt quite a lot.

Why should people come to my session?

To learn about TDD :)
If you want to add another great design methodology to your tool belt - this is the session you need to hear.
It doesn't matter if you tried TDD in the past or just beginning with unit testing.

Why did I choose the topic?

There have been quite a lot of misunderstanding in this field and although it seems as if TDD exist since the beginning of time it is still misunderstood and misused.
I hate to see software developers avoid this practice due to bad experience and lack of understanding of how it works and so I've decided to create a session that shows TDD with a "real" application - not just a trivial example.

Why I think you should go to “Agile from the executive floor – defining agility in business termsby Yuval Yeret?

I've been a software developer for a long time. Having to explain to "top brass" how a proposed change would benefit the organization become part of my job and so I want to hear Yuval's experience and incites on the topic.

Why did the chicken cross the road?

There is no chicken...
Chickens at the Kings Arms in Wing, Rutl by Jez B, on Flickr
Creative Commons Creative Commons Attribution-Noncommercial-No Derivative Works 2.0 Generic License   by  Jez B 

Sunday, October 19, 2014

4 programming books available online - for free

Software development books don’t come cheap and it’s fine. The author has spent countless hours and sleepless nights transferring his/her knowledge and should be compensated for it.
But some authors have decided to provide an online version of their book and in some cases even a free e-book you can download.
I found the book in the list below extremely useful and since I read them for free (legally free) I wanted to give something back in the form of a recommendation – so that both the author and and you might benefit from these good books.

 

Threading in C#

This is the only entry which is not a standalone book - It’s actually two chapters from C# in 4.0/5.0 in a nutshell book.
I’ve used (and still using) this book extensively both for learning new threading topics and as a reference. I found it to be easy to read and understand as well as extensive without being overwhelming.
It’s a must book for C# developers who need to write multi-threaded/task based/async code – which is about every C# developer out there.
Availability: Online, PDF

Introduction to Rx

IntroToRx_Cover
Although not the newest book on the subject this book is still relevant. This book guide you step by step on the path of using Reactive Extensions (Rx).
For only a few dollars (or less) you can purchase a kindle edition of this book.
Availability: Online

Dive into Python

cover-small
I’ve read this book when I’ve decided to learn IronPython a few years ago.
This book teaches by example - each chapter begins with a code example that shows an example of the chapter’s topic that is explained throughout  the chapter.
Availability: Online, PDF, Word and more.

Pro Git

pro-git@2x
If you never used a distributed source control before (you are using source control – right?) this is a good book to learn from.
It takes you step by step, not only explaining the various Git commands but also the concepts and overall picture of using a distributed system.
After reading a few chapters not only you’ll be able to set up Git and use it successfully but also gain a better understanding on why Git works this way and how to utilize it to benefit your project.
Availability: Online

That’s it for now - now go do some reading…
Related Posts Plugin for WordPress, Blogger...