Sunday, 3 January 2016

Engineering without Fear



I am a believer that there is a lot to gain from viewing software development as an engineering discipline.
We can undoubtedly apply principles such as SOLID to contrive and devise systems from our raw building material of code.
But having worked in other forms of engineering I'm also aware that there are differences between the processes and patterns we apply in software engineering and our brethren practicing in other industries.
Far from these differences detracting from the validity of software development as engineering we should embrace these differences, indeed they are the reason I decided to make the switch and become a coder.
Price of Mistakes
No-body wants there software to have defects and we put a lot of effort into the prevention and eradication of them from our code.
But actually compared to other forms of engineering we pay a relatively low price for these mistakes at least in the development phase if not in production. 
Some defects can be fixed in minutes, more persistent ones might take a day, but imagine the life of an electronics engineer where defects might take weeks to fix as PCBs have to be re-laid out, fabricated and populated with components.
In some forms of engineering defects simply cannot be tolerated at all, a structural engineer will be in much more trouble if a mistake causes a bridge to collapse then we might be in if an app crashes.
Our relatively rapid fix and deploy cycle mean we have a much greater capacity to improve our output and drive increases in quality, this coupled with out ability to update software for our users means we are in a unique position to improve things for them on a continual basis.
Speed of Production
We all spend a significant amount of time thinking about and building code, if we work on large code bases it may take several minutes for us to compile and link all our code into an app or web site.
In most other forms of engineering it isn't possible for an engineer to see the fruits of his or hers labour so quickly.
The pace at which we can build and produce software is actually comparably high, when a decent coder gets into a groove significant progress can be shown over the course of a day or two and not just plans or simulations but actual working code.
Creative Thinking
Software development is a very creative exercise.
We allow users to directly interact with what we produce in the form of a UI, whilst also getting to shape this contact via careful application of UX.
This allows us to not just be cranking the handle and churning out code but indulge our more artistic side and build something captivating and engaging.
This very direct contact with our users is not something all engineers have the pleasure of experiencing.
Let Go of Your Fear
So reading this post you may be getting the impression that as software engineers we have any easy life, obviously this isn't true.
Writing software can be a very frustrating as well as rewarding experience, it requires intelligence and a dedication in applying your talent.
What we should take away from these differences is that as software engineers we shouldn't be afraid to try things.
Software development on both a personal and professional level is elevated and progressed by people having new ideas, the speed at which we are able to try these ideas out and put them into practice gives a unique opportunity to advance our industry.
Not every idea you have will be a winner, some may even turn out to be dumb but this should never stop you from trying.
The worst that might happen is you've spent a day or so finding out that this isn't quite going to work as you hoped, but knowing what doesn't work can be just as informative as knowing what does work.
But when you do have a winning idea, that time spent implementing it will not only improve both you and your team as engineers but also your users lives that benefit from your engineering skill and your willingness to try something new. 

Sunday, 20 December 2015

Agile Architecture



