Friday with Filip – Demystify Spaghetti Code

Posted by Filip Ekberg on October 26 2012 1 Comment

Welcome to this week’s Friday with Filip!

Two weeks ago we looked at Dealing with Code Complexity using NDepend. This was the first episode in the webinar series on Code Quality.

This week I am happy to share that Patrick Smacchia and I have recorded the second episode! This week it’s about dependencies, dependency cycles and spaghetti code. I really enjoyed doing these sessions with Patrick and I really hope you will enjoy it just as much. Let me know what you think of it!

Vote on HN

Friday with Filip – Do you deliver high quality?

Posted by Filip Ekberg on October 19 2012 1 Comment

Welcome to this week’s Friday with Filip!

During the Fridays with Filip we’ve looked at how to ensure that certain parts of your project conforms to a high standard. I’m talking about the following:

There are so much more things (even small things) that matter when you want a successful high quality project/solution. You can’t rely on a decent testing strategy, security awareness and rapid development to get high quality. There’s still a lot of other aspects in your project that you need to take into consideration.

When I studied for my Software Engineering degree there was a lot of talk about requirements documentation, design documents and all other kinds of documents that you had to write in order to get a good overview of the project. But there were actually less talk about how to retrieve this information.

One of the first things you’ll learn when talking about domain driven design is: use a language that both you and your customer understands. In order to get all requirements or the system design onto paper, you need to understand each other. Do you see where I’m getting at? We need to step back to the basics, to things that was important even before computers and software projects; we need to be able to talk to each other so that everyone understands!

Previously this week I asked something a bit weird on twitter as you can see in the image below.

At a glance, this might be a very weird question. Why would you explain something that is so far away from their reality? Someone responded and said that they would just not do it because they would probably be killed for knowing too much.

Technology is amazing and as a Software Engineer with a burning heart for programming I tend to try (almost) everything new that arrives to the market but not everyone do and this is very important not to forget. Since not everyone is so used to technology, we need to work on the way that we communicate with each other. A couple of years back I built a website that had to do with cars and the people that wanted the website had no idea how the process of building a website looked. So I had to use analogies they understood, I had to talk in car terms.

Therefore it’s very important to actually step back and find a common ground between you and your customer. Otherwise it will be near to impossible to deliver high quality; you’ll never get the right requirements!

As I said in the start of this there’s not one or even 5 things that will ensure you deliver high quality, you need to work on all different aspects. Here are some concrete suggestions (that might seem obvious):

  • Find a common ground between you and your customer so that you speak the same language
  • Design the architecture in a way that you could talk about objects and structure in the application which the customer understands
  • Use the right person for the right job!
  • Write lots of documentation before, during and after the delivery
  • Have small iterations in the beginning where you continuously deliver parts of the system for reviewing
  • Do code reviews, swap people around and let everyone review everyone
  • Don’t be afraid to ask your customer questions if you don’t understand
  • Don’t be afraid to ask you colleges for help if you’re stuck!
  • Don’t get used to putting work off by writing TODO’s in the code
  • Take a couple of seconds to write a comment about a method or a complex code block
  • Remember that we’re all human, everyone can make mistakes; help each other out and solve them together
  • Stay on top of new/old technology and use what is best for the project/customer. If a newer framework or tool means more job, maybe it’s the wrong tooling for this job; even if you want to play with the latest toys

This list can go on and on, frankly there have been books heavier than my laptop written about each point in the list.

What’s your pro tip to increase quality in the code, project and customer communication?

Vote on HN

10 questions about Surface

Posted by Filip Ekberg on October 17 2012 4 Comments

The Microsoft Surface is due to be released October 26, 2012 and this is less than 10 days from now. There’s been a lot of hype around this particular tablet and I’m very happy that the release date is in just 1½ weeks!

Some background

If you’ve somehow missed what the Surface is or what the Surface looks like, below is a very nice picture of it. Basically it’s a powerful and lightweight tablet that runs Windows 8.

