Category Archives: Programming

Dijkstra on System Design

This paper by Dijkstra was linked from a discussion on ArsTechnica about system design. It’s amazing that many of his high-level points remain true today, despite being published in 1968 – I’ve picked out a few below:

Focus:

Production speed is severely degraded if one works with half time people, who have other obligations as well. This is as least a factor of four, probably it is worse. The people themselves lose time and energy in switching over, the group as a whole loses decision speed as discussions, when needed, have often to be postponed until all people concerned are available.

Star developers:

The intellectual level needed for system design is in general grossly underestimated. I am more than ever convinced that this type of work is just difficult and that every effort to do it with other than the best people is doomed to either failure or moderate success at enormous expenses.

Unit testing:

It seems the designer’s responsibility to construct his mechanism in such a way —i.e. so highly structured— that at each stage of the testing procedure the number of relevant test cases is so small that he can try them all and that what is being tested is so perspicuous that it is clear that he has not overlooked a situation.

I’d add to the list that it’s vital to spread knowledge within a team to maintain productivity and avoid one person being silo’d to a specialist skill and becoming a bottle neck. Instead, find out how to pull the source code out of the repository, how to build and test it – and start making small changes yourself as a learning exercise.

Leave a comment

Filed under Programming, Soft skills

The importance of layered architectures

ArsTechnica hosts a number of discussions on software development, and arguably none more important than layering.

Understanding one thing at a time is easier than understanding two things that only make sense when used together … Rules of thumb such as “No cyclical dependencies” or “Dependencies must only reach down one level” simply capture the practically achievable limit of the fundamental idea that one thing at a time is easier to understand than two things.

Whilst interfaces and abstractions buy you immunity to change for a given class/component, a layered architecture buys you the flexibility to swap out an entire layer of technology. That’s something that can happen surprisingly frequently in the corporate world – need to replace MFC with Silverlight then with WFP then with HTML5? No problem if your architecture is layered such that none of the business models need to be re-released for the change. Similarly if a game-changing Hadoop solution is rolled out for your data storage – a well layered architecture can remain impervious. But mix up all that logic so that models source data and user interfaces call into low-level analytics – you’ve got a migration nightmare on your hands.

Leave a comment

Filed under Programming

Going Native 2013 – Don’t Help the Compiler, Stephan Lavavej

This presentation from Stephan Lavavej could have been entitled “Compiler Knows Best” – he rockets through numerous examples where the advice is to write less code and let the compiler do the right thing.

Here’s a slide of one of the key messages:

20140207-183930.jpg

