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
Re: Independent & Effeciency :: Programming Concepts
Quote:
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
Re: Independent & Effeciency :: Programming Concepts
Quote:
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.