CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2002
    Posts
    5,757

    Independent & Effeciency :: Programming Concepts

    Hi.

    I began learning C++ and programming for the first time about ten months ago. I love programming using C++. Whether I am using C++, MFC, or Winsock, programming concepts never change.

    C++ is an extremely powerful programming language and is one of the most extensive programming language. I find myself often trying to decide one programming style and weighing effeciency, effectiveness, reusabiliy, and manageability for future changes.

    What is most important: Independent or Effeciency?

    Here is one example. Let consider there is a function that does a mathematicaly calculation. However, there are different types of data such as int and double. In this case, is it better to write two separate functions where each performs a task for a specific data type, or is it better to use a switch or if/else state inside one function and manipulate data relative to whatever datatype that is passed in? This is just one example. There are many more complicated scenarios.

    Ultimately, my concern here is about "slick" programming or "safe" programming.

    -> many lines of code : easier to expand
    Is it better to have everything independent, i.e. one function for every job and for every data type? This might seem redundant.

    -> few lines of code : difficult to expand
    Is it better to use "tricks" and cram everything togetter to make programs smaller and maybe more efficient? This might seems difficult to expand in the future. What if you want to add a new feature and needs to add something to a function; however, this feature only applies to one datatype or one scenario.

    Thanks,
    Kuphryn

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449
    is it better to write two separate functions where each performs a task for a specific data type
    What's wrong with a function template?
    Code:
    template <typename T>
    T MyFunc(T data)
    { 
      T Answer = data * data;
      return Answer;
    }
    
    int main()
    {
       double d = MyFunc(2.0);  // Calls double version
       int i = MyFunc(2);  // Calls int version
       MyOwnNumber M1;
       MyOwnNumber M2 = MyFunc(M1); // Calls MyOwnNumb er version (operator * is overloaded for the MyOwnNumber class)
    }
    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Feb 2002
    Posts
    5,757
    Okay. Thanks.

    Kuphryn

  4. #4
    Join Date
    Aug 2001
    Posts
    3
    That the big question I saw in all web site / forum

    The thing is C++ is an OO language. It's mean you need to use OO concept when you write program.

    The existence of class, template, polymorphism and abstract type in C++ allow you to do not use the type of construction that you talk about!

    Frank

  5. #5
    Join Date
    May 2002
    Posts
    12
    The important thing about the question is that neither is more or less important intrisicly. The importance of those, as well as other code qualities, is entirely situational.

    There are applications where speed is more important than anything else, even more important than getting the right answer!

    There are other cases where readability is paramont. In other cases robustness it the primary quality.

    In most apps, many qualities share importance in some mix of priority.

  6. #6
    Join Date
    Feb 2002
    Posts
    5,757
    Thanks.

    BillWilson33: Great summary!

    Kuphryn

  7. #7
    Join Date
    Jun 1999
    Location
    San Diego, CA
    Posts
    600
    What's wrong with templates?

    There are N things wrong with C++ templates. Depending on who you ask and a combination of many factors, N ranges any where from 0 to many.

    First thing wrong is what if your development environment simply would NOT allow you to use template? Or maybe in the future when you are ready to port it to a specific system all of a sudden you find you can no longer use templates.

    Second thing wrong is templates are a sort of macros. Macros are bad in a number of ways. templates definitely does NOT aid readability. What does this line of code mean:
    z = x * y;
    You immediate response is this multiplies two integers and give the result to the third one. But under template, there could be a zillion other possibilities. The data type could be as simple as a byte or as complicated as a 1MB class object. Object could be created, destroyed. memories could be allocated, de-allocated, moved, exceptions could be thrown, all along a single code line as innocent as z=x*y. The one line code, when compiled, could literally translate into millions of lines of assembly code.

    When there are two many possible ways to interpret a code line, the code line stops making any sense. Which means the source code is no longer readable. The source code no longer carries any information which may help understand what it attempts to do.

  8. #8
    Join Date
    Apr 2002
    Posts
    19

    it's a typesafe macro..

    a template is a typesafe macro.. and so it works great.. if you've ever used stl you know what i mean...

    at least i like the stl and templates..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured