Click to See Complete Forum and Search --> : Independent & Effeciency :: Programming Concepts


kuphryn
June 7th, 2002, 08:32 PM
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

Paul McKenzie
June 7th, 2002, 09:59 PM
is it better to write two separate functions where each performs a task for a specific data typeWhat's wrong with a function template?

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

kuphryn
June 7th, 2002, 10:18 PM
Okay. Thanks.

Kuphryn

frdonato
June 7th, 2002, 11:26 PM
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

BillWilson33
June 8th, 2002, 02:53 AM
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.

kuphryn
June 8th, 2002, 10:50 AM
Thanks.

BillWilson33: Great summary!

Kuphryn

Anthony Mai
June 10th, 2002, 07:16 PM
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.

bernhard_r
June 11th, 2002, 07:06 AM
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..