Optimizing my c++ code

Started by
26 comments, last by visitor 15 years, 4 months ago
Hello I though it was time to optimize my code/make good c++ code.. Right now I think I am not really doing object oriented programming... my code is messy and noobish. I use globals a lot, I use structures and any efficient measure for code you can think off I probably don't use.. Could anyone give me some tips/point me to an article on two on how to make good code? Something that caught my attention a while ago that several programs from books and tutorials I came a cross had everything handled in complex classes, classes kept in header files that were accesed in source files. I don't know how to go about or where to start changing my code to this style, it looked really organized and 'efficient'. Any info on the subject would be appreciated.
Advertisement
I suggest the Code Complete books, they cover everything from writing good OOP code to optimizing techniques.

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

Writing good code has little to do with "optimizing" or "efficiency" - unless you are talking about programmer efficiency.

The first step is typically to break your code into smaller chunks. This may help.
Oh I already read that article and did the splitting into headers and source files though I am not sure I am doing a good job when it comes to what functions go in what files. I am also trying to get rid of my global variables.
Quote:Writing good code has little to do with "optimizing" or "efficiency"

More like, writing good code is broad area with a few different measures. In my opinion (i.e. I agree with rip-off) the first area to work on would be code layout, structure and naming . But optimization and efficiency in terms of algorithm choice, and data management are very important in certain areas of games programming at certain levels (generally professional level, or "hardcore" projects).
I would recommend Effective C++ over Code Complete. I liked the concrete examples Effective C++ was able to give, and the lay out was very nice. I would also recommend reading Guru of The Week as a free resource.
Quote:Original post by Antonym
Oh I already read that article and did the splitting into headers and source files though I am not sure I am doing a good job when it comes to what functions go in what files. I am also trying to get rid of my global variables.


Generally you create a header/source pair for every class you write(and the majority of your code should be in classes), the rest of the functions would usually be grouped in files by functionality. I don't know what your project is, but say all your set-up/initialization functions would be in one, possibly along with clean up, all of your asset/file loading could go in one, but liek I said, these would generally be in classes.

The only "loose" functions you should have would be utility functions in which there aren't enough related ones to form a utility class.


also, when you're talking about optimization, if you remember anything, remember this... 20/80. 20% of your code will be run 80% of the time, and that is a proven fact. So although you should generally write code with proformance in mind, figure out the code that gets run the most, and that is what you should optimize first
--------------------------------------Not All Martyrs See Divinity, But At Least You Tried
Quote:Original post by Treb
I would recommend Effective C++ over Code Complete. I liked the concrete examples Effective C++ was able to give, and the lay out was very nice. I would also recommend reading Guru of The Week as a free resource.


Effective C++ points out common mistakes , it does not go too deep into organization, optimization, modularity, cohesion etc. Code Complete teaches you how to write reliable, clear code. Effectiv C++ complements by going deeper into the syntatical correctness.

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

Hi,

if you want to use good object oriented solutions I suggest you to read this book. The book is not for beginners so, before buying, consider if you would be able to understand it.

Another good solution would be to study on good source code or to work with experienced programmers.

Quote:Original post by godsenddeath
The only "loose" functions you should have would be utility functions

Haven't you read How Non-Member Functions Improve Encapsulation?

This topic is closed to new replies.

Advertisement