Tag Archives: Programming

The optimizer – Google’s Jeff Dean

Really interesting article on the difference a ten-times-better programmer can make, plus some bogus Jeff Dean facts:

  • Compilers don’t warn Jeff Dean. Jeff Dean warns compilers.
  • Jeff Dean writes directly in binary. He then writes the source
    code as documentation for other developers.
  • When Jeff Dean has an ergonomic evaluation, it is for the protection of his keyboard.
  • Jeff Dean was forced to invent asynchronous APIs one day when he optimized a function so that it returned before it was invoked.

Leave a comment

Filed under Programming, Technology

Dr Dobbs article on software development for Obama’s campaign

Dr Dobbs wrote this article on the ways developing for an election campaign differ from typical projects.

“We realized early on that we had to understand that ‘good enough’ was indeed good enough. Often, 90% of the way there was the same as 100% and we just didn’t have the bandwidth to put a lot of time into the final 10% or polishing every last detail.”

The client wasn’t used to iterative development where they saw early incomplete releases of features.

The difference turned out to be crucial, as many features were changed in ways that became critical to their smooth operation on election day.

After the feature freeze in October,

No new features were added, unless mandated by regulation or indispensable to the operation. And in some cases, when an app didn’t work as hoped, team members reassessed its criticality and simply dumped the app if it was no longer truly essential.

At least they won’t have to do it all again for the next election, right? Wrong.

The team was unequivocal in its conviction that by 2016, all the code would have to be written from scratch again, due to change in technology and how people communicate. They saw no possibility of reusing their code. In fact, they felt that part of their opponents’ software difficulty was tied to reworking code from 2008, rather than writing the apps from scratch.

Leave a comment

Filed under Programming

Star programmer outsourced his own job!

TheRegister reports that an audit discovered a firm’s star programmer was outsourcing his own work to China!

A security audit of a US critical infrastructure company last year revealed that its star developer had outsourced his own job to a Chinese subcontractor and was spending all his work time playing around on the internet.

The ruse was obviously paying off for him handsomely:

Further investigation found that the enterprising Bob had actually taken jobs with other firms and had outsourced that work too, netting him hundreds of thousands of dollars in profit as well as lots of time to hang around on internet messaging boards.

Leave a comment

Filed under Programming

Personal Projects

Thought-provoking essay on the hidden value of personal projects:

All of these properties of personal projects suggest they’re a really good predictor of talent that is worth hiring. Though this approach may suffer from the occasional false negative (even the greats now and again bang out quick-and-dirty personal projects don’t necessarily impress), a false positive probably never does: you’ll probably never find someone with a great personal project who also turns out to be rubbish.

Leave a comment

Filed under Programming

Delegating Constructors in C++11

Here’s some code that tries out C++ 11 delegating constructors.  These were announced as part of Visual Studio in the November CTP and avoid the need to refactor common code out of constructors into an “init” function.  There are examples of their use in Stephan Levavej’s excellent video series too.

class Request
{
public:
  enum class Priority{ High, Medium, Low };
  static const Priority defaultPriority(){ return Priority::Low; }

  // Constructor with all parameters fully specified
  Request( Priority priority, const std::string& requestId ) :
      m_priority( priority ),
      m_requestId( requestId )
  {
    std::cout 
    << "Request( Priority priority, const std::string& requestId ) called: "
    << "RequestId " << m_requestId << ", "
    << "Priority " << toString( m_priority ) << "\n";
  }

  // Delegate to fully specified constructor above
  Request( const std::string& requestId ) : 
    Request( defaultPriority(), requestId )
  {
    std::cout 
      << "Request( const std::string& requestId ) called: "
      << "RequestId " << requestId << "\n";
  }

  // Contrived example to demonstrate chaining delegating constructors
  Request( const std::vector<int>& requestId ) : 
    Request( toString( requestId ) )
  {
    std::cout << "Request( const std::vector<int>& requestId ) called\n";
  }