In many agile teams the role of architecture is derided and on occasion actively avoided.
I think this view point comes from mistakenly equating architecture with planning.
As agile teams we don't won't to get bogged down in planning and we don't require a large upfront design to get started in delivering value.
And thats fine because this isn't the role of architecture.
Architecture isn't about exactly how we might implement a possible new feature six months down the line, it should be about how we make sure our code base is built to accept change and adapt so that if that feature does come further down the line we haven't painted ourselves into a corner.
Agile teams will often describe their architecture as emergent, I think this can be dangerous if you expect a robust architecture to just come riding into view when you've paid it no thought, instead I believe we should be aiming for an emergent codebase that can go anywhere we want to take it.
Two Big Questions
I don't believe its anti-agile to take a small amount of time just thinking through the structure of the code were about to write, this thinking should be aiming to answer two questions,
How will we test this?
How would we change this?
Agile architecture should be about being able to scale, we need to be able to adapt to new requirements quickly and we need to be able to verify our code still works.
This means the two most important properties our code can have is testability and adaptability, being able to fall back on these two things means you will never be fearful of a new requirement. 
Failing to think about these two things means the architecture that emerges will be brittle and ultimately will fall apart.
Look After the Small Things
Although having this scaleability in our code base is a long term aim fortunately we can ensure we get there by taking care of the small things in the here and now.
SOLID is all about taking care of testability and adaptability, ensuring you write your code being guided by these principles whilst taking a small amount of time to think through how you want to structure things will allow for the emergence that were seeking.
Always Ready to Go
Once we have the emergent code base that will allow us to grow another important aspect of our architecture needs to be the ability to deliver at a moments notice.
For this to be a reality we need to embrace automation, in building, in testing, and deploying, the architecture of our build processes is just as important as the architecture of our code.
If we prefer working software over documentation then we need to ensure that as little energy as possible is expended in getting that working software though the door.
Shaping the Future
Software provides solutions to problems, the genesis for wanting to reduce the role of architecture within an agile team comes from our propensity to solve problems that don't exist yet or becoming too obsessed with minor details.
The role of architecture should be to create a framework to allow creativity, to enable solutions to be found and implemented when the need arises.
Good architecture stops us creating a straight jacket that suffocates us from being able to move forward.
Without this flexibility it is impossible to be agile, without a bias towards automation it is impossible to be agile.
The concept of MVP should be applied as much to architecture as it is to the product the architecture needs to deliver.
We need to define the simplest architecture that can possibly work and continue to work in the future when we change exactly what working means.   

Sunday, 6 December 2015

Test Driven Benefits



I've written many times about the virtues of unit testing, first and foremost because its the best way to prove that code works as expected.
But as with many aspects of good development practices if you do the right thing you'll profit from many additional benefits.
This is also true with unit testing, its the right thing to do, and as well as verifying your code is fit for purpose it can be the mechanism for other good things to happen.
Consumers First
Code is written to be consumed by others, and the very first consumer is your unit tests. Its your chance to both document how your API is intended to be used as well as place yourself in the position of being a user of your code rather than the producer.
Unit tests can be used a design tool, as your writing your tests always be assessing does this make sense, pay attention to things like how much set-up and tear down is needed, does this represent state that will not be obvious to someone trying to utilise this class?
Unit tests are very good documentation, they not only demonstrate the code being used they also state expectations on the data that should be fed in and the data that will be fed out. 
Shrinking the Circle
Every developer has been on the treadmill of making a change to some code, deploying that code, interacting with that application, finding that bug is still their and back round to the start.
This is dull and inefficient, it might be several minutes before any modification can be verified and on a tricky pest of a defect might be a cycle that is repeated all day.
Unit tests are a very good debug tool, a bug in a class that manifests itself in your application should also be visible in your unit tests, if its not you need to write more tests.
Using your unit tests to find a problem will give you much quicker feedback on the impact your changes are making, if the tests are well written it also helps isolate the code that may be causing the issue, your able to ask more precise questions, does this class do the right thing in this situation?  
The What-Ifs
The infrastructure in your unit tests that ensures your mocking dependencies and decoupling your classes reveals your architecture, whether good or bad.
There is a lot of knowledge to be gained by looking at what tests break if you make a change, if we make this change a lot of tests in many different areas of the code base break, why is that and does that indicate we need to make a change?
It also allows you to speak with confidence if your asked a what-if question, we're thinking of adding this feature or making this change, the scale of that work will often be linked to level of decimation it will cause in your unit tests.
Following principles like single responsibility and open-close should ensure that changes don't cause carnage in your unit tests, you'll always be adding more unit tests but are you constantly having to re-write the ones you already have?
Hopefully everyone is realising the benefits of automated testing, and if the only benefit your drawing from it is the automatic verification that your code still works then its been worth it but be open to the fact that there is a long list of other benefits to be plundered.
This is just one example that if you do the right things good code will happen and you'll expunge less energy and brain power in producing it. 

Sunday, 29 November 2015

Just A Pile of Bricks



