Stop Looking for the Perfect Solution

There is no such thing as perfect solution. There are many alternative solutions and some are better than others. But the most important is – any solution is better than no solution!

Key takeaway:

  • Refactoring is a good thing. It can simplify the code.
  • Getting lost in the details is time-sucking. Always have an objective when you re-write your code.
  • Don’t try to find the best solution, find one which is good enough and start using it!

What’s refactoring?

When you refactor your code, you change it without affecting the way it’s working. Here are three simple example.

a = "Csilla"
b = "engineer"
print a + " is an " + b
name = "Csilla"
occupation = "engineer"
print name + " is an " + occupation
persons = []
persons.append({"name": "Csilla", "occupation": "engineer"})
for person in persons:
  print "{0} is an {1}".format(person['name'], person['occupation'])

If you run all three code snippet in Python 2.7, they all will just display: Csilla is an engineer, but all of them will achieve it differently. Which one is better? It really depends. The first one just works. The second has a bit more meaningful names. If you need to print this for plenty of people, a better solution would be the third.

Infinite refactoring

When I start working on a project or script, I notice something strange on how I write code. Sometimes I write something, then I look at it and find a better way. A few minutes later, I found another way and start changing around. I could do this for hours. These iterations can make my code better, but after I changed around everything for the third time, I start losing focusing on the problem I am trying to solve and get lost in details.

Tweet: Network Automation only works if everyone is on board. Please click to share and help to create a Movement!

It’s like when you create a diagram. The first version is ready within 20 minutes with all the most important details, such as hostnames, links and IP addresses. Then you may spend another 4 hours just to re-arrange, adjust with few millimetres, change colours, etc.  When is a diagram ready? Never. But you can stop after a while and you say it’s good enough.

When is your code good enough?

There is never only one solution to a scripting problem. You can always structure your data differently or find other libraries and functions.

The first question you need to answer:

  1. Is my code functional?
  2. Does it cover the typical error scenarios/exceptions?
  3. Is the usage of the script straightforward?
  4. Has it been tested?

If you answered all of the questions with ‘yes‘, you are in a good place. If you have ideas how to make it better via refactoring, it’s great. It’s great, because the work you’ve done actually changed the way you write code. Next time, if you start something from scratch – based on this experience – you’ll start with a better structure.

At this point you need to look at: what is the benefit of re-writing parts of the existing code. Let’s say – you want to replace the variable names for something more meaningful, that could be done quickly and would make the code more readable. You may want to make it run faster? Is speed important? Maybe not.

Why is it important?

If you declare your code good enough – you can move on to another task. Learning to evaluate when it’s time to move on can improve your performance. When there is a new requirement, you have a chance to look at it again with a fresh eye and change it if needed.

If you say – something is “ready”, you and others can start using it, ultimately saving time!

Don’t miss out and stay motivated by subscribing to email alerts (right on desktop, bottom in mobile) or like our Facebook page.

Till next time…


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s