Up until today, at least I have always felt that I at least needed 3 devices. One touch optimized tablet (iPad), One laptop to bring in to meetings that I can also code on and last but not least a very powerful workstation that I can work on. With the Microsoft Surface, we’re closing up with the all-in-one device — simply bring the Surface with you to meetings, watch movies with it in the couch or work on it.

How come information about such hardware ends up on a programming blog? Because the programming model is very interesting! If you’re an iOS developer or an Android developer, you’re most likely familiar with the whole “store” life-cycle; which is great. This will (hopefully) lead to much more high quality applications and less bogus software.

Surface comes in two (majorly) different models

The first Surface that is released October 26 will run Windows RT on an ARM architecture. The RT version of Windows 8 is designed for limited use such as:

  • Watching movies, surfing the web, chatting and all that other tabletty stuff
  • Minor work in Office; Word, Excel, Powerpoint, etc.
  • Only run applications from Windows Store

There’s of course a little more to it, but the last point is the most interesting one. You’ll only be able to run applications on the device that comes from Windows Store! Hence that you will not be able to put an application on an USB drive (yes Surface has a fully functional USB 2 port) and run it.

This might not be new information to you, but it’s still pretty interesting. That’s why there’s another Surface version released in January, called Surface Pro. This version runs on an Intel Core i5 and used the full version of Windows 8. This lets you install any applications that you want and use it as any other computer that you are used to. Plus it’s still a tablet!

Below is an overview of the Windows RT Architecture; The RT API is a little bit different (limited).

Now with a little bit of background, let’s take a look at 10 questions and answers about the Surface!

All the following information is based on information given by the SurfaceTeam on reddit.

1. Why doesn’t surface come with 3G/4G

For 3G/4G we looked at several elements when deciding on what features to include. We knew that the primary use was going to be in the home, we looked at tablet sales data 2/3 WiFi, 1/3 Mobile Broadband. Of the 1/3 sold, 1/2 were activated. Phone hotspot / tethering use was also a consideration.

2. Will Surface be available in any other countries than listed at the moment on the release date?

Unfortunately, no.

Of the primary launch Markets we are launching in, we have keyboards specific to those languages
We are not announcing any new markets beyond what we said today. However, we will for sure be expanding to more markets in the future.

3. Why is there no USB3 port, only USB2?

Currently there’s no ARM SoCs that have an USB 3 controller and Surface is based on ARM, which means no USB 3.

USB 2.0 based on capability of the ARM SoCs during our development timeframe.

4. How much will it cost?

There are a couple of different options available (USD prices only).

  • Surface with 64GB Storage and TouchCover – 699 USD
  • Surface with 32GB Storage and TouchCover – 599 USD
  • Surface with 32GB Storage without TouchCover – 499 USD

5. Is there any NFC or GPS on Surface?

There isn’t a NFC feature in Surface RT; We use Wi-Fi based location services, the device doesn’t have a physical GPS sensor.

6. Why is the kickstand angled as it is?

We wanted the screen to be normal to the face. Voila, you then have a 22 degree angle.
Seriously we did a ton of studies around lighting, reflection, ergonomics, table height, etc…. and then made sure it looked perfect and felt perfectly balanced.

7. Will Surface support bluetooth headsets?

Yes, Surface supports Bluetooth 4.0 and will work with Bluetooth headsets.

8. What happens when I flip the touch cover to the back of the device, will there be accidental keystrokes?

Both TouchCover and TypeCover have sensors that understand orientation relative to Surface… in fact the Type and Touch Covers talk to Surface to figure out their relative position no matter the orientation of gravity of the device (pretty neat!). There are 3 positions modes: Closed (keys and mouse are off), Open to 180 degrees (keyboard and mousepad on), beyond 180 degrees to the back (keys and mouse off). That way you can flip back the covers and feel secure you are not pressing keys by mistake.

9. How does the TouchCover work?

