Whether or not you are already an experienced Java, C ++, Python, or PHP programmer, there is always room to increase your knowledge and skills in the art of computer programming. This article will give you some tips to help you become a better programmer.
Steps
Step 1. The first step is a clear and thorough analysis of the problem you need to solve
Step 2. The second step is to think carefully about the solution of your problem
Step 3. List the requirements your program will have to meet
Take all the time you need to write in a clear and detailed way the functions that your program will have to have, think about the needs of the users who will use your software. The more precise you are at this stage, the more time you will save during the implementation.
Step 4. Develop a work plan
- For a small program, or one that will not have to interact with other software, a simple flow chart or a simple mathematical algorithm may suffice.
-
For large implementations, however, it will help you to divide the work into smaller modules and consider the following steps:
- Understanding what the task of each module should be.
- The data that will be exchanged between the various modules.
- Within each form how the data will be used.
- The analysis phase of the requirements and data structures of your program may seem boring, especially compared to the actual programming phase, but if you concentrate carefully, and if you dedicate the right time to these preliminary operations, you will save many hours in the phase of test and debug your program and you will most likely find more efficient ways of solving your problem.
Step 5. Always add comments to your code
It is a good idea to always comment on the code you are writing explaining how it works and above all why you have decided to implement a function in that way. It also explains what data it needs and what it returns, always thinking that you might not be the only one having to manage changes to that program in the future.
Step 6. Use standards to name your variables
Giving a clear name to the data you use will make your job easier in the future. Definitely writing something like 'x = a + b * c', is quick and easy, but this type of code will be difficult to manage, both in the phase of any modifications, and in the debug phase for any problems. You rely on the Hungarian notation that variable names must have a prefix that identifies their type. For example, if you define an integer variable, you will use a similar name 'intRowCount' while to define a string you will use 'strUserName'. No matter what standard you adopt, make sure your variable names are as descriptive as possible (Read the Warnings section).
Step 7. Organize your code
Always use code indentation when writing your programs. Using this simple tool will make blocks of statements referencing, for example, an 'if-then-else' or a 'while' loop visible at a glance, making your code elegant and easy to understand. Also always leave a space between the variable names and the various operators you will use. A code like this 'myVar = 2 + otherVar' is certainly more readable than 'myVar = 2 + otherVar'.
Step 8. Run thorough and comprehensive tests
Start testing the various modules with the data you would normally expect to find, then use particular data trying to find exceptions to handle, or situations where the code unexpectedly fails. Even performing tests is an art in itself, knowing how to test correctly, and in depth, a software makes it robust and stable. Run your tests considering these situations:
- Extreme: Divisions by zero, or values that exceed the maximum size of the variables, empty strings in the case of text variables or null values in the case of parameters.
- Meaningless values. Even if it will be difficult for the end user to enter meaningless values, it is only fair that your software is still able to handle them.
- Incorrect values. Such as: divide by zero or negative values to perform a square root when positive values are expected, or a non-numeric value within a string where you are looking for numeric values.
Step 9. Practice Makes Perfect
Programming is not an immobile world, on the contrary it evolves very quickly, there is always something new to learn or something old to re-study.
Step 10. Expect changes
In the business world, in a real project, the requirements your program must meet change and often do so quickly. However, the better you understand what the requirements your program will have to meet and the clearer the initial feature analysis phase, the less likely you are to have to make major changes to your code.
- You can move ahead by submitting a written analysis of the required features for your program or by preparing a plan for software development and implementation before you have started writing a single line of code. In this way you will be sure that what you are going to create is what has been requested.
- In order to work on one part of the project at a time, structure it with intermediate deadlines and organize demonstrations to present the progress achieved, the less things to think about, the easier it will be to focus on the objectives to be achieved in an effective, clear and productive way.
Step 11. Start by solving simple problems to get to solving more complex ones
When you have to create a complex program it can be helpful to divide it into smaller programs, they will be easier to manage and write. For example, if you want to create an animation of a figure that follows the movements of the mouse and changes shape according to the speed of the movement, proceed as follows:
- Start by drawing a square and make it follow the movements of the mouse. First solve problems related to movement management.
- Next, troubleshoot changing the size of the square based on mouse speed.
- Finally, create the figure you are interested in viewing and manage it with the two components you created in the previous steps.
- This approach lends itself naturally to object-oriented programming in which blocks of code are created for the management of a specific problem and which can then be reused in all programs in which the same problem is to be solved. For example, you can reuse the block of code that manages the movement of the mouse wherever you want. This will make it easier for you to write code, debug and test, and keep your software up and running in the future.
Advice
-
If you have fellow programmers ask to be able to study their source code. You may discover solutions you haven't thought of. Don't you have this chance? Not bad, the web is full of online communities and forums where you can share information and ask questions about any programming language and for any operating system, you just have to ask.
- If you choose to access these forums, observe the rules of conduct. There are many experienced programmers who, as long as they are asked in a polite and civilized way, are ready to answer all your questions.
- Remember to be nice, you're asking another person a favor. Don't be frustrated if you don't understand the solution given at first try and don't expect the other side to be willing to check 10,000 lines of your code trying to figure out where the error is. Instead, ask questions related to your problem as specific as possible and post 5-10 lines of code at most. This will make it easier for you to get answers that solve your problem.
- Before asking others for help, do a little research in the forum. Your problem has certainly already been addressed, and solved, by someone before you.
- Studying code written by other programmers is a great way to increase your skills. Try to understand the operations these programs perform and how the variables used behave, then write your own code that performs the same operations or even performs them more efficiently. You will quickly learn to write code correctly and to know the tricks that will allow you to obtain robust and fast programs.
- Always back up your data to an external hard drive, or to a portable device, this way, in case of a problem with your computer, you will always have your code available.
- Check the spelling and syntax of your code very carefully. Even the smallest mistake can be a source of great stress and waste of time.
- One way to always have your code available and safe is to use a version management tool (such as Git or Mercurial) that backs up through a free online hosting service such as Github or Bitbucket.
- Talk to other programmers. Meeting other people can often be a great resource for getting information and answers to your questions, especially when starting out. Find out if there is a group of programmers meeting in your neighborhood and join the group.
- Start small and aim for goals that you are able to achieve with the preparation you have at the moment, then continue to increase your knowledge and skills.
- Always write well indented code, not only is it neat and clean, it's also easy to read and understand. This is a key aspect to make subsequent changes quick and easy to make.
- Use a complete and performing code editor. Good development software has an automatic completion function for the code you are writing and is able to suggest, from a list, the keyword you need so as to minimize possible typing errors. It must also be able to highlight parts of your code with different colors, based on the meaning they cover, and must be equipped with a good debugger to test their validity and, possibly, understand where the errors are.
- After you've written large chunks of code, take a break and take the opportunity to do more, then review the code you've written with a fresh mind. You may find a faster and more effective way to solve your problem by writing fewer lines of code.
- Always keep a copy of all the work you do. Not only is it a reference point for new programs, sometimes you can even reuse some portions.
- Instead of using instructions that print out the contents of the variables, or the point your program is executing, use debugging software. It will be easier to understand where and why an error occurs.
- To write your code, use an editor that assigns, based on the meaning of what you are writing, different colors to the text. It will help you later to understand in a few seconds if what you are reading is a comment, a code keyword, a number, a string, etc.
- While you're looking for an error when debugging, make one change at a time and test it before moving on to the next, you'll have more control over what you're doing.
- Websites containing how-to guides on solving certain problems are very helpful.
- Use a version management tool for your code. Programs like CVS or SVN help you easily keep track of changes made to your program.
- Separate your code in 'Package', it will be quick and easy to reuse. This way of programming leads, over time, to having a large library of robust and performing code that you can use in all your programs. This will help you write complex, yet powerful and stable applications.
- Remember that customers and bosses are not interested in understanding how your program works or if it works optimally. Customers are nothing more than people like you, but much more stressed and they may not be amazed at what kind of data structure you use, the only thing they care about is that you can increase the speed of your program by 10%..
Warnings
- Save your work frequently, and as often as you can, or you'll risk wasting hours and hours of programming. Accept this advice or you'll learn the hard way, the hard way, as soon as your computer crashes or as soon as you have to restart it without being able to save.
- In step number 6 use the Hungarian notation with caution. It could lead to inconsistency issues, or other difficulties, by migrating your code to other languages or to an operating system other than your native one.
- Copying and pasting third-party code is a bad habit, but, when done with small portions of open-source code, it's a great chance to learn new things. Don't try to copy entire programs though, to take credit for making them, and don't copy code from another copyrighted program.