  // Contrived example to show that ~Request() is called if the delegating constructor completed
  Request( Priority priority ) : 
    Request( priority, "Empty" )
  {
    throw std::exception( "This constructor is deprecated, RequestId field is now mandatory." );
  }
  ~Request()
  {
    std::cout 
      << "~Request() called:"
      << "RequestId " << m_requestId << ", "
      << "Priority " << toString( m_priority ) << "\n";
  }
private:
  std::string toString( const std::vector<int>& requestId )
  {
    std::string tmpId;
    for (auto elem : requestId)
    {
      if ( elem < 0 || 9 < elem )
        throw std::exception("Invalid request id, should be digits");
      tmpId.push_back(static_cast<char>(elem + '0'));
    }
    return tmpId;
 }

 std::string toString( Priority p )
 {
   if (p == Priority::Low ) return "Low";
   else if ( p == Priority::Medium ) return "Medium";
   else if ( p == Priority::High ) return "High";
   else
     throw std::exception( "Unexpected Priority" );
 }
 Priority m_priority;
 const std::string m_requestId;
};

And here’s a program that exercises the Request class (this uses musingstudio::initialize to conveniently initialize a standard container):

int _tmain(int argc, _TCHAR* argv[])
{
  std::cout << "\nCall fully specified constructor\n";
  Request fullySpecified( Request::Priority::High, "HeartTransplant-749553" );

  std::cout << "\nCall a delegating constructor\n";
  Request defaultPriority( "BookDelivery-5542" );

  std::cout << "\nCall chained delegating constructors\n";
  Request legacy( musingstudio::initialize<std::vector<int>>( { 1, 6 } ) );

  std::cout << "\nThis should throw without executing the destructor...\n";
  try
  {
    // This should throw due to -ve input, but does NOT execute ~Request()
    // because no constructor call completed
    Request invalid( musingstudio::initialize<std::vector<int>>( {-1} ) );
  }
  catch( const std::exception& exc )
  {
    std::cout << "ERROR: " << exc.what() << std::endl;
  }

  std::cout << "\nThis should throw and execute the destructor...\n";
  try
  {
    // This will throw because no requestId is not specified
    // (contrived example to show that ~Request() is executed
    // because the delegating constructor succeeded).
    Request noRequestId( Request::Priority::Low );
  }
  catch( const std::exception& exc )
  {
    std::cout << "ERROR: " << exc.what() << std::endl;
  }

  std::cout << "\nRemaining destructors...\n";

  return 0;
}

DelegatingConstructorsOutput
As with other Nov CTP features, Visual Studio intellisense hasn’t caught up yet, so expect to see red squiggly lines all over the code if you try this out.

What’s interesting is that it’s now possible for ~Request() to be called if a constructor fails to complete, as long as the delegatee (inner) constructor does complete.

Leave a comment

Filed under C++ Code

Money and Speed: Inside the Black Box

HFT Review publicized this excellent video by Marije Meerman on the flash crash:

In her latest film ‘Money and Speed: Inside the Black Box’ she continues this format, talking of High Frequency Trading and the ‘Flash Crash’ of 6th May 2010 through the eyes of the regulators and market participants.

Paul Wilmott is one of the contributors.

Leave a comment

Filed under Finance, Video

Experimental code for simulating Reflection in C++

Motivation for looking at Reflection in C++

At work, we have two frameworks for developing new components – one in C++ and the other in F#. The F# framework is newer and benefits from the insight of previous years working with the C++ framework. In particular, the new F# framework only requires developers to implement a reduced, strongly typed interface in F# by defining a few types (e.g. records) and associated functions between them. This reduced interface is inflated into a full model by the framework, making heavy use of .NET Reflection.

The F# framework has delivered productivity improvements (development time down to a third compared to the previous C++ framework). But the philosophical question remains – how much of that is due to the new architecture developed with the benefit of hindsight? And could we replicate that architecture in C++? The main functionality gap comes down to this: in F# you can use .NET Reflection to discover the names and types of fields in F# types such as unions, records and options, but in C++ you can’t.

Requirements

A full implementation of Reflection for C++ would include ability to discover type information, field names, properties and methods, as well as being able to create instances of types and invoke methods.  I’m interested in a small subset of that scope – the ability to discover the names and values of fields in a C++ struct.

