Independent & Effeciency :: Programming Concepts

Started by
7 comments, last by kuphryn 21 years, 10 months ago
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
Advertisement
Well, Since you are using C++ and not C, you have at least two options that I know of:

Look up "function Overloading"

Look up "function Template" s
quote:Original post by kuphryn
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.


Glad you are happy with it.

quote:Original post by kuphryn
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.


Well, it''s strange you have an opinion so early on. I hope you''ve had experiance with other languages.

quote:Original post by kuphryn
What is most important: Independent or Effeciency?


I fail to understand the Relativity of the ''Independant'' Keyword above. {Don''t worry, I have trouble viewing things from a fixed point of view all the time.}

quote:Original post 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.


I have to stop you on this point:
quote: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


That won''t compile if you submit a double to a function that takes a long. You would have to go through the trouble of using a void pointer and flags to get it to compile.

I agree with the AP above, use templates, or my personal preference, Overload the damn thing; for god''s sake man, you''re using C++.

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


I''m sure there are different opinions of "slick" and "safe", but so far I''ve run across "slick" as being "safe" in the general opinion. When I think of slick, I think of solutions that most people wouldn''t have thought of right away, but that way is probably the most efficient, fastest, reliable way. I personally focus on tight, "slick" code. I''ve rewritten alot of STL functionality because I don''t need the overhead of STL.

quote:
-> many lines of code : easier to expand

These are not equal. Look at some articles about "refactoring" at DevX.com Longer code takes longer to wade through. The longer your code, the greater the possibility you will trash it and start over.

quote:
-> few lines of code : difficult to expand

Simply not true. Smaller is easier to follow and easier to correct. I think of expanding as correcting existing code to work with newer code.

quote:
Is it better to use "tricks" and cram everything togetter to make programs smaller and maybe more efficient?


If those tricks are safe, yes. You keep your footprint small and you will have better perfromance on a wider range of computers. Remember the Celeron? It had about 1/4 of the pentium II''s cache! Imagine the page faults running bloatware!!


The two examples that you stated can in fact be solved very nicely with the methods that AP posted.

In general, C++ is so powerful that in many (and I mean many) situations it allows you to write code that is short, efficient, safe and elegant at the same time. No offense, but you seem to not know an large part of C++. Once you learn more about this language, you''ll know how to use it more efficiently. If you haven''t done so already, investigate STLport (whose debugging capabilities are far better than those of MSVC STL implementation) and Boost. Also, read at least one "serious" book on C++ (I read Stroustrup''s The C++ Language).

Which program aspect is the most important one depends on the person and on the program. I would say that safety and correctness are the most important: if your program doesn''t work correctly or at all, it''s useless. If you write code that you plan on reusing later in your own applications or giving it to someone else, then reusabilty should be a high priority. You should make as few assumptions as reasonably possible and provide general methods that can be tailored to future needs.

If performance is the main concern, then readability and portability may be sacrificed. Remember to add as many debug checks, including ASSERTs on everything that can possibly go wrong, even in performance-critical code, as these mechanisms are debug-only. Also keep in mind that it is often better to optimize algorithms, not individual instructions.

For someone who doesn''t know a considerable part of the language, many if not most optimizations are premature. You should spend time learning the language until you master it, because you can and most likely will find out ways to radically improve your code, so that optimization that you thought about earlier would be not needed at all.

I used to programming with "tricks" a long time ago. I stopped when I spent half a day debugging a problem which resulted from my own stupidity: I merged two statements into one with a comma operator and after I changed some code around that line, the order in which the merged statements were executed had to be reversed. So after finally discovering this bug I said to myself that no "trick" is worth so much debugging time, and I write much cleaner and easier to understand code now.
---visit #directxdev on afternet <- not just for directx, despite the name
Good pointers. Thanks.

I am current studying Java because I have to for a class. C++ is many times more powerful.

Kuphryn

Andres Manggini made a very good point.

http://www.codeproject.com/script/comments/forums.asp?forumid=1647&select=203155#xx203155xx

Kuphryn
quote:Original post by kuphryn
I am current studying Java because I have to for a class. C++ is many times more powerful.


Keep in mind that in general Java and C++ have different programming styles. A solution that is optimal for Java might not be optimal for C++, and vise versa. While the basic concepts are the same across most, if not all, languages, once you get into more advanced stuff the language plays a huge role in how you design your program.
---visit #directxdev on afternet <- not just for directx, despite the name
Good point. Thanks.

Kuphryn
I''m Impressed.
The road may be long, wind may be rough. But with a will at heart, all shall begone. ~savage chant

This topic is closed to new replies.

Advertisement