What mistakes does every novice programmer make?
In this article you will learn how to improve your skills. If you are already a master, you can write about errors that I did not consider in the comments.
Error 1 - Insignificant Names
Clean code - a code that does not need comments, but how to achieve this?
')
Consider the main stages that my code went through:
- Variable in 1 character.
At the beginning of my journey, my variables were called “a”, “b”, “c”. At that time (3 years ago) the programs consisted of 20-30 lines of code, so I did not see the problem. - Variables in transliteration.
If you have not opened the project for more than a month, it will be difficult to remember the assignment of the variable “a”. After this situation, I decided to call the variables "skorost_korablya", "massiv_chisel". Yes, then my knowledge of English left much to be desired. - Significant variables.
A meaningful variable is a variable that reflects the essence of data and actions.
While I was writing variables in translit, the speed of development left much to be desired, so I decided to use the online translator to get the correct translation. Variables began to wear the following names "ship_speed", "array_numbers".
In addition to variables, both function names and class names should be significant. The name of the function should begin with a verb, for example “get_ship_speed”, “updateFrame”.
Error 2 - Missing Comments
At the initial stage, your code should contain comments, it will help to avoid the following exclamations: “Aaaa ... yes, what ... does this ... function !!!"
When you want to make changes to your old project, which was completed a couple of months ago, comments will allow you to quickly recall what a class, function or variable is doing.
The more projects you write, the better their structure will be, which means that the need for comments will decrease. (It works if you apply new knowledge with each project.)
Error 3 - Using "bikes"
What is my understanding of "bicycle"? This is a new technology with hidden errors that does not know about the existence of the old and debugged.
Do you want to write a game? You should not invent the engine, take ready (Unity, UE4).
Maybe you want to write a new OS? It sounds good, but at the initial stage it is better to focus on obtaining a knowledge base.
Error 4 - Lack of Theory
In the first year of studying programming, I could not come up with a task that I could solve. Why? My knowledge did not allow to describe the exact solution algorithm.
The solution of each problem can be represented as a set of basic components; as part of programming, these can be blocks with conditional transition operators and cycles. For the graphical interface of the program, these may be blocks for entering text / date, blocks for displaying an image / table.
Error 5 - Lack of Style
For better readability, each block of code should contain internal padding (4 spaces or 2 Tab).
The declaration of variables should be as close as possible to the block where it is used.
Throughout the entire project, you must adhere to one style - snake (snake_case) or camel (CamelCase).
Error 6 - Duplicate Code Blocks
This error occurs when you do not understand how to organize the work of the code. The best solution would be to review the entire code and rewrite the structure. The best solution would be to make duplicate blocks of code in the function and use them already. In the latter case, if you want to make changes, you will need to do it only in 1 section of the code.
Error 7 - One class and dozens of methods
My first program, written from scratch, was the game "Tic-tac-toe" in the console with the support of the game on the field 78 * 78. It took more than a thousand lines in one class. So do not) Think over the structure before writing a project. For example, for my game now I would single out the following classes:
- MainMenu - is responsible for the interaction of other modules.
- Player - is responsible for the interaction of the player with the system.
- BotLogic - is responsible for the logic of AI.
- LevelGraphics - responsible for drawing graphics.
- Autosave - is responsible for reading / writing the save file.
Error 8 - One function - many tasks
According to the principles of functional programming, the function should not change the external state of other objects and should perform only one task.
Consider an example. Let the function reading data from the file be given. As a result of the program, in addition to returning an array of data, it resets the variable that stores the file name. It is better not to do this, since subsequent functions may depend on this variable.
The correct, in my opinion, implementationprivate void Ok_Click(object sender, EventArgs e) { if (File.Exists(fileName)) insertDataIntoSQLServer(getDataTable(fileName)); else MessageBox.Show(this, " ", "", MessageBoxButtons.OK, MessageBoxIcon.Error); } private DataTable getDataTable(string csv_file_path) { DataTable table = new DataTable(); try { using (TextFieldParser parser = new TextFieldParser(csv_file_path)) {
Error 9 - Excess Optimization
At one time I tried to optimize my programs as much as possible, but it all came down to changing the type of variables or algorithm.
How to get rid of this feeling? Imagine that you have in your array not ten elements, but one hundred thousand. The int type takes
2 bytes 4 bytes, which means that the array in RAM will take less than 1 MB. If you change the type to short, the array will also take less than 1 MB, but overflow errors may already occur.
Error 10 - Lack of protection against incorrect user data
Each project works in “greenhouse” conditions, when only the creator works with it. And try to give another person to play with him).
- Write a calculator? Check that the user does not want to divide the number by 0.
- Want to get the right phone number or email? Check regular expression input string.
- Maybe you are working with a database. And protection against SQL injection added?
Recommended literature
- Steve McConnell "Perfect Code"
- Robert Martin "Clean Code"
- Martin Fowler “Refactoring. Improving existing code "
And what mistakes are present in your projects? :) Write in the comments :)