Touch Cover has a very special digitizer that we invented.. it senses the impact force of your key presses. We designed super-fast electronics and smart algorithm in the keyboard so that Touch Cover can profile your key press down to a 1ms (1000 times a second). Using that information Touch Cover can infer if the user meant to press the key or not.. It is a smart key. So even though there is no key travel, the user can rest their hands on top of the keyboard and find home position without accidentally triggering keys.. pretty cool! The first time I typed on a full working version of Touch Cover, I typed just as fast as I do on a normal keyboard. I am confident you will be able to type significantly faster on Touch Cover than an onscreen keyboard. And with a little practice you will even do better (maybe even faster!) For folks who really love and really need keys that have travel, then Type Cover is one of the best keyboards I have ever used (desktop or other). It has a super awesome snappy key mechanism that feels great (has a strong hysteresis curve)

10. So there’s no NFC, why?

For the product design experience we were aiming for with Surface, the Mg metal enclosure, including the back case, was critical. This made good antenna design for NFC a trade-off in our development process.

To learn more about Surface, check out the video below!

Personally I can say that I am very excited about the Surface products, both RT and Pro.

How excited are you?

Vote on HN

Friday with Filip – Dealing with Code Complexity

Posted by Filip Ekberg on October 12 2012 2 Comments

Welcome to this week’s Friday with Filip!

Yet another interesting week has passed with lots of things to discuss. Before we dig into this week’s subject, I just want to take a brief moment to share something interesting that I found (it was recommended by someone I know from IRC). There’s a hosting company called EDIS based in Austria that is now providing a very neat service; Raspberry Pi Colocation! As soon as I saw this I shared it with HackerNews and got a response from EDIS on twitter:

It’s worth just thinking about this for a second, let’s say that 1000 RPi’s could host some websites, instead of 1000 1U rack servers; imagine the power that would be saved (and the space).

Now to this week’s subject!

Last week we discussed how we could join new projects and do that efficiently and I got some great tips & tricks from a lot of you (in different channels). This week we are going to follow in those footsteps and see how we can actually find complex code in our applications. While finding complex code is important for all different programming environments; we’re going to look at this by using C#, Visual Studio 2012 and NDepend.

It’s a little bit different this week..

I recently recorded the first episode of a webinar series with Patrick Smacchia, the founder of NDepend and who better to talk about Code Quality and Code Complexity than with him? So this week, I’m happy to announce that you can watch the first video in this webinar series!

It’s a 30 minute webinar where Patrick and I talk about Code Quality, Code Complexity (CyclomaticComplexity) and other things around this subject.

After watching the video, please tell me and Patrick what you thought about the video and tell me what your tips are to find complexity in your code or projects!

P.S. Today is the last day to get a 35%
discount on C# Smorgasbord!

Vote on HN

Friday with Filip – Joining new projects

Posted by Filip Ekberg on October 5 2012 1 Comment

Welcome to this week’s Friday with Filip!

The last three Fridays we’ve looked at how to become more productive, how to increase security in your web applications and how to adapt to a real testing strategy. This week I want to talk about how I get deep into the new projects that I join, fast. The projects that I’m referring to are those that have been developed on before. The customer never really wants to pay for you to spend too much time learning the code base and the actual project, so you’ll have to use tools that will help you do so fast.

What I do when I first get the project on my table

The first thing that I obviously try to do, is get the project running or get all the tests to give me green lights. This assumes of course that there are tests written for the system! Just imagine that you need to join a project that has been developed for hundreds or thousands of hours and there’s no tests and no documentation; this is much more common than you might think. That’s why the following quote is what everyone should be feeling when writing code.

Always code as if the person who ends up maintaining your code is a violent psychopath who knows where you live.

As soon as I’ve gotten the project running, or the tests are all OK. I can move on to understanding the code base. Normally when you get a project on your hands, there’s something that is needed to be done, let’s say that you need to implement a feature.