Solution

#include "stdafx.h"

#include <iostream>
#include <string>

#include <boost\preprocessor.hpp>
#include <boost\preprocessor\variadic\size.hpp>
#include <boost\type_traits.hpp>

#include <boost\mpl\range_c.hpp>
#include <boost\mpl\for_each.hpp>

#define REMOVE_BRACKETS(...) __VA_ARGS__
#define REMOVE_NEXT(x)
#define STRIP_TYPE(x) REMOVE_NEXT x
#define DECLARE_DATA_MEMBER(x) REMOVE_BRACKETS x
#define TYPE_ONLY(x) x REMOVE_NEXT(

#define REFLECTABLE(...) \
  static const int number_of_fields = BOOST_PP_VARIADIC_SIZE(__VA_ARGS__); \
  friend struct Reflector; \
  \
  template<int N, class Parent = void> \
  struct FieldData {}; \
  \
  BOOST_PP_SEQ_FOR_EACH_I(REFLECT_EACH, data, BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))

#define REFLECT_EACH(r, data, i, x) \
  DECLARE_DATA_MEMBER(x); \
  template<class Parent> \
  struct FieldData<i, Parent> \
  { \
    Parent & parent; \
    FieldData(Parent& p) : parent(p) {} \
    \
    TYPE_ONLY x ) & get() const \
    { \
      return parent.STRIP_TYPE(x); \
    }\
    const char * name() const \
    {\
      return BOOST_PP_STRINGIZE(STRIP_TYPE(x)); \
    } \
  }; \

struct Reflector
{
  // Get a FieldData instance for the N'th field in type T
  template<int N, class T>
  static typename T::template FieldData<N, T> getFieldData(T& x)
  {
    return typename T::template FieldData<N, T>(x);
  }

  // Reflector is a friend of T, so has access to the private member T::number_of_fields
  template<class T>
  struct FieldCounter
  {
    static const int count = T::number_of_fields;
  };
};

// FieldDispatcher - calls Visitor::visit for each field in T
template<class T, class Visitor>
struct FieldDispatcher
{
  FieldDispatcher( T& t, Visitor visitor ) :
  t(t),
  visitor(visitor)
  {
  }

  template<class FieldIterator>
  void operator()(FieldIterator)
  {
    auto field = Reflector::getFieldData<FieldIterator::value>(t);
    visitor.visit( field );
  }

  T& t;
  Visitor visitor;
};

template<class T, class Visitor>
void for_each_field(T& t, Visitor visitor)
{
  typedef boost::mpl::range_c<int, 0, Reflector::FieldCounter<T>::count> FieldList;
  FieldDispatcher<T, Visitor> field_dispatcher( t, visitor );

  // For each field in T, dispatch the visitor to that field
  boost::mpl::for_each<FieldList>( field_dispatcher );
}

struct PrintNameValueVisitor
{
  template<class FieldData>
  void visit( FieldData field )
  {
    std::cout << field.name() << "=" << field.get() << std::endl;
  }
};

struct Person
{
  Person(const char *first_name, int age, const char* street, const char* town) :
    first_name(first_name),
    age(age),
    street_name(street),
    town(town)
    {
    }

  REFLECTABLE
  (
    (const char *) first_name,
    (int) age,
    (std::string) street_name,
    (std::string) town
  )
};

int _tmain(int argc, _TCHAR* argv[])
{
  Person p("John", 31, "Electric Avenue", "Harrogate" );
  for_each_field(p, PrintNameValueVisitor());

  return 0;
}

Output

Output

Conclusions

The code above ‘works’ – it satisfies the requirements by providing a way to decorate a struct with metadata that can be used to return the names and values of each field. However, in Visual Studio 2010 and 2012, it produces compiler warnings (due to the macro hackery in TYPE_ONLY) and confuses intellisense (which doesn’t cope with the REFLECTABLE macro). In practical terms, that makes it unsuitable, because the productivity benefits are lost when intellisense and auto-complete stop working.

2 Comments

Filed under C++ Code