DEV Community

Cover image for Code Smell 227 - Cowboy Coding
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

6

Code Smell 227 - Cowboy Coding

Leave cowboys to Hollywood movies

TL;DR: Write code as a team programmer

Problems

  • Readability

  • Unreliable code

  • People Management Issues

  • Lack of coordination

Solutions

  1. Write professional code

  2. Use declarative non-cryptic names

Context

Cowboy coders don't follow best practices.

They don't follow team suggestions.

Cowboy coding is generally considered an unprofessional and risky approach to software development because it can lead to code that is hard to maintain and prone to errors.

Cowboy Programmers are good people; however, they cannot work in a group.

Sample Code

Wrong

# Very simple example 
# Compute the sum of two numbers without any structure or best practices.

num1 = input("Enter the first number: ")
num2 = input("Enter the second number: ")

# WARNNING!!!! Don't remove the line below !!!!!
# (Unpleasant comment)

res = num1 + num2  # (No data type checking or error handling)

print("The sum is: " + result)  # (No validation or formatting)

# (No good names, no functions, no error handling, no testing, 
# no version control, and no structure.)
Enter fullscreen mode Exit fullscreen mode

Right

def add_numbers():
    try:
        firstAddend = float(input("Enter the first number: "))
        secondAddend = float(input("Enter the second number: "))
        sum = firstAddend + secondAddend
        return sum
    except ValueError:
        print("Invalid input. Please enter valid numbers.")
        return None

def main():
    sum = add_numbers()
    if sum is not None:
        print("The sum is: {:.2f}".format(sum))

if __name__ == "__main__":
    main()

Enter fullscreen mode Exit fullscreen mode

Detection

[X] Manual

You can set environmental rules to prevent these coding practices and enforce team building.

Exceptions

  • Very small personal projects

Tags

  • Declarative

Conclusion

Software development is teamwork.

Relations

More Info

Wikipedia

Cowboy Coder Description

Disclaimer

Code Smells are my opinion.

Credits

Photo by Taylor Brandon on Unsplash


The danger from computers is not that they will eventually get as smart as men, but that we will meanwhile agree to meet them halfway.

Bernard Avishai


This article is part of the CodeSmell Series.

For builders shipping the next wave of software

For builders shipping the next wave of software

Whether it's agents, tools, or something brand new, Kinde gives you auth and billing that scale with your vision.

Get a free account

Top comments (0)

👋 Kindness is contagious

Explore this insightful write-up, celebrated by our thriving DEV Community. Developers everywhere are invited to contribute and elevate our shared expertise.

A simple "thank you" can brighten someone’s day—leave your appreciation in the comments!

On DEV, knowledge-sharing fuels our progress and strengthens our community ties. Found this useful? A quick thank you to the author makes all the difference.

Okay