Mobile App Development Blog

Blog Home

Coding For Apps: Does your code smell? Time to refactor it!

November 16, 2016

As an entrepreneur, you know that you have a functional mobile app and it’s working reasonably well. You have no reason to believe that there’s anything wrong with your code. You may even ask, what is this stench that you are talking about? And why can’t I smell it?

refactor-code

Well, let’s just say expert software programmers are like bears when it comes to smelling bad code. We have a critically low threshold for odor (or in this case, bloated code). What may look like a massive codebase to you may actually come across as an absolute mess to us. While writing and maintaining code, we rigorously follow Grandma Beck’s advice on child rearing: “If it stinks, change it.”

But how can I detect rotten code?

To us, any code that is less than ideal is rotten code. And the intention here is not to judge your codebase, but to help you figure out what red flags to look for so that you can refactor your existing code and adjust it in a timely fashion. Let’s try to spot some common issues:

  • Duplicated Code: When you see the same code structure in more than one place, you have successfully detected the number one problem. Duplication is usually the result of multiple software engineers simultaneously working on different parts of the same program. One programmer may end up writing code that is similar to a colleague’s without realizing it.

But not all instances of duplication are easy to spot. For instance, code that looks different at a glance may be found to be performing the same function when examined a bit more closely. These are the sneaky little pests that are especially good at hiding.

  • Long Method: This one is fairly easy to detect— when you come across a method that contains too many lines of code, you’ve found another culprit. The problem with this one is unsurprising: simply put, it is difficult to work with code that has increased to gigantic proportions. The longer the method, the lower the cohesion and the harder it is to maintain and understand the code.

Some developers may say that long methods are not always bad. While we can argue about that, for the sake of this post, let’s just say that they are nearly always bad.

  • Switch Statements: This one is most abused in object-oriented code. The problem it presents is more or less similar to that of duplication. Usually, similar switch statements are scattered throughout a program. If you add or remove a clause in one switch, you often have to find and repair the others too. So whenever you encounter switch statements, it should immediately make you suspicious of your code quality.

What is refactoring?

Simply put, refactoring is the technique to improve the quality of existing code. While refactoring server side code, we alter the internal structure of the code without making any changes to its external behavior. In other words, the functionality of your app never suffers; all the changes occur in the background while your users can continue enjoying your app without skipping a beat. They’ll never see any ‘Work in Progress’ or ‘Under Construction’ landing pages.

Please note: Refactoring is not rewriting code!

How do we do it?

While refactoring, we don’t axe your code altogether. We make a series of incremental changes (smaller refactorings) one step at a time. Each change, when viewed in isolation, may not look like much. But when viewed in its entirety, the changes amount to significant restructuring. Taking it slow has its own benefits: you are less likely to go wrong, your app is fully functional and your system stays intact even after a massive restructuring.

But my code works, why should I refactor?

Yes, we’ve heard that before. And yes! You are right, your code does work. But the point of refactoring is not to fix broken code; it is to clean it up, to simplify it and to make it more amenable to change.

We understand that you have a schedule to take your product to market. You don’t want to do anything that will hinder meeting the tight deadlines you’re working with. Basically, why fix anything when it’s not even broken?

Well, if you are writing a program that is not meant to change over time, then leave your code as it is. The compiler doesn’t care whether or not your code is clean. But if you adopt an agile method of development, your code is likely to get enhanced with every subsequent iteration and without refactoring, there will be unhealthy dependencies between classes or packages, poor allocation of class responsibilities, way too many responsibilities per method or class and enough instances of duplicate code. Basically, an insane amount of clutter. And no human likes to navigate through rot.

As refactoring also makes the code more modular, it becomes particularly useful for startups and businesses from a mobile app development standpoint. You can save a lot of app development time by using modules of code from one app and extending its capabilities into another app.

How can code refactoring help you?

Refactoring server side code has many advantages. If you don’t have the time, you can have an expert app development company do it. A third party vendor doesn’t need to know the intricacies of your app to make changes that clean up code and reduce latency. They only need a strong grasp of the language you’ve written your server side code in to be able to make changes that save you time and increase customer satisfaction. And the benefits are enormous:

  • An Improved Code Design: As the code keeps changing, it also starts to lose its structure. With more and more lines of code being added, it becomes increasingly difficult to see the design by reading the code. The design essentially starts decaying. Refactoring can help you eliminate bits which are not in their proper place and also remove duplicates.
  • Easier To Read Code: More often than not, while writing code, people forget to factor in future developers. They simply don’t realize that writing code is much easier than reading code. While you may understand and be able to navigate through your own complex and bloated code, other people may find it very hard to, unless it is written following best practices. That is why refactoring can offer a huge benefit if you’re working in a team or thinking about getting help from a third party vendor. Cleaner, easier to read code makes it easier for both you and other entities to make changes to your existing codebase.
  • Error Reduction: One of the biggest benefits of refactoring any piece of code is that while individually examining each line, you’ll be much more likely to spot small errors or bugs that you may not have noticed before. It’s not likely that you’ll have syntax errors if you have a functional product, since your compiler probably will have pointed those out to you. But there are other ways a product can have errors while still functioning. Chief among these to watch out for are security flaws. It is much easier to spot vulnerabilities in a code base when you’re reading over it line-by-line.
  • Latency Reduction: Refactoring your server side code may have another key benefit: making your product faster! A product making many thousands of calls to a server in a short time can be greatly improved by simple code refactoring that takes mere milliseconds off of the latency of each call. Latency can be reduced in many ways. It can be as simple as deleting unused and unnecessary variable assignments and calculations, or a second reading of your code can reveal that it would be more prudent to use a different algorithm in a calculation.
  • Improving Efficiency: Going hand in hand with latency reduction is improving the efficiency of your product. As mentioned earlier, the effect of a minuscule improvement in the efficiency of a server call will be magnified when that same server call is made several thousand times in a short timespan. Creating a more efficient product will win you customers who want fast and effective service, and will make the load on your servers easier to bear.

We are cleanliness freaks! Are you?

A wonderful blog maintained by a code hygienist explains refactoring perfectly: “A popular metaphor for refactoring is cleaning the kitchen as you cook. In any kitchen in which several complex meals are prepared per day for more than a handful of people, you will typically find that cleaning and reorganizing occur continuously. Someone is responsible for keeping the dishes, the pots, the kitchen itself, the food, the refrigerator all clean and organized from moment to moment. Without this, continuous cooking would soon collapse. In your own household, you can see non-trivial effects from postponing even small amounts of dish refactoring: did you ever try to scrape the muck formed by dried Cocoa Crispies out of a bowl? A missed opportunity for 2 seconds worth of rinsing can become 10 minutes of aggressive scraping.”

So, unless you refactor your code, you are likely to spend your time finding and fixing bugs rather than adding new functionality.

Want us to help you refactor and improve existing code of your app?

Talk To Us!