Programming the right way?

Started by
22 comments, last by Ronnie Mado Solbakken 10 years, 9 months ago

I'm always happy to point people to Code Complete (2nd ed.). It's got lots of great language-agnostic information and is easy to use as a reference. But as others have said, it's only through experience that you improve at coding.

+ rep for you, I was about to recommend that same book. It really is "the" book about how write quality code. Just a short 960 pages of line after line great advice. I took notes while reading this book, the notes were on a large white board I keep on the wall by my computer, they've been there for a year, I know them by heart now but I can't bring myself to erase them.

If this post or signature was helpful and/or constructive please give rep.

// C++ Video tutorials

http://www.youtube.com/watch?v=Wo60USYV9Ik

// Easy to learn 2D Game Library c++

SFML2.2 Download http://www.sfml-dev.org/download.php

SFML2.2 Tutorials http://www.sfml-dev.org/tutorials/2.2/

// Excellent 2d physics library Box2D

http://box2d.org/about/

// SFML 2 book

http://www.amazon.com/gp/product/1849696845/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=1849696845&linkCode=as2&tag=gamer2creator-20

Advertisement

1) Design before writing code

2) Refactor duplicated code

3) Make sure the code is short and straightforward

When you write code, make sure the other person can understand it without comments

Well-written code are straightforward

here is a little tip i just caught myself in - don't use random numbers in your code - give the number a variable name somewhere..

i just found a line in my code that said if ( pos.x * 1.27f <= 23.45f )

this took me like an hour to figure out why i wrote that

I think the thing that's most important when writing any program is thinking about maintainability before you start writing it.

No matter what framework, or tools you decide to use, you should always break the functionality of your program into organized pieces, so that you always know where to look if you're extending, or if something breaks.

You can use patterns like MVC for this, or you can create your own.

As long as you understand how your app is built without having to dig for anything, you're in good shape.

here is a little tip i just caught myself in - don't use random numbers in your code - give the number a variable name somewhere..

i just found a line in my code that said if ( pos.x * 1.27f <= 23.45f )

this took me like an hour to figure out why i wrote that

Or you could save some time and just write in a comment on the same line what the numbers are about. Then you don't have useless variables that could get you confused.

"Errare humanum est, sed in errare perseverare diabolicum."

here is a little tip i just caught myself in - don't use random numbers in your code - give the number a variable name somewhere..

i just found a line in my code that said if ( pos.x * 1.27f <= 23.45f )

this took me like an hour to figure out why i wrote that

Or you could save some time and just write in a comment on the same line what the numbers are about. Then you don't have useless variables that could get you confused.

It takes no more time to declare a well-named variable than it does to write in a comment explaining a number, and a well-named variable shouldn't be confusing. Using magic numbers is a terrible habit. I'm sure there are situations where the approach you describe is appropriate, but advocating it as a rule sounds to me like saying that doing cocaine is fine as long as you go jogging afterwards because jogging is healthy, right? It's focusing on the wrong end of the problem.

-------R.I.P.-------

Selective Quote

~Too Late - Too Soon~

here is a little tip i just caught myself in - don't use random numbers in your code - give the number a variable name somewhere..

i just found a line in my code that said if ( pos.x * 1.27f <= 23.45f )

this took me like an hour to figure out why i wrote that

Or you could save some time and just write in a comment on the same line what the numbers are about. Then you don't have useless variables that could get you confused.

It takes no more time to declare a well-named variable than it does to write in a comment explaining a number, and a well-named variable shouldn't be confusing. Using magic numbers is a terrible habit. I'm sure there are situations where the approach you describe is appropriate, but advocating it as a rule sounds to me like saying that doing cocaine is fine as long as you go jogging afterwards because jogging is healthy, right? It's focusing on the wrong end of the problem.

I never said it should be a rule. Just my opinion. I don't like it when i have variables that are only used once just for the sake of knowing what they are used for. That's what comments are for. If i use them more than once i almost always use variables for them because it's easier to change the value of it. I only have to change one value and not find and replace 3 values. But if the value is only used once in the whole program why not just write it in there and make a small comment. I know it's preference but still i don't like it when there are 'unnecessary' variables.

"Errare humanum est, sed in errare perseverare diabolicum."

I'm quite new to programming (c++) and I've been trying to make sure I'm learning good techniques. A lot of things I'm reading is to avoid older ways of doing things. Like you should be using containers such as Vector instead of Arrays, trying to avoid pointers, and if you use a lot of New/Delete's then it's worth reading about RAII.


I never said it should be a rule. Just my opinion. I don't like it when i have variables that are only used once just for the sake of knowing what they are used for. That's what comments are for. If i use them more than once i almost always use variables for them because it's easier to change the value of it. I only have to change one value and not find and replace 3 values. But if the value is only used once in the whole program why not just write it in there and make a small comment. I know it's preference but still i don't like it when there are 'unnecessary' variables.

As I said, there are bound to be cases where your preference here is the best choice. But in practical terms, as I also said, declaring a variable with a good name takes no more time than writing a comment, and a well-named variable should be equally clear about its purpose as well as more concise and more convenient for code maintenance.

It will normally be true that a number, used only once and only in a function's local scope, can be similarly well represented by a plain integer/float/double/whatever or a named variable. But it is less convenient and less natural for a programmer to refer to a comment, located outside of the code fragment being reviewed, to figure out the significance of a variable which is being used when a descriptive variable name can document its own purpose and significance in-place.

There are other design issues related to this, described quite well in the book I mentioned above. The shortest summary is that named variables help a programmer manage the complexity of the project, while magic numbers do not. A magic number is improved by a descriptive comment, but is still inferior to a named variable. But my main point is that your preferred approach on this offers no advantages over a named variable, not even saving time, while being less clear, less concise, and less expandable.

I would suggest that you think a bit more broadly about both approaches when comparing them. Just because you aren't using named variables in these cases doesn't mean that they are useless, and aside from satisfying your pre-existing preference your style isn't even providing the one benefit you attributed to it. I would be interested to hear any other arguments you have, but at the moment I am entirely unpersuaded.

EDIT: Added quote to make the response clearer.

-------R.I.P.-------

Selective Quote

~Too Late - Too Soon~

In the case I was talking about it was a simple mistake - I was just in a hurry testing my grid system... a comment or a good variable name - both good solutions to the problem.

My tip really was more aimed at when your in a hurry and just throwing numbers around to test stuff - or code around in general - make sure if you were to not see that piece of code for weeks you could easily see why its there.. This situation happens a lot I think and a simple variable or comment could have saved me some time (it turns out I got distracted with something else and didn't look at that code again for a few weeks).

Moral of the story always try to write your code as if someone else is going to read it - it may seem a bit tedious but the time saved is almost always more than the time it takes to write code that way

This topic is closed to new replies.

Advertisement