r/PythonLearning • • Sep 18 '26

Showcase After taking in everyone’s thoughts and criticisms from my last post and using them, i managed to make the code much easier to read and more intuitive. Thank you everyone who gave me amazing tips on how to create more readable code!

Right pic is before and left pic is after

88 Upvotes

25 comments sorted by

•

u/Sea-Ad7805 Sep 18 '26

Run this program in Memory Graph Web Debugger to see the program state change step by step.

→ More replies (2)

4

u/Ender_Locke Sep 18 '26

you really don’t need is valid. you can just check the opposite of your other vars. you also don’t need the vars , they are making your code more lines you could just check the calc

if ya wanna have some more fun add in a password and learn about hashing it properly😊

0

u/abt-dabest Sep 18 '26

I thought about removing is valid with your exact idea actually but then I realised it can get really messy if you wanna add more rules in the future and it becomes harder to maintain.

Also people told me about how its better to use pseudo code for your style and it has helped me a lot so i decided to substitute fewer lines for an easier to read code. Im sure it’s all a matter of preference though.

As for the password thing it sounds like a really good idea! Definitely doing this next! Thx for the tip!

1

u/iplayconnections Sep 18 '26 edited Sep 18 '26

realised it can get really messy if you wanna add more rules in the future and it becomes harder to maintain.

It's less messy without the is_valid variable that gets reassigned. If you add more rules, you'd add the rule to one is_valid check at the end. That's one place to look instead of every if block where you mutate is_valid.

It's kinda clunky though. Having a big if statement at the end to determine validity feels wrong. You can bring in some help with a Rule class. It would have the condition (that's what you are currently assigning to each rule variable) and message (what gets printed when not satisfied). You define all your rules with a terser syntax and keep their attributes tied together that way. Then, if you really want it compact but still readable, you can assign them all inline to a list, which conveniently serves as your validation iterator.

Like this:

rules = [
  Rule(len(username) > 12, "Username must not exceed 12 characters."),
  # more rules...
  Rule(not username.isAlpha(), "Username must only contain letters.")
]

for rule in rules:
  if rule.condition:
    print(rule.message)

if not any(rule.condition for rule in rules):
  print("You have successfully created a username!")

Sticklers would argue that this results in an additional iteration over your rules. I argue that the enhanced readability is worth it.

You'd have to write the Rule class, which I didn't include. It's just another way to organize things. For this small example it's overkill. It definitely makes things more manageable when you have a big number of cases. As programmers it's up to us to assess what choice is best given what we currently know about our rules and data and what we expect to happen to our rules and data in the future.

Im sure it’s all a matter of preference though.

That you already know this puts you way ahead. There's always somebody with a different preference. But I will add that which preferences win out often comes down to the cold, hard facts you can use to back up your choices. Keep being critical!

0

u/abt-dabest Sep 18 '26

Wow, this is all a huge info dump for beginner me! I got the gist of it but I will be sure to save this for later when I have a better understanding of coding in general. Thanks!

1

u/CoolLog1908 Sep 18 '26

Hey again!

Your refactoring looks a lot better than the previous post! Something you did really well is describe what you are checking, like ```is_too_long``` or ```is_too_short```.

Something I would recommend looking into is optimizing your ```is_valid = False``` flag. It's hard to get into ways to make it better without providing concepts you are probably not familiar with yet, especially since you want to retain and show the user all the reasons why their username is not allowed. However, something to keep in mind as you move forward is that the more times you update a flag throughout different parts of the code, the harder it will be to sort out bugs later down the road - especially logic bugs, which can be quite difficult to resolve.

All that said, I love that you took everyone's input, and made great changes!

0

u/abt-dabest Sep 19 '26

I learned a ton from the comments on my previous post! Not just about how to write code but how it can be made cleaner and easier to read.
I will save the optimization method for when I’ve learned more and can incorporate it to my programs.

1

u/EmotionalStock4143 Sep 18 '26

this looks more understandable and less complex, but i havent seen the previous post

2

u/XusKiz Sep 18 '26

Try using short-circuit evaluation and make the initial value of is_valid variable fasle then if everything is ok change it to true
Using error handling would be a good idea if you have reached the point in the course

1

u/[deleted] Sep 18 '26

Just a beginner's question, how to avoid thousands of "if/else" if there are a lot of conditions in a program ?

1

u/Sea-Ad7805 Sep 18 '26

Write a separate function for each condition that returns True or False, then use any() and/or all() to combine them.

1

u/jpgoldberg Sep 19 '26

Nice. It is clear and well organized. It is also easy to modify if you later need to add, remove, or change the requirements.

1

u/Neither_Berry_100 Sep 19 '26

I would line up the equals. Also I would collapse the if statements to single lines and line them up if this is possible in python. Much easier to read.

1

u/TheKnottyOne Sep 19 '26

I’d (personally) instantiate is_valid as False and only pass it as True when it passes a criteria. It’s less to write and I’m lazy 😂

1

u/TheKnottyOne Sep 19 '26

Follow-up: a decent alternative to IF statements is a switch (sometimes referred to as a switch/case). I don’t see them often these days, but I do appreciate when they’re used. Syntax is a little tricky at first, but same concept as IF/ELSE

1

u/aroberge Sep 19 '26

Nice. You could also define

rule2 = username.count(" ") == 0

and use

if not rule2: ...

like you did for rule3.

If you really wanted to push this to the extreme, you could write

rule1 = 4 < len(username) <= 12

and

if not rule1:
     print("Username must have at least 4 characters and not exceed 12 characters.")

Thus mapping your rules 1 to 1 with your tests.

0

u/[deleted] Sep 18 '26

[deleted]

1

u/abt-dabest Sep 18 '26

Im definitely not upset at criticism! I know that I am a beginner(around 6 hours of total learning) and that almost everyone here has more experience coding than me. If you look at my last post i looked at every criticism and tried to incorporate it into my learning. I was really happy that people took time out of their day for no gain of their own just to teach a stranger how to get better! This process has been really helpful to me!

1

u/ProgramBrave718 Sep 18 '26

Yeah, u are right 👍

2

u/[deleted] Sep 18 '26

[removed] — view removed comment

0

u/abt-dabest Sep 18 '26

Tysm for the encouraging words! I really hope i can use this alongside my mechanical engineering knowledge someday!