#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    55
    Rep Power
    0

    Functions and Classes


    Can something that is basically a function be a class?

    When a user registers on my website, one of the things I need to do is check to see if the Username already exists.

    Should this just be a function (method) in a class, or can I break it out and make it its own class so that it can be re-used by others?


    Debbie
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    By function I assume you mean method.
    All code must be in a class. You can have a class with a single method.

    can I break it out and make it its own class so that it can be re-used by others?
    Not sure what "break it out" means? Can you explain?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    55
    Rep Power
    0
    Originally Posted by NormR
    By function I assume you mean method.
    All code must be in a class. You can have a class with a single method.


    Not sure what "break it out" means? Can you explain?
    The context of my question is this...

    Let's say I decide that I need a "RegistrationSevice" class. (Not sure what you think about that. An annoying number of people say don't do that?!)

    And this class needs to do the following things to register a user...

    - checkRequiredFieldsComplete()
    - isValidEmail()
    - isValidPassword()
    - doEmailsMatch()
    - doPasswordsMatch()
    - isEmailUnique()

    Maybe I see value in "isValidEmail()", "isValidPassword()", and "isEmailUnique()" and want to be able to somehow re-use them later.

    Would it be a good design decision to create classes like:

    - EmailValidator

    - PasswordValidator

    - UniquenessValidator



    Debbie
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Whoops, you've sort of wandered off of methods and classes to another topic related to designing an application.

    I'll leave that for someone else.
  8. #5
  9. Santosh Vaza
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Location
    JHome
    Posts
    356
    Rep Power
    13
    Originally Posted by DoubleDee
    Let's say I decide that I need a "RegistrationSevice" class. (Not sure what you think about that. An annoying number of people say don't do that?!)

    And this class needs to do the following things to register a user...

    - checkRequiredFieldsComplete()
    - isValidEmail()
    - isValidPassword()
    - doEmailsMatch()
    - doPasswordsMatch()
    - isEmailUnique()

    Maybe I see value in "isValidEmail()", "isValidPassword()", and "isEmailUnique()" and want to be able to somehow re-use them later.

    Would it be a good design decision to create classes like:

    - EmailValidator

    - PasswordValidator

    - UniquenessValidator
    Yes, consider using JavaBeans , what you are doing is server side validation.
    it would be better if you also do client side validation for that you will have to take a move to javascript or other client side scripting languages, server side and client side validation would be much better.
    Originally Posted by DoubleDee
    When a user registers on my website, one of the things I need to do is check to see if the Username already exists.

    Should this just be a function (method) in a class, or can I break it out and make it its own class so that it can be re-used by others?
    you can do it by executing some sql query against tables but my opinion is to use database side procedures or functions to handle this if your db supports it.

    Comments on this post

    • tfecw disagrees : [0] i'm not sure you understood the question. Your responses don't really match the question.
    no one can become perfect by merely ceasing the act
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Nov 2004
    Location
    Washington DC
    Posts
    2,756
    Rep Power
    1572
    Originally Posted by DoubleDee
    The context of my question is this...

    Let's say I decide that I need a "RegistrationSevice" class. (Not sure what you think about that. An annoying number of people say don't do that?!)
    WHo said that and what reasons did they give you?
    And this class needs to do the following things to register a user...

    - checkRequiredFieldsComplete()
    - isValidEmail()
    - isValidPassword()
    - doEmailsMatch()
    - doPasswordsMatch()
    - isEmailUnique()

    Maybe I see value in "isValidEmail()", "isValidPassword()", and "isEmailUnique()" and want to be able to somehow re-use them later.

    Would it be a good design decision to create classes like:

    - EmailValidator

    - PasswordValidator

    - UniquenessValidator

    Debbie
    'Good design decision' is hard to say. Personally, it sounds like you're on the edge of violating YAGNI

    Don't get me wrong, there is certainly some value in stubbing all this stuff out, esp when it comes to unit testing but you just sort of get a feel for when and when not to do it based on experience.

    Let's expand the design a bit. Say i want to have a registration service, but i don't know what is actually going to handle the authentication/authorization. I would stub all that out so i could pop in LDAP, some SSO Impl, a custom solution, whatever. I'm not 100% sure i would stub out a 'UniquenessValidator' If my password rules were that complex, i would maybe have a PasswordHelper class to delegate all that stuff to.

    Email Validator is an interesting one because it's a more complex problem. How do you validate an email? You can half *** it with a regex, but you don't really know if an email has been validated until you send an email, and they send a response. You def wouldn't want that logic in your Service class.
    Open for extension, closed for modification
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    55
    Rep Power
    0
    Warnings Level: 8
    Reputation Power: 7
    Geesh, what did you do?!


    Debbie
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    55
    Rep Power
    0
    Originally Posted by tfecw
    Who said that and what reasons did they give you?
    A lot of people still feel that classes must be physical things and things which must be nouns.

    I disagree, and from what I've read, more seasoned OO programmers disagree with that limited focus as well.



    'Good design decision' is hard to say. Personally, it sounds like you're on the edge of violating YAGNI[/url]
    How so?

    If anything, a Registration class gets me criticized because it is not re-usable enough?! (Definitely not YAGNI material!)


    Don't get me wrong, there is certainly some value in stubbing all this stuff out, esp when it comes to unit testing but you just sort of get a feel for when and when not to do it based on experience.

    Let's expand the design a bit. Say i want to have a registration service...
    I just appended "Service" to my class name because some people got hung up on the name "Registration" which they said is not a thing but an action or process. (In fact it is a noun and it is definitely an abstract "thing".)


    , but i don't know what is actually going to handle the authentication/authorization. I would stub all that out so i could pop in LDAP, some SSO Impl, a custom solution, whatever.
    You lost me.

    I say "Registration" makes a logical class. Is it extensible, yes. Would I make it an interface or hope to re-use it all ver the place, no. I think it is a YAGNI.


    I'm not 100% sure i would stub out a 'UniquenessValidator' If my password rules were that complex, i would maybe have a PasswordHelper class to delegate all that stuff to.
    No, I was thinking maybe you could abstract a function that checks for unique values and turn it into a reusable class.

    All I was saying is that for those who felt my Registration class was too UN-reusable and procedural, I could break out methods that could be re-usable classes if need be.


    Email Validator is an interesting one because it's a more complex problem. How do you validate an email? You can half *** it with a regex, but you don't really know if an email has been validated until you send an email, and they send a response. You def wouldn't want that logic in your Service class.
    I agree that is a complex challenge and not bullet-proof.

    You are saying I wouldn't want that in my Registration class?

    Why?

    And if so, how would you handle it?


    Debbie
  16. #9
  17. Daniel Schildsky
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    Location
    KL, Malaysia.
    Posts
    1,555
    Rep Power
    1621

    Handling complex registration scenario


    Hmm...seems like an interesting topic to discuss about.

    If you think that a process may be complex enough to have its own attributes, who said it cannot be made into a class? It makes perfect sense to do so.

    As for the individual validators, I think that it depends on your own preference. For a banking software vendor I worked for in the past, they have specific validators for each type of data, like text, name, id, date, number, phone number and so forth. These validators are made into a generic library for use by other products in the same line.

    If you are simply developing a small and simple application, I do not see the need to over complicate the application itself. Some practices may be good for large scale application development or streamlined application vendors may not be suitable for a small scaled and simple application.
    When the programming world turns decent, the real world will turn upside down.
  18. #10
  19. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Oct 2004
    Location
    Brussels
    Posts
    1,001
    Rep Power
    841

    Lightbulb


    Yes, sometimes a function can be a class/object.
    For an introduction of the concept read this.
    Also java functor.
    "Problem" is just a bleak word for challenge. -- Richard Fish
    Javalanche

IMN logo majestic logo threadwatch logo seochat tools logo