What defines ‘simple’ code?

Simple code is not complex code. Ok, well what does complex code looks like? We don’t like or want complex code because it is:

  • hard to read
  • hard to understand
  • difficult to update and maintain

Ok, so simple code therefore is:

  • easy to read
  • easy to understand
  • easy to update and maintain

As a result of the above, other benefits become more easily within reach. For example, simple code is easier to unit test.

What can we do to ensure we write simple code? Many of the commonly known (but not commonly applied) industry best practices lead to simpler code. For example:

  • Single Responsibility Principal: Bob Martin defines this as a ‘single reason to change‘. A class should have a single responsibility, one feature that it is responsible for. If there is more than one reason that would require changes to this class, it has more than one responsibility, and therefore is doing ‘too much’.
  • A class or a method should do one thing and do that one thing well. Not 10 things, not 5, just one thing. Limiting to just one thing reduces the opportunity for complexity to creep in (this is really the same idea as the Single Responsibility Principal).
  • A single method short be short enough that you can easily read it and grasp the whole intent. Too long is when you have to scroll page after page, and at that point, it’s difficult to grasp the entire purpose of the method, without scrolling around and re-reading. If it takes you too long to read to the end of a method, and by the time you get there you’ve already forgotten what it was doing at the start, and the start of the method is already several pages off the top of your screen, your method is too long.
  • Clearly named variables, methods and class names: a clear name that describes what the variable is for (it’s purpose, what does it represent), what a method does, what a class does, helps to convey it’s purpose and improve understanding. A method that does something else other that what it’s name describes it not simple and it not easy to understand. We don’t like surprises.
  • Clear documentation. In Java we use JavaDoc. Your JavaDoc should describe what the Class does, what each public (at least) method does. It should NOT state the obvious, it shouldn’t repeat what is already implied from your clear class and method names. If you’re just repeating what the method name says, you’re not aiding readability, you’re adding more content that I have to read, but for no gain. For example, this is not useful documentation, although many developers do this:
/**
* This method creates a new account.
*/
public Account createAccount(){
...
}

… I know it’s a method because I am a developer, you don’t need to tell me that. I know this method creates an account, because the method name says so. This JavaDoc adds no additional value, and if there’s no additional detail to be added, it would be best just left out.

If you set out from the start to create simple code, it’s more easily achievable than creating something too complex and then trying to simplify. Refactoring is your friend, and you should always invest time to refactor when you’ve finished your first iteration of getting your code working. However, by aiming to avoid complexity from the start you can make your job easier in the long run.

My software development blog posts from 2018: AWS, Twitter bots, Machine Learning, Docker, Kubernetes and more!

Looking back, I’ve investigated and played with a lot of interesting stuff this year, I was pretty busy 🙂 Early in the year I was doing a lot of experimenting with AWS as part of my prep for the AWS Solution Architect Associate exam, so a lot of my posts this year were AWS related, but I did some a some time working on some other projects too.

Here’s a look back at some of my favorite personal projects and blog posts during the year:

  • Using AWS Sagemaker to train Machine Learning models – here and here
  • mvmdvm and Pi-Star setup – multi digital voice Amateur Radio modes on a Raspberry Pi – here and here

Phew that’s quite a lot! I sincerely appreciate the feedback and comments I receive on my posts and hearing that at least some of my content is useful to others. I look forward to continuing to share my thoughts and write more content that hopefully will be continue to be useful in the coming year.

Happy New Year!

InfoQ interview with Martin Fowler : 2nd Edition of his 1999 Classic, Refactoring, is now shipping!

Martin Fowler’s classic software development book, Refactoring, was first released in 1999. It’s been a staple on my bookshelf since I got a copy in 2000, something that I regularly refer back to for advice on how to improve the structure and maintainability of my code.

The 2nd edition of the book has now been released, updated for 2018, with all the code examples in the book which were previously in Java now replaced with equivalent examples in JavaScript. I have my copy on order with Amazon and should be receiving my copy before the end of the year.

InfoQ have a great podcast interview with Martin, discussing the motivations for releasing an updated edition for 2018, 19 years after the 1st edition was released. Check out the interview here.

The best way to learn anything new in software development is to try it out yourself

You can read books, watch YouTubes and listen to as many podcast as you like, but the best way to learn anything new in software development is to try it out for yourself. Why? Because you’ll learn far more from the hands-on experimentation with a new tech/library/api when you try to use it that can ever be transferred as knowledge and experience from a single book/video/article/podcast. What you can learn from a single 1 hour podcast can give you a high level overview of a topic, but you can never learn as much as you will from trying it our yourself.

Part of the learning experience is working out how to solve the problems you run into. The ‘huh, it never said that in the manual’ experience. Once you’ve worked through the unexpected issues along the way, you’ll have built a much deeper understanding of what it actually takes to use a new technology. It’s where the rubber meets the road that counts.