F
F
free0u2011-01-04 00:01:17
C++ / C#
free0u, 2011-01-04 00:01:17

How to learn to write efficient and error-free programs that work?

What measures should be taken for this?
Maybe there is some good literature on this topic.
We are not talking about big projects, but rather a bias towards Olympiad programming.
Even if the program is simple and not very large, I personally often make small annoying mistakes in the process. And I want to strive to ensure that the program immediately works as it should.
(language - c++)

Answer the question

In order to leave comments, you need to log in

11 answer(s)
H
Hizof, 2011-01-04
@Hizof

The first thing that came to mind was S. McConnell's Perfect Code.
But it seems to me that “so that the program immediately works as it should” is a utopia.

E
Evgeny Elizarov, 2011-01-04
@KorP

> but personally, in the process I often make small annoying mistakes
, sorry, but in my opinion, no literature will help here, train attentiveness, I myself struggled with this with just such a method - all attention to the code

A
amc, 2011-01-04
@amc

Develop for yourself your own standard for writing code, so that, for example,
if you think “and here I need a cycle”, then immediately issue it completely, with all {} and everything else that is needed in your C ++, and not after three hours of reinforced coding to detect that you did not close the cycle SOMEWHERE.
It helped me a lot at the time.
Well, plus drawing diagrams and writing code on “paper” (you can use a simple text editor), when you write the text not in the development environment and then just compile, you quickly begin to pay attention to the correctness of the code.
Plus, it was a lot of fun when I wrote (under Delphi) without a working debugger, I had to write checks myself, which also helped to write better.

G
Georgy Khromchenko, 2011-01-04
@Mox

What you wrote is a utopia and a reason for neurosis on the topic that you are what you are - a fig programmer.
Accept the fact that you are spelling wrong. Having understood this, you can already think about what to do with it. For example, write tests. Try test-driven-development. Hire a tester. Then there is the possibility of issuing a product.
If someone needs a programmer who writes without errors, just leave such a customer / employer. He will just make you feel guilty.

J
javax, 2011-01-04
@javax

You don't care about that.
What's really important:
1. Write programs that are easy to read
2. Detect bugs early

R
Rigidus, 2011-01-05
@Rigidus

In order to avoid “little annoying mistakes”, it is better to start with a minimal prototype that can be captured at a glance and easily tested. Sometimes for rapid prototyping in programming competitions, familiarity with other languages ​​​​and approaches helps. It is very useful to train on typical Olympiad problems in order to be able to implement typical algorithms with your eyes closed.
For these recommendations to work, you need to fix your mistakes, otherwise there will be no good feedback and a clear picture. It takes a certain kind of discipline

C
charon, 2011-01-04
@charon

agree with the above. To minimize the number of errors (you will never get to 0 anyway), you need to practice more - that's all.

O
OlegTar, 2011-01-04
@OlegTar

It doesn't matter how many mistakes you make. It's important that you fix them.
==
You have to test and test how your program works. Even if you are 100% sure in the code (after all, some kind of typo may just creep in!)
I myself once did not bother to check how my code works.
There were bugs in my programs. Then I began to check the programs, but they still found errors.
And then I began to check my programs both in the tail and in the mane .
==
A respected person in the world of programming M. Fowler says: any program that has not been tested does not work
That is, check, check and check again.
Wrote a piece, checked, wrote another piece, checked.
==
Now from words to deeds.
Google "test-driven development".
True programmers first write tests, and only then code.
To do this, see the classes for testing for C ++
en.wikipedia.org/wiki/Unit Testing

G
Gleb_Sevruk, 2011-01-11
@Gleb_Sevruk

Train, read about common mistakes, language features, try, watch what happens with a debbugger.
After all, if there are such problems on a small project, then what will happen next? Well, you should read about coding standards and antipatterns:
www.insidecpp.ru/antipatterns/

D
Dmitry, 2011-01-11
@Neir0

Everyone makes annoying little mistakes. With experience, the likelihood of these errors decreases. It is impossible to exclude such errors altogether, it is only possible to reduce the likelihood of their occurrence. On the topic of identifying and preventing such errors in large projects, many articles, books have been written, a lot of practices and techniques have been developed. In small Olympiad problems, it is probably worth trying to write as clean code as possible, i.e. avoid pearls like this

 for (int i = 0; i < postParams.Length; PostParams += postParams[i] + (((++i) % 2 == 0) ? "&" : "=")) ; 
try to break the program into logical blocks so that it is easier to localize the error and train, train, train ...

S
Sh0ttky, 2017-05-18
@Sh0ttky

1) autotests that cover everything and everything
2) self-tests in the code.
In short, if in the course of writing the program I realized that something could be checked, they immediately drank "assertion" with output to the log and return of an error. Seriously reduces development time by the way.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question