Independent & Effeciency :: Programming Concepts

Sharky Forums


Results 1 to 9 of 9

Thread: Independent & Effeciency :: Programming Concepts

  1. #1

    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
    Reef Shark mefisto3's Avatar
    Join Date
    Jul 2001
    Location
    Melbourne, Vic, Australia
    Posts
    429
    i think that your example there is not very good. if u have a math functions then it cant take both int and double as a parameter. u could of course pass both (as separate parameters) and have a flag or something indicating which one is to be used, however, if u r going to do that and use if statement then u r still writing code twice. so the answer is pretty clear: write 2 functions. make your code easier to maintain and to understand. not to mention faster.

    if u r refering to classes, u could pass a super class object. but then the function should be able to handle all classes extending the super class. if u want to perform a specific operation on an extended class object then write it specific for that class.

    i dont think u make a program more efficient by making it smaller. it could have hundreds of if statements etc, which makes it less efficient. by craming the code u make it hard to debug, hard to modify and understand, slower etc.

    if u have more than 4, 5 nested loops or switch and if statements then u should probably consider splitting the function into few smaller chunks. think about what u r going to code. try to make it simple and easy to follow. make sure that when u come back to your program in a few weeks u will be able to understand what is going on straight away.

  3. #3
    Catfish df's Avatar
    Join Date
    Sep 2000
    Location
    Lexington VA(USA) + Melbourne(Aus)
    Posts
    146

    Re: Independent & Effeciency :: Programming Concepts

    Originally posted by kuphryn
    Hi.
    -> 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.
    personally, I dont worry about either. you cant halve the size of an enterprise application with tricks, nor will overcoding make it easier to read.

    i know you were talking only of 1 function, but what does one function mean in the context of the app. nothing, if you cram it with tricks and remove two lines of code that equates to 10 bytes, which you dont see since the compiler rounded the code segment to a paragraph... etc.

    the best thing to do is neither size nore speed, its efficiency. desk check the function and see how well the flow of it works.

  4. #4
    Hammerhead Shark
    Join Date
    Sep 2000
    Location
    Edinburgh,UK
    Posts
    1,188
    it cant take both int and double as a parameter
    You could use a union of an int and a double, but that would be unpleasant
    Last edited by stoo; 06-08-2002 at 11:31 AM.
    Stoo

  5. #5
    Okay.

    I prefer independent programming style over "cramming" too.

    Kuphryn

  6. #6
    Expensive Sushi
    Join Date
    Apr 2001
    Posts
    36

    Lightbulb

    You don't have to make the two functions totally independant if you use polymorphism here. There would be two functions, one that accepts a double and another that accepts an integer. You can convert an integer to a double without losing any precision, but you can't convert a double to an int as you will lose precision. So, what you need to do is make the function that accepts the integer take that integer and convert it to a double. After converting it to a double, call the function again on the double value. The function that accepts the double would do the actual work. Here's a quick example (I didn't test this, so don't pounce on me if it doesn't work 100% right... it's purely for illustrational purposes):

    public class Polymorphism
    {
    public double square (double d)
    {
    return d * d;
    }

    public int square (int i)
    {
    return square(Double.parseDouble(i));
    }
    }

  7. #7
    Goldfish pcbuyer's Avatar
    Join Date
    Jun 2002
    Location
    Philadelphia
    Posts
    96
    Ok. I think you are reffering to loose coupling and efficieny. Loose coupling is always good. After you write a couple large systems you will find that programming up front for efficiency is an exercise in futility. Make the code work first. And then if you have efficiency problems let a profiler tell you where your bottlenecks are. If you programmed up front with loose coupling and high cohesion you will find that the changes for efficiency will generaly be easy to make later.

  8. #8
    Tiger Shark
    Join Date
    Feb 2001
    Location
    Satan Country
    Posts
    564
    Efficiency in 90% of your code is of no use to you and there are one or two hotspots.

    More important, the algorithm must be correct...if you have the correct algorithm other improvements eek out another 10% generally, whereas the wrong algorithm will complete in hundreds of years in many cases.
    I'm half Scottish and half French.

    I surrender to alcohol.

  9. #9
    Thanks.

    That is an excellent point. I agree that getting the correct algorithm is imperative.

    Kuphryn

Posting Permissions

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