softwaretested
Jessica Bullet
Updated on
Useful Resources

15 Signs You Are Writing Bad Code (And How to Fix It)

No matter how much time you spend writing code, you will almost certainly write bad code at some point. This can happen to the most experienced developers, and it’s easy to do, especially when you’re under pressure or time constraints or working on new and unfamiliar projects. 

However, there are signs that your code is not as good as it should be, and if you learn to recognize these signs, you can make the code cleaner before they cause problems.

Too General

This might seem like an undeniable point, but if your code is too general, it will be difficult for anyone else to use.

Pro tip

The best way to fix this is by getting more specific about what you try to do with the code.

For example, if you have a function that does something very generic, like print out “Hello World!”, it may be time to break that down into smaller functions that each perform a specific task. 

If the function prints out Hello World!, maybe there should be one called “Print out Hello World!”. This ensures that your code can be easily reused and others know what they are getting before using it.

A specific code is ideal, especially when creating software and entities like the hpdm framework.

Duplicated Everywhere

Many people think writing duplicate code is a good idea because it makes it more maintainable and easy to update. However, this can be an ad if the code is copied in multiple places and you end up with some duplicated lines of code that have been edited at different times and are out-of-sync with each other.

Pro tip

Duplicate code should be avoided whenever possible.

Look for duplicate blocks of text and consider refactoring your code so that it can use functions, loops, or other constructs to prevent duplication. 

When you find duplicated text, it may be best to leave the blocks as they are for now until the other changes have been made. Then the copied parts can be removed simultaneously before finalizing those edits.

No Testing

Another sign that your code is bad is if it has no tests. Your code should be covered with tests; the more complicated it is, the more coverage you should have.

Pro tip

If you find yourself writing code without any tests, stop and write some tests for what you just wrote. This will ensure that you don’t break anything else while working on what was broken before.

If there’s no way for you to test something because it relies on external data or some other system that’s not in your control, then try making a quick mock-up of what would’ve been needed from an API or database query so that you can at least write some code around those assumptions.

Using Long Methods and Functions

Long methods and functions sign that developers are trying to do too many things in one place or have written an overly complex algorithm

Pro tip

It’s always better to break down the problem into smaller parts and only work on one aspect simultaneously. If you find yourself repeatedly returning to a part of the code, you should probably make it its own function.

Once you’ve broken your long method or function into smaller chunks, you can move each piece of code closer to where it is needed within the program, so there is less jumping around from one part of your codebase to another

A good rule of thumb for methods is that if it takes more than 20 lines (excluding comments), it should be refactored into its class with specific functionality.

Dependencies are Wrong or Missing

Dependencies are the packages or libraries that your code relies on. If you’re missing a dependency or need to set one up, your code won’t work as expected.

The error will be something like Module not found in module_name. Some common signs of dependency problems are 

  • There is a yellow warning sign next to the package name in your project file, meaning it is a warning rather than an error. 
  • There is a red circle with a line through it next to the package name in your project file, which means that it is an error and prevents you from executing your script. 
  • Missing imports: When you don’t import a library before using it in your code, missing imports occur. It’s easy to forget this if many libraries have different functions in the same folder. 
  • Unexpected behavior: Sometimes, even if all your dependencies are right and imported correctly, you might still have unexpected behavior because of how they interact. For example, if two modules want to use the same data structure or function, they’ll clash when they try to access each other’s data structures or functions simultaneously.

Bugs

One of the most common signs that you are writing harmful code is that there are bugs. 

In software engineering, a bug is an error or fault in a computer program that causes it to produce an incorrect or unexpected result.

Pro tip

If your code has any bugs, you will need to find and fix them before moving on with development. Bugs can be tough to locate, so if you have found bugs in your code, it is best not to continue developing until they have been fixed.

Also, it’s essential to work backward from the problem when trying to fix these bugs. For example: if you notice that the final output from your program does not match what was expected, then you should try to track down where the input data goes wrong and make corrections accordingly. 

Sometimes, developers use debugging tools like debuggers and loggers for this process. 

Overly Complex Code

One of the most frequent mistakes programmers make is writing overly complex code. The simplest solution is often the best and will cause you fewer headaches down the line. 

Pro tip

If you find yourself trying to solve a problem with an insanely complicated algorithm, it’s time to rethink your approach. What are the possible approaches? What information can you gather?

