1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Function vs Class

Discussion in 'General Programming Chat' started by Divinityfound, Feb 3, 2011.

  1. Divinityfound

    Divinityfound Registered Member

    Joined:
    Feb 15, 2010
    Messages:
    82
    Likes Received:
    24
    Occupation:
    Software Engineer Developing Financial Software
    Location:
    Omaha Nebraska
    I know a handful of programming languages... but I keep running into functions and classes in all of them, and from what I've gathered, they are pretty much the same thing...

    I initially learned functions first, and then decided to pick up classes for the sake of knowing it and so I can tell employers I can do it... but the problem I have now is seeing the practicality of classes.

    The most I can see for classes is organizing your functions and programs to make it more readable and help navigate your own code... but putting in comments in the code does that just as well. So I guess in the end I am wondering...

    Whats the difference?
    What do you use?
    Which do you use for which instances?
    Rules of Thumbs to follow?
     
  2. blakamia

    blakamia Junior Member

    Joined:
    Jan 25, 2010
    Messages:
    162
    Likes Received:
    343
    You can initialize classes to objects usually, which allow you to abstract and organize your data in infinite ways. A trick I like to do with my scrapers is

    Class HarvestedData

    public HarvestedData(string html)
    {
    // Extract data from html and put into local variables
    }

    public string GetName()
    {
    return this.name;
    }

    public string GetData()
    {
    return this.date;
    }

    public string ToString()
    {
    return this.name + " " + this.date;
    }

    etc...

    then you can put all HarvestedData objects into a list. When you want to export it you just do foreach to iterate through list, call ToString on each HarvestedData object, accumulate everything into a StringBuilder, then save StringBuilder to file when foreach is done iterating. Clean way to organize program :)
     
    • Thanks Thanks x 1
  3. RevenueDollars

    RevenueDollars Newbie

    Joined:
    Feb 3, 2011
    Messages:
    12
    Likes Received:
    0
    ^^^ thanks

    This helped me out with some stuff im working on.
     
  4. Divinityfound

    Divinityfound Registered Member

    Joined:
    Feb 15, 2010
    Messages:
    82
    Likes Received:
    24
    Occupation:
    Software Engineer Developing Financial Software
    Location:
    Omaha Nebraska
    So it is all about how you want to organize your scripts really... and re-usability --- Can be done with functions, but it doesn't make sense to exactly save functions where as classes it makes more sense.

    I think I got a better idea now, but any more suggestions or thoughts or takes would be appreciated, information is good and I is greedy for quality suggestions :D
     
  5. blakamia

    blakamia Junior Member

    Joined:
    Jan 25, 2010
    Messages:
    162
    Likes Received:
    343
    Well, one school of thought I hear a lot is:

    1) Code current project really fast
    2) When it's done, Refactor/Modularize/Reorganize mercilessly.

    Break down the code into smaller and smaller chunks, and make as many reusable classes as you can. Keep reorganizing your existing code until it's fully modularized. The reason I say this is you'll be amazed at how reusable your code is.

    For example: Other day, friend asked me to code something that went to EzineArticles and GoArticles, searched for random article, scraped it, spun it with TBS, and then posted it to wordpress. Since I already had modules to all this, it literally took me 20 minutes to put together the application. I just imported all modules I needed, then did this:

    Article ezineArticle = RandomEzineArticleScraper(keyword);
    string title = TBSSpintaxGenerator(ezineArticle.GetTitle(), tbsusername.Text, tbspassword.Text);
    title = Spinner.Spin(title);
    string body = TBSSpintaxGenerator(ezineArticle.GetBody(), tbsusername.Text, tbspassword.Text);
    body = Spinner.Spin(body);
    string tags = ezineArticle.GetTags();
    WordpressHtmlPoster.Post(username.Text, password.Text, blogurl.Text, title, body, tags, true);

    ... and that's it. Now you be the judge of how powerful that is :)
     
    • Thanks Thanks x 2
  6. Divinityfound

    Divinityfound Registered Member

    Joined:
    Feb 15, 2010
    Messages:
    82
    Likes Received:
    24
    Occupation:
    Software Engineer Developing Financial Software
    Location:
    Omaha Nebraska
    Okay... so basically you are saying that many many many problems and functions are VERY recyclable and more often than not you are wasting your time and gonna be banging your head a lot more than necessary, and if anything else you can save a lot of memory by simplifying it with classes?

    But damn, that is impressive! Makes me think about making my own modules for stuff like that... hmm. And I assume that most compilers take into account your own modules that you constructed and includes it into your project?
     
  7. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,468
    Likes Received:
    10,143
    • Thanks Thanks x 1
  8. Divinityfound

    Divinityfound Registered Member

    Joined:
    Feb 15, 2010
    Messages:
    82
    Likes Received:
    24
    Occupation:
    Software Engineer Developing Financial Software
    Location:
    Omaha Nebraska
    I think I really am getting the idea behind Classes, thank you BHW!! ^.^
     
  9. Nuz25

    Nuz25 Junior Member

    Joined:
    Aug 20, 2010
    Messages:
    129
    Likes Received:
    100
    Occupation:
    I'm a student
    As soon as you work on big projects it becomes really really hard to code everything using just routines(functions) and make them interact with each other.

    That's why we use classes, because it gives you the opportunity to build the architecture of your program on a higher level of abstraction. Which makes it easier to understand for you and the team that will work on it. This is crucial when you're a software engineer that builds architectures for programs that will be built by 100s of programmers.

    That's why I think it's better to use classes.
     
    • Thanks Thanks x 1
  10. madoctopus

    madoctopus Supreme Member

    Joined:
    Apr 4, 2010
    Messages:
    1,249
    Likes Received:
    3,498
    Occupation:
    Full time IM
    OMG! You must be a really bad coder if you just said that. No offense but saying that a car is the same as a bunch of wheels with toothpicks between them is... not what an auto engineer would say.

    Maybe you should start reading a book on the basics of programming so you learn why a class is not a function.

    I guess design patterns, interfaces, factory classes, method overriding, etc. makes no sense for you at all.


     
  11. Divinityfound

    Divinityfound Registered Member

    Joined:
    Feb 15, 2010
    Messages:
    82
    Likes Received:
    24
    Occupation:
    Software Engineer Developing Financial Software
    Location:
    Omaha Nebraska
    1) Fuck you.
    2) This is a place to learn, not demean.
    3) I have and I didn't find the answers I look for.
    4) You don't know what I know.
    5) We all have a place to start and learn. I simply state what I have observed, and if you want to be helpful, say something constructive.
    6) If you have nothing nice to say, bugger off.
     
  12. Monrox

    Monrox Power Member

    Joined:
    Apr 9, 2010
    Messages:
    615
    Likes Received:
    579
    Well tbh a class is indeed just a way to organize a bunch of functions. It was created to bring some order in the source code, and as said above to make team work easier.

    You can have one humongous function or a humongous class with multiple functions. Personally I don't like classes because they never end up versatile enough. Everytime I need to reuse something, I need a bit different output. In terms of code this means rewriting the whole thing because at least the error handling becomes totally different and some newly created bug emerges when least expected.

    The only useful classes I created are those sending windows messages to windows to click buttons, toolbars and stuff and guess what, they don't work with WPF.

    But if you have the resources or the need to turn everything into a class that is not a bad thing too. Java and the .net languages for example are 80% like that: thousands of ready made classes that the user (programmer) can use. The cost is speed. If you do it yourself for a given goal however you may very well get a 50% or more performance increase over some generic image resizer class.

    In short, if you are not copy&pasting code like crazy from old apps to new apps you don't have to worry that you don't have any classes.
     
    • Thanks Thanks x 1
  13. madoctopus

    madoctopus Supreme Member

    Joined:
    Apr 4, 2010
    Messages:
    1,249
    Likes Received:
    3,498
    Occupation:
    Full time IM
    I recommended a book on programming. If you didn't understand from what you have read, read a different one.

    http://en.wikipedia.org/wiki/Object-oriented_programming
    http://download.oracle.com/javase/tutorial/java/concepts/ (good explanation with pictures and real life concepts)

    PS: Why are you so jumpy? Chill... listen to your breath... feel the air touching your skin... mmmm... relax

    PPS: I just don't think a BH SEO forum, or a forum in general is the best way to learn coding. Get a good book, one explained in simple terms and read it. If you don't understand something, read about the same thing in other books so you give your brain some food and different connections.

    PPPS: I also think you should Google stuff before you ask people about them. You will find quite a few answers that are clear. Search "OOP", "object oriented programmin", "oop tutorial", "oop concepts", "oop insert-programming-language-name-here", etc.
     
    Last edited: Feb 4, 2011
  14. Divinityfound

    Divinityfound Registered Member

    Joined:
    Feb 15, 2010
    Messages:
    82
    Likes Received:
    24
    Occupation:
    Software Engineer Developing Financial Software
    Location:
    Omaha Nebraska
    This is far more helpful than your previous post. Thank you.

    I just wanted to see the practicality of using classes and why I would want to use classes, and from the information that has been provided by everyone and examples, explanations, articles, and then putting what I saw into use, I've managed to see a massive improvement in some of my programs, making it FAR FAR FAR easier to read, and I can think of half a dozen other projects that would use similar methods to achieve a task.

    I already know how to program to accomplish a variety of tasks or figure out how to solve some other problems, so calling me a bad programmer is more annoying than anything else, not helpful. I already knew how to use them, I just didn't understand WHY I would want to use them.

    But at this point, I feel much better about using classes. I just wanted a good answer for a question that has been burning in my head, since I couldn't get a very good answer out of someone with a BS in Comp Sci and my father who has has spent the last decade doing this. I learned it PURELY for the sake of knowing it, but always found functions more useful and faster till I saw everything in a different context.

    Regardless, thank you for helping. :)
     
  15. tacopalypse

    tacopalypse Executive VIP Jr. VIP Premium Member

    Joined:
    Nov 30, 2009
    Messages:
    980
    Likes Received:
    2,485
    Home Page:
    classes are used as templates to instantiate objects.
    objects contain collections of related functions as well as a bunch of other stuff.

    so in short, classes contain functions. they are not the same thing. ;)
     
    • Thanks Thanks x 1
  16. squark

    squark Junior Member

    Joined:
    May 22, 2008
    Messages:
    156
    Likes Received:
    1,206
    Everybody seems to be dancing around the real use of classes...

    They're not just a different method to organize the same code. They allow for fundamentally different programming because of encapsulation, abstraction, and inheritance.

    Encapsulation is simply the ability to maintain state independent of the individual functions that make up a class. This state can be stored as either properties or internal private variables that exist outside the function and method bodies. This encapsulation also enables you to have multiple instances of a given class that each have their own data and properties. With procedural programming your would have state variables floating all over the place to accomplish the same thing.

    Abstraction and inheritance go hand-in-hand. The ability to define a base class that implements certain core functions that can then be extended through inheritance (or a number of other methods such as AOP). This is perhaps the most compelling feature of class-y programming (OOP). The level of code interaction and re-use this enables is awesome.

    The advantages of real OOP are far more than just personal preference or consolidation of code. Once you get the hang of it you'll wonder how you ever did it the old way.

    Most of the higher-level languages moved to OO years ago. The holdouts typically are the scripting and web languages that still give you an option. Specifically I'm thinking of PHP and javascript.

    And even though javascript isn't a true OO language, if you've ever tried to write a huge library procedurally vs. classy (mostly with closures and objects in javascript) it's night and day. Once I started coding jQuery-style I can't hardly stand to do it any other way. It's so much better.

    P.S. I don't know specifically what language you're dealing with, so I can't offer language-specific assistance. But, in general, I say you would be well served to jump in and learn more about what classes can do.
     
    • Thanks Thanks x 1