While agile has taught us the lesson that spending large amounts of time on big up-front architecture is not necessarily productive this shouldn't discount the need for our code to have a recognisable architecture.
Architecture is what separates code from software, what separates development from engineering, and ultimately what enables continual improvement and scaleability.
So we have a fine balance to make between inefficiency caused by spending too long thinking and not doing and rushing in and making a mess.
So with this in mind how do we recognise when our code lacks an architecture? How do we know when we have a pile of bricks and not a house?
Avoidance of Thingies
The first step towards understanding what a class does should be its name, when a class is well-named looking through the source for that class should re-affirm what you assumed were going to see not shock.
The name should also imply that classes role within the overall architecture, allow certain assumptions to be made on how it is used and infer a commonality with other classes with similar names.
In code bases lacking an architecture this often isn't the case, in these code bases we have classes that share a suffix like Manager, Controller and Service that have very little in common.
Their public API's don't look the same and learning how to utilise one of them is a learning experience every time.
In a similar manner when you have to look under the hood you don't see much in common between them.
If were going to create well defined layers in our code classes that identify themselves as being part of that layer need to have commonality in approach and interface otherwise we end up with classes that may as well be called thingies.
Without this commonality we will end up with each class operating at different levels of abstraction and this imprecision will be passed out to all the classes that utilise them.
As a code base grows our understanding of how it fits together should grow not diminish, this is a Service I've used one of those before so I know what to expect, or I assume there is some kind of controller to deal with that.    
I'd Need to Look into That
A good architecture creates a narrative, you don't need to know the details of all the implementation to have an appreciation of how the system works.
This means when your asked questions like "would we able to do this?" or "what would be involved in adding this?" your able to understand and evaluate the work required and give feedback. That is not to say adding the feature is quick or easy but you understand what would need to be done.
When your working with a code base that doesn't have a clear architecture questions like this are difficult to answer and usually provoke the answer "I'd need to look at the code".
No assumptions can be made and every trawl through the source is an adventure leading to new discoveries about how things work.
When work starts on a new feature there often isn't any obvious starting point, theirs no pattern to follow or structure to fit into, everything requires an upfront brain storming session to figure stuff out.
Instead we want to be in the situation where most of the time (you can never account for everything) when were asked for something new we could list the classes or modifications that would be required because we know how things are structured even if we don't know the detail. 
Mud Pie Not A Cake
A by-product of having a clear architecture is the possibility of re-use, either within the same product or when starting a new product.
We can draw vertical lines through the code base and "lift and shift" features to slot into a different code base.
Looking at our code base is like looking at layers of a cake, clearly separated layers each with there own role to play.
When a clear architecture isn't present our cake becomes more of a mud pie, the structure isn't clear and each layer of code flows and bleeds into the other.
The possibility of re-use is greatly reduced, we can't cut vertical slices through our code because code keeps flowing through our fingers.
Ultimately an architecture needs to be flexible both to allow the code base to grow organically but also to bring the benefits of loose coupling for testability and re-use.
But even with this flexibility an architecture does need to be present and it does need to provide structure and bring all the benefits we've talked about.
This isn't an easy feet to accomplish and we'll very often get it wrong, thats why we need to recognise when an architecture is missing or is breaking down. Its easier to reverse a car than a ship, recognise when something is wrong and change it.  

Sunday, 22 November 2015

What Does Anti-Agile Mean?