The first thing that I try to do, is get an overview of the complexity and what is depending on what. To do this, I’m using NDepend (which I’ve written about in C# Smorgasbord). NDepend will give you great reports and help you find complexity in many forms. Here’s an overview that shows what is connected to what and the size of the boxes indicate the complexity:

When I find something that I think is too complex, I try to identify if there’s a test for that by analyzing the code coverage. If the code coverage is bad, I try to start writing some tests, this will help me get custom to the project. I don’t worry too much if the tests fail; this means that I need to debug the tests which will force me to step into the complex parts of the system and analyze them further.

To get a great overview of my testing, I use the built in (not in VS2010 Express versions, but in VS2012!) MS Test tooling.

Break & Fix the code

When I’ve come as far as starting to write tests for complex parts that are already in the system; I try to refactor small parts, break stuff to understand the business logic. This all helps me get a great idea of what the particular parts do.

In order to help me get an idea of what is covered by tests and not when browsing around the code, I use NCrunch. NCrunch is great, I configure it to add black dots everywhere to indicate statements that are not covered by tests. Then when they are tested, the dots go green and this is updated in real time!

At this time I can normally start looking at the actual task that I need to perform. Depending on how large the project is and how complex it is, the above process might be short or long. But at least for me, it’s a great way to get my head in a new project.

tl;dr

So you’ve got a new project that you need to implement features in, here’s what I recommend:

  • Analyze the complexity of the code with tools such as NDepend
  • Write tests for un-covered code to understand the flow of the application
  • Use NCrunch to annoy yourself with black dots so that you’ll write more tests and get a better knowledge of the code base
  • Break the code and fix it again — then refactor!

What are you doing to spend less time understanding a system and more time actually fixing the problems?

Vote on HN

Creating a Direct2D game for WinRT

Posted by Filip Ekberg on October 1 2012 7 Comments

If you want to write a game for Windows 8 and was thinking of using XNA, think again. When creating games for Windows 8 you’ll have to go back to using DirectX. But don’t worry, with Visual Studio 2012 on Windows 8, you’ll get a lot of help doing so. Let’s have a look on how to create a Direct2D game for WinRT!

The first thing we would have to do is to fire up Visual Studio and create a new Direct2D project. Can’t find it in the C# section for Windows Store applications? That’s because we’ll have to use C++ for this! You’ll find the project template in Other Languages -> Visual C++ -> Windows Store -> Direct2D App (XAML):

When the project is created, we will have a lot of sample files in our solution. As you can see, there are also XAML files in the project! We’ve got the App.xaml that will tell us which the default view is, which in this case will be DirectXPage.xaml. Let’s run it in a simulated mode and see what this default code gives us. With Visual Studio 2012 we get a very handy simulator, to run the application inside it, simply change the target from Local Machine to Simulator:

This will bring up the simulator and the application that we got by default will let us drag around the title and if we right click the surface, we get an app bar that will allow us to change the background:

Here’s a small video on YouTube showing what this looks like when it’s running:

What’s interesting about this demo is that it combines XAML, C++ and Direct2D. The text in the center that says “Hello, XAML!” is actually just a normal XAML TextBox:

<TextBlock x:Name="SimpleTextBlock" HorizontalAlignment="Center" FontSize="42" Height="72" Text="Hello, XAML!" Margin="0,0,0,50"/>

The TextBox lives inside a content panel called SwapChainBackgroundPanel and this is something new. According to MSDN, this panel will only be used when we interop with DirectX:

Implements a XAML layout surface target for Microsoft DirectX interoperation scenarios. This panel has some atypical restrictions on its usage within an app window; see Remarks.

MSDN also says that this only applies to Windows Store applications.

We’re getting a lot of DirectX code free here which we don’t have to write ourselves. What we do want to take a look at though is the SimpleTextRenderer. This class handles drawing the text “Hello, DirectX!” on the screen. It inherits from the DirectXBase class which will do all the DirectX magic.

Have a look in the method SimpleTextRenderer::Render(), if we were to add a rectangle instead of the text, like this:

auto rect = D2D1_RECT_F();
rect.bottom = 100.0f;
rect.top = 0.0f;
rect.left = 0.0f;
rect.right = 100.0f;

m_d2dContext->DrawRectangle(rect, m_blackBrush.Get());

We could run the application and it would look like this:

Now, try to move the rectangle around, notice that it moves around just like the text we had before did? That’s magical! Actually, it’s just a transformation going on; the Direct2D context is transformed and the area is moved to wherever the mouse was. These are the lines in that method which changes the transformation:

Matrix3x2F translation = Matrix3x2F::Translation(
    m_windowBounds.Width / 2.0f - m_textMetrics.widthIncludingTrailingWhitespace / 2.0f + m_textPosition.X,
    m_windowBounds.Height / 2.0f - m_textMetrics.height / 2.0f + m_textPosition.Y
    );
m_d2dContext->SetTransform(translation * m_orientationTransform2D);

If we simply comment these out, we’ll see a completely different result. You should now see the rectangle at the top of the screen being 100 pixels wide and 100 pixels high:

Now to make this a bit more interesting, let’s make the box move around a little bit. In order for it not to rely on the point & move event, we need to go back to the DirectXPage and remove something from the event handler DirectXPage::OnRendering. As it is now, it checks if it needs to be rendered by falling back on a boolean variable, but this bool is only set to true when we’ve moved the mouse (or finger) around. That means that the event handler should look like this instead:

void DirectXPage::OnRendering(Object^ sender, Object^ args)
{
    m_timer->Update();
    m_renderer->Update(m_timer->Total, m_timer->Delta);
    m_renderer->Render();
    m_renderer->Present();
    m_renderNeeded = false;
}

In order for the box to actually move around, we need to change a couple of things in SimpleTextRenderer::Render as well as in SimpleTextRenderer::Update. First we need to introduce a couple of global variables, I’ve called them x and y. These two variables will be in charge of telling us where the rectangle should be drawn on the screen. This means that SimpleTextRenderer::Render can look like this:

float x = 0.0f;
float y = 0.0f;
void SimpleTextRenderer::Render()
{
    m_d2dContext->BeginDraw();

    m_d2dContext->Clear(ColorF(BackgroundColors[m_backgroundColorIndex]));

    auto rect = D2D1_RECT_F();
    rect.top = y;
    rect.left = x;
   
    rect.bottom = rect.top + 100.0f;
    rect.right = rect.left + 100.0f;

    m_d2dContext->DrawRectangle(rect, m_blackBrush.Get());

    HRESULT hr = m_d2dContext->EndDraw();
    if (hr != D2DERR_RECREATE_TARGET)
    {
        DX::ThrowIfFailed(hr);
    }

    m_renderNeeded = false;
}

There’s a second pair of global variables that I want to add at this point, these will tell us in which direction the rectangle should be moving. As you might have figured out, I want to make the rectangle bounce around the window!

float x_direction = 1.0f;
float y_direction = 1.0f;

Last but not least, I want to make some changes to SimpleTextRenderer::Update, since this is where we get information about the delta time, the time since the last frame was updated. Basically I want to increase both x and y with timeDelta * 200 and then multiply that with the current direction of which we’re moving the rectangle.

When bouncing something around, we need to know the bounds of the window so that the rectangle does not exit outside the window. Luckily for us, the class that we’re inheriting from has a member variable called m_windowBounds which is a rectangle that tells us the current window bounds.

We can use this rectangle to check if we’re outside of the bounds and then we also need to check that we don’t reach below 0 in either x or y direction. This leaves us with a method looking like this:

float x_direction = 1.0f;
float y_direction = 1.0f;
void SimpleTextRenderer::Update(float timeTotal, float timeDelta)
{
    x += (timeDelta * 200) * x_direction;
    y += (timeDelta * 200) * y_direction;

    if(x > m_windowBounds.Width - 100.0f || x <= 0) {
        y_direction *= 1.0f;
        x_direction *= -1.0f;
    }
    if(y > m_windowBounds.Height - 100.0f || y <= 0) {
        y_direction *= -1.0f;
        x_direction *= 1.0f;
    }
}

Side note: The global variables need to be defined before they’re used, add the floats to the top of the file

Here’s a video of what this looks like when the application is running inside the Windows 8 Simulator:

Combining XAML with C++ and DirectX will let us create very nice applications and games. Go ahead and great your cool game today and play around with the DirectX samples that come with Visual Studio 2012!

Vote on HN