Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am trying to learn how to tell if my code will be better off creating a class as opposed to using many functions. I am a new Python programmer trying to get the hang of when classes should be implemented.

Determines whether the username is valid or invalid:

def is_good_user(user):
    is_user_good = True
    if user in database:
        print "Sorry! Username is taken! Create another."
        is_user_good = False
    if not 5 < len(user) < 25:
        print "Username must be at least 6 characters!"
        is_user_good = False
    if is_ascii(some_string="") is not True:
        print "Please create username that follows ASCII formatting."
        is_user_good = False
    return is_user_good

Determines whether the password is valid or invalid:

def is_good_password(password):
    count_upper, count_lower = 0, 0
    for characters in password:
        if characters.isupper():
            count_upper += 1
        if characters.islower():
            count_lower += 1
    is_password_good = True
    if len(password) <= 10:
        print "Password is too weak, must be more than 10 characters long!"
        is_password_good = False
    if set(database).intersection(password):
        print "Password Must Contain Alphanumeric Characters!"
        is_password_good = False
    if count_upper < 1 or count_lower < 1:
        print "Password must contain at least one uppercase and one lowercase character!"
        is_password_good = False
    return is_password_good

These two functions are amalgamated into one function to create a username and password:

def create_user(database):
    while True:
        user = raw_input("Enter a New Username: ")
        if is_good_user(user):
            print "Great Job! Username Successfully Created! Time to Create a Password!"
            break
        print "That's not a good username, please try again!"

    while True:
        passcode = raw_input("Enter a New Password: ")
        if is_good_password(passcode):
            print "Welcome! Username & Password successfully created!"
            break
        print "That's not a good password, please try again!"

    database[user] = passcode
    make_digest(password=passcode, salt=None)
    dump_data()

Should I add in a class for readability of my code? Being new at Python, I'd like to take up the challenge if adding a class is necessary.

share|improve this question
1  
Welcome! Please do not roll back edits unless they change the meaning of the post significantly. In this case, the boldface was removed, which isn't necessary for all text. We also don't like "thanks" in posts as that is noise. To show your thanks, upvote helpful answers after earning 15 rep. –  Jamal Mar 1 at 23:32
    
I made some grammatical corrections. What would make this post appropriate for the community? @Jamal –  Stephen Estrada Mar 1 at 23:34
    
Grammatical corrects are absolutely okay. It was just not made clear as the boldface was added back. –  Jamal Mar 1 at 23:35
    
The code as posted isn't complete: database appears only as a local in create_user, but not as a global or other nonlocal for the is_good_* functions. –  Michael Urman Mar 2 at 14:58
add comment

1 Answer

It's a design choice. A class is "meant to be" a collection that encapsulates functionality and "behavior" in code. The purpose is to expose specifically an interface where a class's instances can live on their own.

A function/method is an independent piece of code that in theory "shouldn't" relate to other pieces of code outside of it.

So for the example you've described, you could create a class named "User". And user can include these functions as it's members. This does make it more readable and easier to debug.
However do not take unrelated functions and dump them into a class just cuz! Examples are utility type functions, they could be kept in the vicinity of each other but should not be clubbed together as a class.

An additional benefit of classes is that they can be Inherited from. So more complex classes can inherit the basic functionality and add to it, while using the features already in the base class. You can achieve a similar effect by having "higher" versions of functions that calls more basic level functions. There again at least in my design perspective, I make a class where the whole functionality will be inherited as opposed to functions that are stand-alone.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.