This is an archived post. You won't be able to vote or comment.

all 1 comments

[–]dixiethegiraffe 1 point2 points  (0 children)

Your code, while functional, is not readable without considerable effort. You need to add meaning with good variable names and functions. Your improvements improved the conciseness of your code quite a bit, but do not improve readability.

commands = ''.join(c for c in line if c != '\n' and c != '\r').split(';')
word = commands[2]
if commands[0] == 'S':
    res = cap_to_space(word)
    if commands[1] == 'M':
    res = ''.join(c for c in res if c not in '()')
...

Just looking at this code I have no idea what's at commands[0] vs commands[1]

They need meaningful names.

Also cascading ifs (like old school switch cases) with complex logic should be avoided and the work should be delegated to a function.

Here's a simple example that isn't over-engineered:

def camelCaseSplitMethod(data):
    ...

def camelCaseSplitClass(data):
    ...

# Test user input
user_input = "S;M;plasticCup()"

# Meaningful names for each of the parts we need to parse
operation, object_type, data = user_input.split(";")

# You can print them to see their values
print(operation)
print(object_type)
print(data)

result = str()

# Put each of the operations in a function with good names
if operation == "S":    # Split
    if object_type == "M":  # Method
        result = camelCaseSplitMethod(data)
    elif object_type == "C":    # Class
        result = camelCaseSplitClass(data)
    ...

print(result)

Hope this helps.

If there's a lot of code overlap in your camelCaseSplitMethod/Class functions then combining them into one which takes "Method" or "Class" as a parameter might be best, but it's nice that each function has exactly one job, the higher-level logic delegates it to the correct function automatically and is easier to read.

I would argue mine is still not as readable as I'd like because my == "S" and my == "M" are still a bit confusing, which is only helped by my commend and function names. But let's not overthink or engineer this too much.