Programming is Easy
Hello friends, this is Pruthwiraj Ghadge back again with a new blog. I always wonder how people write programs in their general routine. I mean what procedure they follow to write programs. Now, you might be thinking why am I thinking about this and does it actually matter? Frankly speaking yes, it matters a lot. But what could be the procedure in a real sense? We generally think that writing program is writing code. Of course, it is but that is not the only thing which is programming. There are many other things associated with it and we must know these things.
So how to write a program? I prefer the following methodology for writing programs and I do follow it in practice
- Algorithm writing
- Exploring Data and Functions
- Flow and coding conventions
- Code it with comments
- Don’t forget to document
- Keep practicing
People normally feel writing algorithms is boring (in fact I do think so). Of course, it is, but then this step is the key to writing error-free programs. In most cases, we spend a lot of our time thinking about how to start and how to go about the code. When we are not aware of the sequence, it is a best practice to write the algorithm and then proceed further. With we have a precisely designed algorithm handy we can easily write the program. Algorithms are generally the pseudo-code or a set of statements in your own language – generally, one we speak. It is the best characteristic of algorithms that they can be written in any language you know and there are no rules on how to frame the statements for them. An algorithm is a set of sentences in your own language that define how to perform a specific task. While writing an algorithm there are no bounds of any language and hence it is very easy to write.
If you are sick of algorithms and you like coding you can write a pseudo-code which is almost near to coding itself. However, when writing pseudo-code you have to take care of keyword and terms being used in it. Both of these things help us writing programs in a clean and neat manner. Once we have a set of written instructions in our own language we can easily transform it into code (of course, you should know the syntax of the language first).
Exploring Data and Functions
Once we are ready with the algorithm or pseudo-code, we can start examining the data and functions needed for the program. Now you will say, what is the need for this thing? Actually, this reduces a lot of coding overhead, if sorted first. Most of the time, when we write the programs we randomly decide on what data should be taken, their data types and also the functions, if at all we are writing. But do you know that deciding a proper datatype for data is as important as writing a program? In most cases when we need some data in our program we choose the default datatypes or common data types. This may lead to unnecessary wastage of memory and also could result in loss of data sometimes. For example, if we are writing a program for temperature conversion we must understand that the temperature values will be in floating-point i.e. decimal point values. Most people prefer taking numeric data as an integer. However, here, in this case, the formula includes division and a fraction and hence it is obvious that the result will always be in decimal point or floating-point values. This means that we should choose the floating-point data type for our data in this program.
We also should consider modularising our program and make it more flexible, readable and most importantly reusable. Before writing any code, it is good to check for any possible code redundancy in the program and make sure to remove it. This can be done by introducing functions in the program. Functions separate the code which can be individually executed as a different path. Programmers in their early stages write programs in a single function, which means everything goes inside one block of code. Meanwhile, they learn about functions and they start using functions. After some time they get bored with writing code in different functions and get back to the old style of writing everything within one block. Or some people still write code with functions but within the same file. It is a good practice to write modular code and it comes by practice, writing good modules. It is a good way to write the functions in different files to use and reuse them whenever and wherever necessary. Separating out the functions in different files makes it more maintainable, moreover, it makes it reusable.
Flow and Naming Conventions
While writing a program it is beneficial to divide your code into multiple different parts and along with that, it is necessary to name the data and functions properly and suitably. Many programmers, especially the new ones, are unaware of the fact that there exists a concept like naming conventions. It’s very likely to not name your variables and functions properly in the earlier stages. However, this could lead to severe consequences and in turn may put you in trouble at some point in time. So what is the way to name your variables and functions? In the first place, we must use proper and meaningful names for the variables and functions. Generally, the variable names should be nouns – proper nouns or common nouns – depending upon what value they store. Whereas functions are verbs describing the action that it performs. If we talk about classes and objects in OOP and methods and interfaces too, I must say that classes are common nouns and objects name can be proper nouns depending upon what they hold. Method names are verbs and interface names could be adjectives or nouns sometimes.
It is not sufficient to name the variables and methods/functions in a meaningful way, but these names also should be readable while reading programs. To make program and code readable we should take every possible measure that we can. There are many coding conventions available for naming variables and functions and we can definitely use them to improve our coding standards. Many companies in the industry have adopted these standards and some companies have created some of their own. If you want to be a good programmer, you should also try using one of these standards or else create your own. The use of naming conventions make it easy to understand the code, for future programmers and for you too. Apart from the naming conventions coding standards are also one of the important parts of it. Use of indentations, proper spacing and aligning of code is another way to make your code more userfriendly.
Apart from the naming and coding conventions, it is very crucial to understand the flow of the program and decide on how to keep it neat and simple. Many a time we end up using the constructs which are not necessary or which are irrelevant at the specific place. It’s a very delicate thing to choose the proper construct in a proper situation. Especially, when choosing loops we use a specific loop only because it is easy to use. However, it is completely inappropriate to use it at the place. For example, loops are designed in a particular way to suit different needs in different situations. Some cases where we have to perform the exact number of iterations whereas in some cases the user decides the loop exit. There are some statements that can be easily replaced by the other statements and the code can be made very easy. Such constructs must be used. Like if there are else-if ladders, they can be replaced by switch cases which makes coding easy. Such practices make us smart programmers.
Code it with Comments
Once you are done with the above parts, you are ready to start the coding. Make sure you use the coding, naming conventions you have chosen and follow the algorithm you have written. Now with the above all things handy, you can easily write your code. However, again there is another big thing you must not forget is comments. Comments are the parts of the program that are not going to execute, so why should I worry about it? Well absolutely not, if you have enough time to spare reading the whole program again to understand what it is all about. Exactly, if you don’t want to waste your time when you come back to your code after a long time and you need to understand it quickly it is the best way to remember, using comments in the code. Commenting the code often is a boring task but it saves you a lot of time in the future. Also, the programmers who are going to refer to this code, aren’t interested in reading the whole code. Commenting the code takes up a small-time while coding however it saves much more in the future so take it seriously. Comments make it easy for new programmers to understand the code easily. Also, many a time we read our code again to maybe debug or just correct. In such cases, we don’t want to read the irrelevant code and directly go to the needed one. Comments help us find the necessary code easily.
Don’t forget to Document
While we write the code we write comments in the programs. While creating the documentation these comments prove to be very useful. Yes, I am talking about documentation. Although the most tiring and tedious jobs to do, documentation is a highly important thing to make. Without the documentation, software or program is a handicap. If we write a good program but do not document it properly, it is as good as not writing one. We use advanced IDEs and tools to write the programs and they generally help to create documentation for our programs automatically, provided we write proper comments in the code. If you want to get an idea of how to write the comments read the library codes and programs written by some good and experienced programmers. Documentation is another good way to learn any new thing. All the programming languages come up with huge and descriptive documentation. If you want to learn the language you can use the documentation and learn the language without anybody’s help. It normally has the syntax and references that give us detailed information about the various things we are using in the programs.
Programming is an art and we have to practice it a lot to perfect(?) it. It is said in programming that a single problem has many different solutions. It means that different people solve the same problem in many different ways. The same thing is called logic and to develop it we must keep practicing. While we code in different languages, practice is the key to logic development. Program coding is very similar to mathematics, more you practice more you become an expert in it. If we find a solution to a problem, it is not the end of the coding there, we must try and find another way to solve the same problem in different ways. This can only be achieved by exercising, practicing. So keep practicing and be an expert.
I hope you would like this article. I will be back with another one some other time. Till then have a nice time.
Ghadge, Pruthwiraj | SevenMentor Pvt. Ltd.
Call the Trainer and Book your free demo class for JAVA now!!!
© Copyright 2019 | Sevenmentor Pvt Ltd.