How to criticize my code and test it for efficiency?

Hello,
I am a self taught coder and I'm not an expert but yet not a beginner. There are a few questions which I cannot find answers for online and are important for me to become a better programmer (since that's what job I want when I'm older - I'm 15). This question is mainly aimed at experienced programmers, or professional ones, but anyone with knowledge on the topics can answer :)

- When I need to code something, what is the process I should go through to design any algorithm(s), and solve the problem in the most efficient/flexible way? At the moment, I just think up a way of solving it and if it works I will use the code in my current project, but as I have found out from some of my larger projects, this definitely is not the way to go!

- How can I criticize my own code? Again, if I write some code that works I'll keep it, but what do you guys do? Go through the code and look out for optimizations or refactor the code? Which leads to my last question...

- How can I test my code for efficiency? What are the best ways of testing algorithms (etc) that I have added to see if they are fast enough? Of course, it will probably involve some kind of timer, but how will I be able to tell if X milliseconds is a good enough time?

If you can help me with any of these (rather vague) questions I'd be delighted! Thanks in advance :)
Hey, um, few key tips:
- It's better to write function definitions in the beginning.
- Keep all necessary structures and variables as globals.
- Avoid use of goto function.
- Go for readability more than functionality, you should be able to change stuff as and when you need.
- Lastly, comments are love, comments are life. Helps you remember stuff when writing a big code.

Hope this helps! :)
Thanks! Yeah, commenting code has always been my weakpoint, I need to do it more!
Pratik K wrote:
- Keep all necessary structures and variables as globals.

That is poor advice. Use of globals should be avoided. Variables should be a local as possible.

Irrelevant Elephant wrote:
When I need to code something, what is the process I should go through to design any algorithm(s), and solve the problem in the most efficient/flexible way?

That is part of the skill of being a programmer. You are always making trade-offs as to what is more efficient verses what is faster to write. You can spend hours or days trying to tweak an algorithm to gain a few microseconds, but if you only call it once in your program, who cares if it takes 5 microseconds verses 2 microseconds. Most of the time you're going to be waiting for the user to enter something. Now if you're solving highly compute bound linear equations, that's a different matter.

- How can I criticize my own code? Again, if I write some code that works I'll keep it, but what do you guys do?

As you develop your C++ skills, you will find yourself building libraries of reusable objects. Making these libraries reusable will help you in writing programs more quickly. As a professional programmer, I rely on a large collection of class libraries that I know work. That way I can incorporate those classes in a new application without worrying about testing every line of code. Could I write smaller code for a new application? Of course, but my objective is to deliver new functionality as quickly as possible with the highest possible quality.

How can I test my code for efficiency? What are the best ways of testing algorithms (etc) that I have added to see if they are fast enough? Of course, it will probably involve some kind of timer, but how will I be able to tell if X milliseconds is a good enough time?

One way of testing code for efficiency is the use of code profilers. Again, how important this is depends on the type of application that you're writing.





Okay thanks for the advice! :)
Topic archived. No new replies allowed.