Even if it seems like all roads lead to Rome, there must be a better option than just choosing randomly or picking one randomly. Take a few minutes to think about how your program should run before you begin coding. 

No Security Measures

One of the biggest problems with security measures not being implemented in code is that they are too easy to hack. This is especially true when the code has a known vulnerability. 

Hackers will look for these vulnerabilities and exploit them as soon as possible. The more popular a system is, the easier it will be for hackers to find it, which means you need security measures before your product becomes popular. 

If you don’t want to implement any security measures or spend time developing one from scratch, some services are available that provide user authentication and authorization mechanisms.

Code Breaks When Changing Something

Code needs to be flexible and resilient. If any changes you make –  break the application, you should fix it as soon as possible. Otherwise, these fragile parts will continue to grow in size and complexity. 

It may become difficult or impossible to change anything without breaking other application features. This can result in a complete rewrite of the application because of one lousy part that no one noticed until later.

Sometimes, pieces of your application are not easy to test with unit tests. Or they take too long to run all the tests. In addition, brittle code is hard to test. 

When this happens, it becomes very tempting for developers to start adding more if statements and conditionals so they know exactly when things go wrong during testing instead of fixing the underlying problem. 

However, this ends up making your code even more brittle than before – now you have an error on one line, but everything works on another line! 

No Room For Improvements

Does it seem like it does too much? This might be because it’s doing too many things at once. Often, it can be difficult to tell what you’re trying to accomplish with a particular piece of code.

Pro tip

Try breaking the code into smaller pieces that work on one task each.

It can also be challenging to figure out how the different parts of your code interact. One way to fix this issue is by separating your different areas and organizing them, so they’re more straightforward to read.

Too Many IF Conditions

IF statements are powerful and a great way to execute codes depending on conditions. They can get out of hand and start involving too many conditions. That makes the code difficult to read, debug, and maintain. 

Pro tip

Consider breaking it up into two or three separate statements if you use an IF statement with more than two or three conditions. The tradeoff is that there will be more lines in your file, but all the individual statements will be shorter and easier to understand.

Loops can also turn into spaghetti code if they aren’t controlled well. For example, if one person writes a loop ten lines long while another writes theirs as 100 lines long, both loops would be long-running. 

The difference is that one person’s loop is easy to follow while the other’s takes much longer to grasp what’s going on.

Remember that simplicity always wins when it comes down to readability. Try not to let loops grow any longer than 15 lines so everyone can keep track of what’s happening.

Too Many Branches in Control Structures

As a developer, you write tons of code lines daily; chances are higher that some will be bad. 

Refactoring involves changing the internal structure of your code without changing the external behavior. This helps keep your code clean and easy to read. 

The other way to prevent terrible coding habits is by avoiding common pitfalls in software engineering, such as complexity and overuse of conditional statements (branching). These are two ways that developers often fall into when writing bad code, but they have ways out.

Many Magic Numbers

A magic number is a literal number with no meaning outside the context.

They are often used as constants in code, but they can be hard to find when you need them later on because they have no name. Using an identifier for these numbers will make your code more readable and easier to debug.

Magic numbers are wrong for two reasons: 

  • the value cannot be changed;
  • the variable name does not provide any information about what it represents. 

If a variable’s purpose changes, it should be renamed so the new purpose is clear. If the variable has nothing to do with the rest of the program, it should also be renamed or deleted from the program altogether.

No Enough Comments

Comments are a programmer’s best friend. They tell you what the code does, which is essential when it comes time to update or troubleshoot your project.

For instance, the 3rd-5th lines tell us what to do with the code:

Pro tip

Use comments to give your teammates an idea of code purpose so they can learn from your experience. Finally, comments remind you of what you had in mind when writing this code section, which is especially helpful if you come back to work on it later.

No DRY Code

The Don’t Repeat Yourself (DRY) code principle states that every piece of knowledge must have a single, unambiguous, authoritative representation.

If there are two pieces of code for the same functionality, then you should delete one of them.

So, what does this mean?

It means that when you copy and paste chunks of code and call it algorithm, you create poor-quality software. Your method has three parts, but when something changes in part two, it will not just affect part three; it will also affect part one. Sure enough, an error appears in the last place you would expect.

Conclusion

Writing bad code can be detrimental to your team and your business, but you don’t have to fall into this trap. If you want to deliver high-quality code every time, you should ensure that it doesn’t contain any signs that indicate that you’re writing bad code, as well as how to fix them if it does. I hope these tips help you write better code for your software.