The majority, if not all, of the people reading this will be working in agile teams. That will mean different things in different organisations but that isn't necessarily a problem.
Agile isn't one size fits all, a drastic over simplification of agile might be "try things, do what works".
But when does something become anti-agile? When does it actually stop us from achieving our goals?
Interrupting the Flow
An important aspect of agile is effective and on-going communication, with the most valuable form of which being face to face.
Something is anti-agile if it gets in the way of people just talking and figuring stuff out.
This can come in many forms, poor division of work and resources causing constant blockages by being reliant on others who are siloed with there own problems.
By having a needless hierarchy creating ineffective lines of communication where the people who can work through problems are communicating via proxies.
An agile mentality assumes that if smart people are given the time and room to work with each other they will produce results, this process cannot be forced or controlled it just requires trust.
Build Something
Some other processes place a large importance on having a plan, its a misconception of agile that it doesn't involve a plan.
Agile does involve a plan, the difference is the scope, we don't plan to take over the world we just plan to make a start towards doing it. We realise that users can't use plans but they can use the things we build.
Anything that gets in the way of building something is anti-agile. This is nearly always caused by a fear of the unknown, how do we know this is the right answer?
The truth we have to swallow is your never going to know in advance that your doing the right thing, you cannot plan to succeed you can only plan to have your best stab at it.
Users tell you if you doing the right thing and they do that based on the product you put into their hands not based on the plan of what you intend to one day give them.
Listen Don't Tell
In a similar vain your best chance of succeeding is when you listen to what people want.
Users will tell you what they want, users aren't always right and sometimes you will need to persuade them that your idea is a good one but you won't be able to do that effectively and you want be able to adapt if you don't understand where your users are coming from.
Developers will tell you how they can best work effectively, they are the ones who have to make your vision a reality, if you don't trust them to know how to do that you've put together the wrong team, you need to succeed because of your team not in spite of them.
Its anti-agile to assume you have all the answers, an agile team will embrace their ignorance and see it as an opportunity to learn, to find out what people want and deliver it.
Flex Don't Snap
The theme running through these points is the need to be flexible, with agile the clue is in the name.
Questions can be hard to define, answers can be wrong, people can make mistakes. You can either spend a long time following a clearly defined plan moving slowly towards failure or you can accept the fact that there will be rough seas that require changes in direction.
Something is anti-agile if it prevents you from implementing change when you know its the right thing to do.
It sounds crazy to think that a team could know they are doing the wrong thing but carry on anyway, but it happens all the time when that team is operating in an environment that isn't agile.
Either the people that know this is wrong aren't communicating, your not finding out from the user if they want what your doing or your machine is so big that it simply cannot change direction.
You can't be told how your team can work in an agile manner, you have to figure that out for yourself, but there are guidelines you can follow.
You'll find your own way you just need to be able to recognise when your heading in an anti-agile direction and hit reverse. 

Tuesday, 17 November 2015

Playing Strong and Loose



There are four important concepts in development, sometimes they get mixed up but its important to have a good understanding of the relationship between them.
"Abstraction leads to loose coupling"
"Encapsulation leads to strong cohesion"
So why exactly are these statements so important are why do we sometimes get them confused?
Abstracting Away the Detail
The purpose of abstraction is to hide detail.
Detail leads to complexity, complexity leads to instability and instability leads to unhappy users.
Whenever we define how pieces of code are going to interact this should be based on an abstraction that represents the functionality that is going to be consumed, it shouldn't be based on details of how that functionality will be implemented.
As a slightly contrived example, if you wanted to save files you wouldn't expect the interface to do so to talk in terms of disk sectors and binary sequences. You might instead expect to use file and file-writer abstractions to protect you from that complication.
Once your protected from the detail then the implementation can be tweaked, modified or completely rewritten without your code breaking, this is what we mean by loose coupling.
Coupling is a mechanism by which classes are connected to each other and can act as a channel for all sorts of things to be transmitted between them including complexity, defects and breaking changes.
Encapsulate the Concept
A class in your code should represent something in your problem domain, anyone that understands that domain should recognise what the class represents and realise how it can be used.
When this isn't the case and a class only loosely represents a concept developers will end up having head scratching moments where they have to write code where it isn't immediately obvious either why some invisible join between classes has to be made or why a certain piece of functionality lies in a particular place.
Classes that do properly represent a concept are said to have strong cohesion, every line of code in them is all geared towards representing the same concept.
Cohesion is a mechanism by which understanding can be transmitted to a developer looking at your code leading to less head scratching and more productive time building well engineered solutions.
Strong and Loose
Sometime these concepts can get mixed up in developers minds, this isn't surprising since they very often interrelate, a class that operates at a good level of abstraction often also demonstrates good encapsulation.
A good abstraction hides the detail of the implementation by being well focused on representing a single concept within the system which brings us back to encapsulation.
The important thing to remember is that its all about the relationship between code, code inside a class should be very closely related whereas the code between classes should be very loosely related.
If code represents detail this means detail in one class shouldn't relate to or depend on detail in another class, if it does its because an inadequate level of abstraction is failing to hide detail or because bad encapsulation means code that should be together has been split-up.
Spotting which once of these is causing the problem is not always easy and in many ways is the essence of what it means to be an experienced developer but remembering that all these things are interrelated and that detail, complexity and confusion are the enemy is a pretty good start.   

