OriginDev

OriginDev

0%
Don't Be That Person Who Writes "One-Liner-Code-Full-of-Encrypted-Symbols"... I Can't Read Your Code!

Don't Be That Person Who Writes "One-Liner-Code-Full-of-Encrypted-Symbols"... I Can't Read Your Code!

July 01, 2024

In a collaborative environment, nobody but you thinks compact, mysterious code that does the same thing with the same performance as something easy to read is genius.

Example 1: Finding the Sum of Squares of Even Numbers

"Wow!" Version

```python

result = sum(x**2 for x in n if x % 2 == 0)
```

"What I Would Like to Read in My Code Review" Version

```python

# Filter the even numbers
even_numbers = [number for number in numbers if number % 2 == 0]

# Calculate the square of each even number
squares_of_even_numbers = [number**2 for number in even_numbers]

# Calculate the sum of the squares
result = sum(squares_of_even_numbers)
```

Example 2: Filtering and Transforming Data from a Dictionary

Encrypted Version

```python

result = {k: v**2 for k, v in d.items() if is instance(v, int)}
```

Readable Version

```python

# Initialize an empty dictionary to store the results
result = {}

# Iterate over each key-value pair in the original dictionary
for key, value in data.items():
# Check if the value is an integer
if is instance(value, int):
# Calculate the square of the integer value and add it to the results dictionary
result[key] = value ** 2

```

Readable code is essential for long-term maintenance and collaboration. By writing code in a clear, step-by-step manner with well-named variables, you ensure that your code is accessible and understandable to others. And you make more friends ❤️.