Independent & Effeciency :: Programming Concepts

This is a discussion on Independent & Effeciency :: Programming Concepts within the C++ Programming forums, part of the General Programming Boards category; Hi. I began learning C++ and programming for the first time about ten months ago. I love programming using C++. ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    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
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    hey kuphryn, were did you learn WinSock API from? i have searched the internet for tutorials, but i havnt found any good ones =[
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Hey. I recommend Network Programming for Microsoft Windows, Second Edition by Anthony Jones, Jim Ohmund.

    That learned Winsock API from that book. It is an excellent Winsock reference.

    http://www.amazon.com/exec/obidos/AS...235171-8876613

    Kuphryn

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    194

    Re: Independent & Effeciency :: Programming Concepts

    Originally posted by kuphryn
    Hi.

    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


    Cramming everything together in a few lines of code is usually not as efficient as it seems. If your example about a math function using ints or doubles, if you made a function that handled both types, then every time you called the function an extra if statement would need to be executed, but if you wrote 2 seperate functions the programmer would decide which version to use.


    version A
    int i=0;
    while( i < strlen(s1) , s2[i]=s1[i] ) i++ ;

    version B
    int i=0;
    while ( i < strlen(s1) )
    {
    s2[i] = s1[i];
    i++;
    }

    Both versions of code do the exact same thing, and may even may produce the same output from the compiler (or very close).
    It is easier to understand the second version, so future programmers may update/change the code. So if future developers cant figure out whats goin on, of if you forget what it is doing it will be easier to figure out

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Good point. Thanks.

    I prefer independent programming style over "cramming" too.

    Kuphryn

  6. #6
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317

    Re: Independent & Effeciency :: Programming Concepts

    Originally posted by kuphryn
    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.
    If that is the case, dunno... I find myself never getting consistent on my code style when it comes to efficiency. Sometimes, with a given algorithm, it just looks better if I cram it. Say, create a template function (reading from your example). But with my next problem, it can feel about right to overload. I think the bottom line here is readability, like RPiMatty pointed out.

    Usually. on a first stage, I don't give much thought to efficiency unless I know i'm working on some resources hog piece of code. Mind you, this is not the case for C++ yet, as I'm still learning it. But I have an extensive programming experience ranging for 12 years now and what i've seen from C++ until now makes me believe that should also be the case.

    Actually, efficiency may as well be your enemy if you, like me, work behind tight schedules; sometimes impossible ones. Efficiency comes at the cost of development time. On all these years I always aimed for code efficient projects. When I had the chance, I made sure I got into it after release and changed what I could (making it less readable, but more efficient). It would then become an optimised next version.

    This is in my opinion, the best course of action. Aim for it, but don't depend on it.
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How write address family independent code ? hints ? experiences ?
    By fredy100 in forum Networking/Device Communication
    Replies: 0
    Last Post: 03-31-2008, 11:17 AM
  2. Links to Software Engineering concepts
    By alois_rone in forum Tech Board
    Replies: 5
    Last Post: 12-05-2007, 11:56 AM
  3. Replies: 18
    Last Post: 09-12-2003, 10:39 PM
  4. Basic 2d Programming Concepts
    By JoshG in forum Game Programming
    Replies: 4
    Last Post: 05-17-2002, 05:54 AM
  5. Independent streams of pseudo random number generator
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-15-2001, 04:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21