Sunday, 1 November 2015

The Scores on the Board



As developers working within an agile team we spend a significant amount of time looking at some kind of sprint board.
We see what's left to do, whats being worked on and what we've completed. All of this helps us tell whether or not were going to achieve our goals.
But is their actually more to see on the board than that? Is it actually possible that the board can tell us something about our code?
Stories Reflect The Code
I'm sure all of us have been in the position where were struggling to see a way through the sprint.
"This story depends on that one, and we can't start that until this one is done" 
Quite often were quick to just write this off as bad luck or bad planning but when this happens over and over again is their something more?
Tightly coupled code will produce tightly coupled stories. If you struggle to break down your code base into elements that can be worked on individually and simultaneously all of your sprints will contain a lot of blocked stories, progress will be slow and much of your resource will be twiddling its thumbs.
This will never be more true then when your sprint involves making changes to an existing part of your system, the extra time taken early on in development to define some robust and flexible interfaces will be paid back here when you can fully utilise everyone in the team can get stuck into making the necessary changes.
This isn't just about the developers either, sprints with a lot of blocking stories will also be hard on testers as it inevitably results in a big bang of delivery to test towards the end of the sprint rather than a gradual stream.
How Many Points?
Even before the sprint starts the quality of our code can influence how smooth the agile process will be.
A major part of pre-sprint activity is pointing the stories which can on occasion be greeted by a large exhaling and puffing of checks from everyone involved.
An inability to point a story can be caused by the story not being well defined or ambiguous but it can equally be caused by the code.
It isn't easy to point stories when the code is large and complex and where the interaction between classes or sub-systems is strongly coupled in a manner that is not easy to penetrate.
In this situation their is also a tendency to be pessimistic adding on a certain margin because were confident things are going to go wrong when we start trying to work with that part of the code base.
This ball of string pointing where we become scared that if we change this everything else is going to come crashing down around us is another sign that the agile process is giving us just as much information about our code then it is about our product and team.
We Still Have Defects
Defects are a fact of life in software development, the business of writing code is too complex for mistakes never to be made and for consequences to always be anticipated.
But this doesn't mean that we should just shrug our shoulders whenever a defect is found. This isn't to say that we should be looking for someone to blame but more that we should be identifying why this particular change to this part of the code is causing so many problems.
This is not only to help understand how we can improve the code base but also to stop fear spreading. A strong desire to not change one particular aspect of the code can lead to bad decisions being made, decisions that from a structural or architectural view don't make sense but simply mean if we do it like this we don't have to touch that.
As with most things in life it best to take your medicine early, if you start seeing signs that something is wrong, no matter where those signs may be coming from take the information on board and plan a remedy.
Agile is indeed the gift that keeps on giving, it not only allows us to effectively streamline and manage our delivery but also gives a lot of feedback on the quality of our code all we have to do is listen.   

Sunday, 25 October 2015

Playing Nicely with Each Other



Nearly every app or piece of software of any reasonable size will end up having to integrate with another system, most commonly some kind of server.
This kind of integration can very often be a painful experience, two systems designed to come together seamlessly end up singing from different song sheets.
This pain also often results in aspersions being cast and blame being apportioned, "this is a server issue" or "it all looks fine in our logs".
So are we always doomed to this integration hell or are there things we can do to smooth the road?
Here not Everywhere
First of all integration shouldn't be all pervasive, code you don't control is dangerous, when you integrate with another system the surface area of your code that is exposed to this danger needs to be kept to a minimum.
When explaining the integration you need to be able to draw a circle around the class, package or namespace thats responsible for getting the job done. If you end up having to draw multiple shapes on the page to explain it all your probably just explaining how any changes in the system your integrating with are going to cause large scale destruction in your code.
Its also important, as always, that the integration is represented by a well engineered abstraction within your code. You never want your code to be exposed to details, this is especially true when you don't control those details.
For example you may be making a server call to get user data from a server, the only relevance to the rest of the code is that this data will be retrieved asynchronously from some source, the fact its an HTTP request or a SQL  query is detail. You need to throw another value into the header or adjust the query go right ahead its all in this class here.    
Its Not You Its Me
When it comes to testing your integration its important that your in a position where you can vary one thing at a time.
To go back to our example of making a server call, it should be possible to test your code against an idealised stubbed version of the server, whether this be loading data from a file or a more formal approach that provides some kind of virtualised backend.
Equally its important that on the server end you have some way to exercise your API that doesn't involve the application.
Obviously at some point everything needs to come together but before you leap headlong into it you need to have established some kind of confidence that your code works. If you don't have that confidence when something does go wrong you'll be left shrugging and probably laying the blame with the other guys.
Having this ability to stub or fake the interaction with the other system will also naturally force us to think about the interface, "exactly what is the JSON schema going to be?", "what format do you want us to send that in?". 
Independently Verified
Once we have defined the nature of that interface were in a really good position to practice some TDD and give ourselves another way to prove the integration is working.
As is the case with most things unit tests provide an effective first line of defence against things getting broken and can also be useful in playing out some "what if" scenarios.
We are now able to prove our individual classes work with our unit tests, prove our system as a whole works by using our replacement or fake server before finally attempting to actually integrate.
Despite all of this careful preparation things will still go wrong, quite often because one of the systems fails to comply with the agreed contract, "if I give you this your supposed to give me that".
In order for us to know whats going wrong we need to have identified exactly what that contract is and we need some way of verifying that individual elements when given the correct input will supply the correct output.
This isn't about apportioning blame its about having a way to quickly identify what needs fixing and fixing it, were all in this together after all.     

Sunday, 18 October 2015

Telling A Tale of Code


When we become experienced developers there is a subtle change in how we read code, we stop necessarily reading keyword by keyword, variable by variable.
Instead were able to recognise prose we've seen before and appreciate the story being told whether good or bad.
Our ability to do this is dependent upon the care that is taken in trying to tell the story.
An often quoted mantra is "Good code doesn't need comments", while it would be a slightly extreme view point to never write comments its certainly true that good code is readable code and there are certain techniques we can use to maximise this readability without the need of explanation.
Whats In a Name?
All good stories need well defined characters, a skilled author is a master of nouns and verbs, clearly the name of a variable should convey what is being stored and a function should describe what action it will perform.
What can be more subtle is that in neither case should we be describing the how, a variable name shouldn't talk about the underlying data structure and a function name shouldn't expose detail about implementation. For example should a method be called saveToDatabase() or just save()?
Details change and unfortunately there is no inherent mechanism to keep our variable and method names in sync. One form of unreadable code is where you simply cannot tell what is going on, another just as destructive one is where you think you know but the reality is very different.   
Paragraphs and Chapters
No matter how skilled you become at reading code there is a limit to how much code you can interpret in one go.
Another approach to things like the Single Responsibility Principle and Interface Segregation Principle is that they limit the required scope of understanding of the reader.
By dividing and conquering we make our code more readable and understandable because their is less action on the page and less to get our heads round.
Logical structure also means we can take much more as read, if your injecting a dependency called IPersistentStorage that has a well defined CRUD interface I have no need to look into the detail and I don't need to understand anything more than what is on the page.  
Clear and Consistent Voice
In a similar manner to having a logical structure having a clear and consistent voice when writing code will also go a long way to helping others understand your story.
This means a common approach to naming, using similar solutions for similar problems and ensuring that a common plot is running through the whole story.
A common problem when looking at large open source projects is that many people have been involved in its development, each with a different story to tell and a different style and approach.
Individually all these different voices may be clear and logically but when there all layered over each other it suddenly becomes confusing. Navigating around the code base suddenly feels like different chapters from different books.
In many of these situations it doesn't really matter what you do, just always do it that way.
A large amount of comments in a code base is a slight smell, whether its because the writer feels you may not understand this bit or because they want to draw attention to there genius it implies the code itself is not well structured or logical.
This becomes even more prevalent as the code base evolves as the comments get more and more out of date with the code there supposed to explain.
There will be occasions when you really are forced to do something that requires explanation but ultimately nothing is more expressive than the code itself after all its this that defines what the story is and how it will end.  

