Code Smell 227 - Cowboy Coding
Tags: webdev, beginners, programming, tutorial
Published: 2023-10-15 20:52:56

*Leave cowboys to Hollywood movies*

> TL;DR: Write code as a team programmer

# Problems

- Readability

- Unreliable code

- People Management Issues

- Lack of [coordination](https://en.wikipedia.org/wiki/Cowboy_coding)

# 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](https://www.linkedin.com/pulse/software-development-cowboy-coding-hakan-atba%C5%9F/).

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/b600a57bd4f4b095b919cbc6a2e65323)
```python
# 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.)
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/5fc81635624c7e7ed7f0de537094ad6d)
```python
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()

```

# 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

{% post https://dev.to/mcsee/code-smell-06-too-clever-programmer-37lb %}

{% post https://dev.to/mcsee/code-smell-02-constants-and-magic-numbers-obb %}

{% post https://dev.to/mcsee/code-smell-105-comedian-methods-55h0 %}

# More Info

[Wikipedia](https://en.wikipedia.org/wiki/Cowboy_coding)

[Cowboy Coder Description](https://www.linkedin.com/pulse/software-development-cowboy-coding-hakan-atba%C5%9F/)

# Disclaimer

Code Smells are my [opinion](https://dev.to/mcsee/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned-1n3a).

# Credits

Photo by [Taylor Brandon](https://unsplash.com/@house_42) on [Unsplash](https://unsplash.com/photos/3HmP1kOdACU)
  
* * *

> 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_
 
{% post https://dev.to/mcsee/software-engineering-great-quotes-26ci %}

* * *

This article is part of the CodeSmell Series.

{% post https://dev.to/mcsee/how-to-find-the-stinky-parts-of-your-code-1dbc %}