Other highlights for me:

  • decltype(auto) coming in C++14 looks very cool (eliminates duplicate code when using auto with decltype for late-specified return values)
  • Don’t call std::make_pair( x, y ) – the T, U are inferred by template argument deduction, so should always be left out (unless you want to force a type conversion when T != typeof(x) e.g. short -> long
  • Use nullptr because it is convertable to all pointer types, but 0 and NULL are always type int

I love the new features of C++11 and couldn’t have written some of the cool libraries I’m working on without them. But – presentations like this make me concerned for the complexity of the language and the steep learning curve that even experienced C++ hires will have to climb.

Leave a comment

Filed under C++, Programming, Video

Why do std::shared_ptr and std::unique_ptr have different template parameters?

Interesting question on StackOverflow – Why does unique_ptr take two template parameters when shared_ptr only takes one?

I haven’t used std::unique_ptr much and wasn’t aware that you could supply a custom deletor.

If you provide the deleter as template argument (as in unique_ptr) it is part of the type and you don’t need to store anything additional in the objects of this type. If deleter is passed as constructor’s argument (as in shared_ptr) you need to store it in the object. This is the cost of additional flexibility, since you can use different deleters for the objects of the same type

Since unique_ptr is lightweight and doesn’t have the overhead of a control block, it uses a template parameter for the deletor. std::shared_ptr takes the other choice since it already has a control block and gains the extra flexibility.

Leave a comment

Filed under C++, Programming

Going Native 2013 – Effective C++11/14, Scott Meyers

Scott Meyers presented three guidelines from his upcoming new edition of Effective C++:

  • Understand std::move and std::forward
  • Declare functions noexcept whenever possible
  • Make std::threads unjoinable on all paths

The advice on std::move and std::forward echoes thoughts from Scott’s earlier presentation on Universal References. This time, he also talks about efficiency and points out that some standard library methods (such as std::vector::push_back) won’t move unless it can do so using noexcept methods. This is the motivation for the second guideline to use noexcept whenever possible on these key methods (move operators and swap).

The guideline on std::threads harks back to the ongoing discussions in the standards committee – when a joinable (“running”) thread is destroyed, should it block/detach/terminate. Scott advises to use RAII to force the behaviour that you want, and presents a simple class for that purpose.

Leave a comment

Filed under C++, Programming, Video

Herb Sutter GotW95: Thread Safety and Synchronisation

Excellent article on thread safety and synchronisation from Herb Sutter.

A race condition occurs when two threads access the same shared variable concurrently, and at least one is a non-const operation (writer). Concurrent const operations are valid, and do not race with each other.

Guideline: A type should only be fully internally synchronized if and only if its purpose is to provide inter-thread communication (e.g., a message queue) or synchronization (e.g., a mutex).

Leave a comment

Filed under C++, Programming

How to Integrate GTest into Visual Studio 2010

I’m using GTest for C++ unit tests and have found a neat way to integrate this into Visual Studio 2010. It’s very low-tech, but works pretty well – see http://stackoverflow.com/questions/6414788/how-can-i-add-a-custom-command-to-visual-studio

  • Go to Tools | External Tools… to add the command line running your GTest executable
  • In the dialog, browse to your command, add the command line arguments, and be sure to select “Use Output window”
  • Count up the external tools to identify the index of your new one (starting at one, not zero)
  • Create/customize a toolbar – click “Add Command” and pick your new tool under category Tools, External Commands 1,2,3 … n
  • Edit the name using “Modify Selection”

That’s it! Now, your command runs inside Visual Studio and writes into the output window. Even better, if there’s a test failure that includes the source location, you can double-click on it to go to that line in the editor.

1 Comment

Filed under C++, Programming

VC++ 2013 Class Layout Change and Wasted Space

Bruce Dawson posted about class layout incompatibilites in Visual C++ 2013.

Leave a comment

Filed under C++, Programming

Optimizer bug in Visual Studio

Bruce Dawson posted about an optimizer weakness in VC++.

Using macros to implement Min and Max has numerous well known pitfalls that can be avoided by using template functions. However the definitions of min and max that are mandated by the C++ standard cause VC++ to generate inefficient code. Until the VC++ compiler fixes this it may be best to use custom Min/Max template functions to avoid this problem.

Leave a comment

Filed under C++, Programming

Going Native 2013 – ‘new’ STL algorithms

I’ve watched a few of the Going Native 2013 videos now and, whilst I was impressed by Herb Sutter’s games written in Cinder, I particularly like the slide and gather algorithms demonstrated by Sean Parent in his C++ seasoning presentation:

template<typename Iter>
auto slide(Iter begin, Iter end, Iter target) -> std::pair<Iter,Iter>
{
    if (target < begin) return std::make_pair( target, std::rotate(target, begin, end) );
    if (end < target) return std::make_pair( std::rotate( begin, end, target ), target );
    return std::make_pair( begin, end );
}

template<typename Iter, typename Pred>
auto gather(Iter begin, Iter end, Iter target, Pred predicate) -> std::pair<Iter,Iter>
{
    auto start = std::stable_partition( begin, target, std::not1(predicate) );
    auto finish = std::stable_partition( target, end, predicate );
    return std::make_pair(start, finish);
}

Whilst I haven’t used std::rotate or std::stable_partition, I’m sure to use these two algorithms built on top of them, probably because their behaviour is so much easier to describe.

Leave a comment

Filed under C++, C++ Code, Programming, Video