Sunday, 11 October 2015

Square Pegs and Round Holes


When writing unit tests one of the most important things that will determine whether we end up writing good tests or bad tests is how we deal with dependencies.
Bad tests won't properly separate a class from those it uses creating either unstable tests or tests whose results don't actually tell us anything about the health of the class supposedly being exercised. 
It is also very often the case that problems we face with dependencies when trying to test a class are indicative of bad design, testing shouldn't be difficult, but of course your all practising strict TDD and writing the tests first right?
So how can we supply dependencies during tests?
Sucking on a Dummy
A dummy object contains no useful functionality, it is not intended to be used by the class under test but simply by the wonders of polymorphism it can stand in for a dependency.
The fact that a dummy can be used during a test is a big smell, if you aren't going to use this object why am I having to supply it to you? Yes I could potentially pass null but that degrades readability and you have to know that null isn't actually going to break anything.
If your supplying a dummy to a constructor then clearly the class under test doesn't need the dependency to fulfil its role, maybe it does on occasion require that dependency but if its not essential it shouldn't be in the constructor.
If your supplying a dummy to an API then this would suggest you could make the developers life easier by supplying an overload that doesn't require the dependency (internally you can just call the original API passing null). This makes it much clearer how your API should be used requiring the developer to have less implied knowledge about how this all works.  
Faking It
Fakes appear to offer the same functionality as real dependencies but they take a short-cut or cheat in someway, for example supplying JSON from a file rather than calling a server.
Generally this is done to de-couple the code from some outside dependency and give more control over the data the code is working with, they also generally remove any performance factors from influencing how the code behaves.
There are many good situations to use fakes especially when prototyping or trying to debug specific conditions or data, unit testing is generally not one of this situations.
The implementation of fakes is not necessarily simple, this potential complication can add ambiguity to unit testing, this is something to be avoided.
The output of unit testing must be clear and unequivocal, if its red we need to fix the class under test, if its green were all good. Anything that detracts from this simplicity should be removed.    
Stubbing It Out
Stubs are essentially containers for pre-programmed responses to interactions, if X is called return Y, this is regardless of what is passed in the call and how many times that call happens.
They provide no response to APIs where nothing as been put in the container and they carry no expectations on what should and shouldn't happen.
This all leads to the important aspect of stubs, they play no role in deciding whether a test passes or fails.
Stubs should be used to fulfil dependencies where we don't particularly care how the class under test interacts with it, this will generally be because the call does not carry a high overhead and doesn't have any side effects for the system as a whole.
It maybe that we have an API that checks for some environmental condition or retrieves some configuration data, do we really care whether this fairly inconsequential method call is made 2 times or 3 times? Or are we more concerned with the fact that the business logic ends up doing the right thing?
Once again its important that if a test fails then we know for sure we have to fix something, if tests fail because of some trivial function call this detracts from that clear cut view.    
Send in the Mocks
And finally we come to mocks.
Mocks are much like stubs but with one important difference, they do play a role in deciding whether or not tests pass or fail.
Like stubs mocks contain pre-programmed responses to method calls but unlike stubs they do carry expectations on how these methods will be called. With mocks it does matter what methods are called, how often and with what arguments.
Mocks should be used for dependencies where it does matter how the class under test uses them, for example whether we make a server call once or twice or how many times we hit the database is important. 
With the other types of objects we've discussed were more concerned with verifying the output of the class under test, with mocks we are also interested in verifying how it provides that output.
Its a skill to determine the best way that mocks should be used, once again to hammer this point home failing tests have to carry a clear message, being over zealous in using mocks to enforce very strict limitations on the class under test will lead to nagging doubt that maybe the problem is with the test rather than the code being tested.
Its important that unit tests are treated with the same care and attention as our functional code base, this isn't just because were relying on these tests to give our code a clean bill of health but also because they shine a light on issues with our implementation.
When you encounter some difficulty in supplying a dependency to a class under test don't just write this off as being just one of the frustrations of writing tests, instead ask yourself what this tells you about the class your testing.
If your class is a well written loosely coupled class with high cohesion and a well thought out API maybe you shouldn't be having this problem?