Don’t Repeat Yourself. A Life Changing Obvious Idea In Programming.

In many ways, the idea seems simple, but it is easy to forget. When you are writing a program repeating code happens quite often. Sometimes it happens in various sections of your program so it isn’t always quick to recognize. Other times, we are just in a rush to get something done without thinking it through so code gets cut and pasted several times with few if any changes.

While, we have all made those mistakes, it makes your life easier to step back and find a way to fix it. Often, the fix involves pulling out a chunk of code and turning it into a function or a method. Other times, it involves rethinking why you are doing things the way you are doing them. Still other times, finding better ways to use the language will solve the problem.

A common fix in Swift is to replace a collection of if statements with a case statement. That doesn’t solve all of the issues, but it helps out more often than you’d think. As I am writing this, I can think of several areas in my own code that would be helped by this piece of advice alone.

If you can avoid the repetition, it will make your code more readable. But, beyond that, it will also make your code easier to update. It is far easier to change one function than to track down all the areas that you might have used the same code in order to make a simple change. Just think of how many potential bugs could be introduced by missing one or two areas.

Another way to avoid repetition is to set up constants for values that get used throughout your program. This allows you to change values in many places by only setting one variable. I use this many times with various values such as a scale modifier, or animation constants. It allows you to stay more consistent through the code, while also allowing you to make quick changes. If the value is limited in scope, then try to define it in that scope if at all possible. Also, make sure that you use variable names that are descriptive and make sense. It’s far easier to figure out why you used spriteScaler than a number 4.5 when you are reading the code.

So, take a few minutes to look through some of your code to find areas that you repeated yourself and take the time to fix that issue. You will thank yourself in the future.

Don’t Let Negativity Destroy Your Work.

Negativity has a way of creeping into work. It can show up in many forms – our own thoughts, frustration, errors that won’t go away, or a bad review. We are taught from an early age that negativity is bad and we should simply pay attention to positive thoughts. While, there is some value to that thinking at times, it ignores the potential gains you can get if you can find a way to be constructive with the negativity.

An easy example is in the form of a negative review. If the person making the review took the time to give reasons for that review then it is worth given those reasons some thought. Do their points reflect issues that other people may have? Are there suggestions for new features that would make sense to implement? Are they having a problem understanding some aspect of your program that could benefit from some documentation or an interface update? Does the description of your program suggest features that don’t exist? There are many possible reasons for a bad review – many of them will suggest useful updates that will allow your program to better serve its intended users, or even open it up to new users.

It is also important to realize that the outcome of a bad review won’t always be positive. That’s ok, too. No program can possible make everyone happy. It just isn’t the way it works. If you have a large collection of bad reviews for one program then you really need to find out what the problems are and fix them. But, if you only have a handful of bad reviews spread through a large number of good reviews then take what you can from them and find a way to put it behind you. I know that’s easier said than done, but it is necessary. If you let the negativity be your focus then the rest of the program is going to suffer. If you let that happen, then you will never succeed in creating useful programs or apps.

Also, know your intentions with your program and don’t let their suggestions take those away unless it makes sense. I know some of my own apps have changed drastically because of a reviews and they were better for it. On the other hand, I’ve also ignored suggestions that were brought up in reviews because they would change the app at a fundamental level that didn’t make sense.

I have also had several reviews that simply stated the app didn’t work or didn’t work as they expected. As far as I can tell, the app wasn’t crashing. There didn’t seem to be any other issues that I could see. At the same time I was getting great reviews and emails praising the same app, so it helped me push those useless bad reviews out of my thoughts. If it had been the other way around and I was getting more bad reviews that good reviews then I would have taken a serious look at the app and figured out how it was failing.

In the end, in all of these cases, I was able to use the reviews to better apps and the app experience. But, the negativity doesn’t just come from other people. How can you get past your own negative thoughts? That one is a bit trickier and will depend on your own personality.

For me, I often step away from a project for a bit. Either I work on something else, or I do something distracting away from the computer. If I’m fighting a problem that can’t seem to be solved, I often walk away completely for a bit. I have solved many issues on walks, in the shower, or in my sleep. If you get frustrated to the point that work isn’t getting done, then it makes no sense to keep trying. Walk away. Come back to it later or the next day. If you can get away from the computer altogether, then do that. If not find an unrelated project and focus your attention there. Yes, even if there are deadlines in place. If you brain is stuck, it needs a change of focus before it will unstick itself. Trying to do anything else is going to hurt the overall project and just waste your time and possibly other people’s time.

So, the short version. Negativity happens. Find a way to take anything valuable away from it and walk away from the rest.

Sometimes, Diving into Your Past Can Be Constructive.

I wrote yesterday about keeping old ideas in a notebook. Well, it’s also useful to take some time out occasionally and page through those old notebooks. It is often interesting seeing how much you have grown over time as your skills and knowledge has expanded. It’s also often useful to see how you solved problems in the past and to possibly use some of those ideas in the future.

The past version of you was far less knowledgeable, but sometimes that lack of knowledge leads you to solutions that seem less obvious once you understand things. It is sometimes those very solutions that are the most interesting and elegant.

Basically, you need to remind yourself that sometimes looking at the world with your earlier naivety will sometimes offer you the perfect solutions. Let yourself view things as you once did and then use your current knowledge and experience to make it work even better.