Programming the right way?

Recommended Posts

Greetings all,

This is my first post here, and I suppose the write place to be asking,

I've been programming for several years but I would describe myself as a hack and slash programmer, with buggy code and not very well written.

Can anyone point me in the right direction to coding the right way with less bugs and more reliability?

My preferred learning platform is visual and audio :)

I mainly program in vb6 and vb.net but am currently learning c++ I don't know if the proper technique can be applied across all languages or whether there is a proper way to do each one?

Any help/links would be greatly appreciated.

Thanks

Richard

Share on other sites

I hadn't really considered looking over peoples source code, but I will definitely start having a nosy at other open source projects and how they are built.

Share on other sites
IcedCrow    267

To me it comes down to working on projects and meeting your objectives.  Experience is the first thing.

After that I'd suggest looking up books on clean code.  I learned a lot from working with people that had worked a lot in the business and every place I've worked at I have had the opportunity to learn even more.

Hit up amazon for "Clean Code".  There should be a book with that title, and its pretty good IMO.  Goes over some things that people do all the time that contribute to the downfall of their own code.

Share on other sites
alvaro    21246

Hit up amazon for "Clean Code". There should be a book with that title, and its pretty good IMO.

That book it is a bit extreme. For instance, they advocate that each function should do only one thing, so you shouldn't write code like this:

void compute_salary() {
double result = 0.0;
try {
result += compute_base_salary();
result += compute_bonus();
}
catch (...) {
complain("Problem computing salary: Returning 0.");
return 0.0;
}
return result;
}


The reason is that this function does two things: Exception handling and computing something. The book advocates placing a single function call in the try' block, so that this function's only purpose is exception handling. Unfortunately, I find myself struggling to find a good name for such a function (compute_salary_without_exception_handling'?), which is my primary guide as to whether something should be in its own function.

But it is still a good read, and it will make you think about many interesting issues.

Share on other sites
NightCreature83    5002

I found looking at other peoples code helped a lot but also looking at my old code that I wrote years ago and spotting the mistakes in them. This will also give you a sense of growth as a programmer in that you get to see what you are doing better now.

Share on other sites
Telastyn    3777

Can anyone point me in the right direction to coding the right way with less bugs and more reliability?

First things first, there's no "right" way - there are a bunch of ways that will get the job done well.

Beyond that, I would look at the code you have written, and figure out why there are bugs. Did you use certain patterns that made the code fragile? Did you name things poorly so they got misused? Did the functions have too many side effects? Did you not have a good process to prevent typos/merge errors/etc?

Books and even good programmers can only bring you so far. Eventually you have to internalize the knowledge so that you can get the feel of what things are troublesome and why; what things are usually good and why. Then you can better apply those things to the problems at hand.

Share on other sites
mmakrzem    1036

For C++ programming I highly recommend checking out Scott's "Effective" books: http://www.aristeia.com/books.html

They are a great resource and point out common techniques that everyone should use.

Share on other sites
tufflax    504

This is in my opinion the best resource ever on this topic: http://www.infoq.com/presentations/Simple-Made-Easy

Also, you might wanna read this: https://sites.google.com/site/steveyegge2/being-the-averagest (and maybe this, this is where the name of the other article comes from: http://www.paulgraham.com/avg.html)

Edited by tufflax

Share on other sites
Khaiy    2148

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.

Share on other sites
EddieV223    1839

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.

Share on other sites
Nicholas Kong    1535

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

Share on other sites
EarthBanana    1794

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

Share on other sites
Epricity    107

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.

Share on other sites
Infinity95    324

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.

Share on other sites
Khaiy    2148

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.

Share on other sites
Infinity95    324

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.

Share on other sites
Godmil    748
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.

Share on other sites
Khaiy    2148

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.

Edited by Khaiy

Share on other sites
EarthBanana    1794

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

Share on other sites
Vexter    132

Here are some other useful books about best practices that I have picked up and seem to be popular:

Design Patterns: Elements of Reusable Object-Oriented Software

http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612/ref=sr_1_1?ie=UTF8&qid=1374776130&sr=8-1&keywords=design+patterns

C++ Coding Standards: 101 Rules, Guidelines, and Best Practices

http://www.amazon.com/gp/product/0321113586/ref=wms_ohs_product?ie=UTF8&psc=1

Share on other sites
redellion    130

Hey Richard,

There is nothing wrong with hack and slash / rapid prototyping it can make you very productive, with that being said you need to train yourself to spot potential issues with the code you write and know the impact it may have.

That should give you a long term to-do list, just remember premature optimisation is the root of all evil.

So what to do?

Pick challenging personal projects which cover broad areas that are just beyond your current level, for example: an account management system with a login and user profile front end.

That should cover database and class design, security, state management etc, also you may end up with a reusable system for your future game projects.

Read up on topics before tackling them to give yourself a degree of confidence that you are going in the right direction.

Object Oriented Programming Concepts (Overview)

Object Oriented Programming Concepts (In Detail)

S.O.L.I.D. Principles of Class Design (Overview)

Software Design Patterns (Overview)

A couple of "classic" books which are fairly language agnostic:

• Code Complete.
• Patterns of enterprise application architecture.
• The Pragmatic Programmer
• Refactoring: Improving the Design of Existing Code

Useful websites for finding answers to technical questions:

http://stackoverflow.com/

http://programmers.stackexchange.com/

Try picking up another programming language once you are confident, it will make you a more rounded programmer and will teach you something new that you can take with you in future.

Sorry this is a bit of scatter gun approach to answering your question, but hopefully you can find some useful information in there!

Jamie.

Edited by redellion

Share on other sites
LennyLen    5715

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.

That doesn't sound like particularly good advice to be honest, at least not for a beginner. Arrays and pointers have particular uses and using a vector when an array is the more ideal solution is just as wrong as using an array when a vector would be better. At first, it's better to learn the difference between the two, and then learn when, and when not, to use them.

Avoiding pointers is just non-advice really (at least without context). If you're using Java, then you don't even have pointers, and if you're using C, just try and do anything meaningful without them.

Share on other sites

I can only talk about what I've been doing myself:

a) - Watch videos about computer science and programming, e.g. from Stanford University or UNSWlearning.

b1 - For Java Programming (my discipline atm), get books like Effective Java Second Edition, Java The Complete Reference, etc.

b2 - For general Programming and Problemsolving, get The Art of Computer Programming by Donald Knuth.

c) - Get a better IDE (if applicable) and be sure to make your own code as easy to read as possible, even to yourself. Proper commenting is key.

d) - Github and open source code sharing is your friend.

etc.

Edited by Malabyte