Software Development

From Grundy
(Redirected from Programming Practices)
Jump to: navigation, search

This is probably the most important part of coding, and is useful irrespective of the field you choose to be in. Truth be told, good development skills can land you a job, save you hours of debugging, or even uncover deadly bugs. (yeah quite literally "dead"ly. Read this).

Software development includes everything, literally every product that can be made from programming varying from desktop to mobile applications, utilities, games etc. We do have specialised guides on Game Development and Web Development though. Software development usually solves a problem. This sets it from other types of programming which might seem abstract and have little or no direct consequences in real life. This article explains the basic work flow in software development, and some good practices. Note that this article is written with the perception that the reader is new to development. Professional software development in companies works in several different ways and is much more rigorous and organised.

To see a list of library / package guides we have on our wiki, have a look at Software Library Guides. Once you decide on a particular language / framework, please look at this.

Pre-requisites

  • Know the basics elements of your first language. That will be needed to learn good development techniques. If you haven't done so, please go through our Programming 101 guide.
  • Have in mind what you're going to make. Never get into development aimlessly, figuring out what you're going to make on the way. Even if this is the first time you're trying to make something, have a clear picture of the target so you always know what you need to do next.

Don't Re-Invent the Wheel!

This is probably the most important tip when starting out development. As beginners, we are eager to get our hands dirty and write some code implementing cool algorithms and techniques. Unfortunately, it's not always a good idea to do so, especially when it comes to standard tasks. There are thousands of libraries in each language doing the most common tasks. You should always prefer to use libraries wherever possible rather than write your own code, unless you are trying to learn something. Here's why you should use them,

  • Written by the Best - These libraries have been written by people with years of programming experience. They know all the tricks of the trade and know the best optimizations possible.
  • Years of Testing - Most popular libraries are more than a couple of years old and have been extensively tested using #Software_Testing's techniques. Besides this, millions of people across the world report bugs and suggest improvements. Their code is bound to work much better than yours.
  • Faster - Using a library is generally much easier (generally just needs a Google / StackOverflow search) than implementing your own code.

Importance of Modular Code

If you're going into software development for the first time, you'll notice that the code (rather codebase) increases dramatically in size. With this size increase there arises a need to split the program into as many modules as possible, each performing an independent task. Doing so helps in several ways. The code base becomes easier to understand, bugs become easier to find and testing becomes easier too. Having independent modules also means that changes in one do not often affect the behaviour in another.

Effective Debugging

Embrace each bug you find. A bug found and fixed is one that you would not have to deal with again. Also, finding one bug can often give you a lead to find another. Debugging is one of the largest parts of development and patience here is key.

Lints & Properly Formatted Code

The importance of properly formatted and well written code can never be overrated. A well written code is one which documents itself. There are many things that need to be taken in account to ensure proper formatting and well written code:

  • Naming Convention - Try to stick to the standard file naming convention generally practiced worldwide for that language. Even if you astray from the standard practice, make sure you are consistent across the project. Make sure you follow this for file names, functions, variables and modules.
  • Shorter Lines - Try not to exceed 80 / 100 characters in a line consistently across a project. This makes code a lot more readable.
  • Comments and Documentation - Comments are used within your code to explain the non-trivial parts of it. You don't need to comment about everything, but make sure your files are not just blocks of code. Documentation on the other hand describe your project in general and provide a description about your functions / classes (doc-blocks). Documentations can be rendered into an HTML web page if written correctly.
  • Proper Indentation - Make sure you code follows a consistent indentation scheme across the project. We won't get into the debate for the best type of indentation, (it's also language dependent), but you can have a look at Space vs Tab for a good discussion.
  • Error Handling - Make sure your code has try-catch-except blocks to handle code that could potentially produce errors, such as downloading files (which could fail due to a weak internet connection). However, error handling should be used only on code having uncertainty. Your core logic should not be placed under try-catch-except blocks as you want to find errors in it, to improve it.
  • Avoid Code Repititon - As far as possible, break your code into modules which can be re-used efficiently to avoid code reptition. This makes code easily expandable and a lot easier to read. Also, you can improve the code by just changing a single file.
  • Avoid Hard Coding - You should not hardcode any values in your modules. You should have a common `strings` and `constants` file to store all the constants. These files should be used all across the project to feed in your constants. So a single change in these files will apply in all files using it! Also, dynamic values should be passed as parameters instead of hard-coding.

Having such a code base makes it easier for one to debug and for a new contributor to understand the codebase. A lint is a tool that checks your code for formatting and structure errors based on a set of defined guidelines. Using such tools is easy and very valuable.

A great article to write excellent JavaScript code, which applies to most languages - How to Write Beautiful JS?.

Use Version Control Systems

Nobody likes maintaining copies with different names. Whether or not you're working in a team you'd really want to use a Version Control System for managing your software development project. Doing so gives you great power over "commiting" mistakes, being easily able to revert them. Here are some important advantages of version control systems -

  • No need to create separate copies of the same project. Use branches! Save a lot of space, have much more power by easy comparisions using diff, easy merging and finally just one folder has it all.
  • Very effective collaboration - Using version control tools, doing a group project just got a lot easier.
  • History of all changes - You have a complete list of things that you or your friend did, in sequential order. This speeds up debugging tremendously and allows easy code review.
  • Very easy to share code - With utilities like Github and patches, sharing code changes are fast and effective. You won't ever need to copy code to your email or entire project folders to a pendrive!

Probably the most popular version control system is git. Check out our Git guide to get started. It's a good idea to host your own codes on Github. This is a good learning exercise for git and extremely useful while collaborating.

Importance of Code Review

Code Review is pretty much what it means literally, you ask a second person to look at the code you wrote and check it. Here check does not refer to check for correctness. It's rather a read through the code, checking whether the implementation is okay, room for improvement, formatting and whether it will work in all cases. Generally, code being reviewed should be functional at a basic level.

It's a good idea to review your team members' code after each commit to understand the work they did and suggest corrections. You should ask for your code to be reviewed to. In larger open source settings, code is never merged until it has been thoroughly tested and reviewed by the module owner.

To implement code review, there are some good Github integration software like Reviewable. You can enable them in your repositories on Github. Github itself has a great review system and support for bots like Jenkins. If you don't know about Github, see our Open Source and Git guides.

Software Testing

How do you verify whether your code is correct? Well of course, you run it and check the output produced. But is that enough? Far from it. Software testing and verification is a very important field and almost all companies have separate teams to carry out software testing. However since you're probably not in a company, you'd want to make sure that you rely on reviews as much as possible. Thorough testing should take place before any code is added to the codebase. Border cases, dependencies and performance issues should be taken care of. If you don't have tests, then you don't know when you're done, and you won't know if you've done it right. Writing tests ensures that any future changes in the codebase doesn't break anything. So to be a good programmer, you have to be a good tester.

How Much Should One Plan?

While it is true that you should have a clear picture of what you aim to build before you start coding, it is advisable for beginners to get some eggs in the basket before they plan to sell the chicken. In other words, do not spend time planning how you'll implement a feature that's going to require a base which you haven't built yet. Plan the build in steps and make sure not to over-think a step before completing the previous ones.

Let's go over a small example. Let's say you are constructing a copy of Minesweeper. As a planning step, you should plan your overall data structure and list of classes / functions. For instance, the data structure could be a 2-D array of mine arrangements along with another 2-D boolean array revealing whether or not a cell has been uncovered. You could have a display() function which takes these arrays and prints the numbers on the screen, a check() function, which checks whether the move was successful, a win() function and a lose() function. You could also plan the algorithm for challenging tasks like uncovering cells having no surrounding mines. (Flood-Fill algorithm). There is no need to get into specifics of the number of variables, exact win / loss conditions as these could be fairly straightforward decisions once a framework is ready.

Read Code

To write better code one must read better code. Like most languages, reading more will enhance your own writing skills. However unlike literature, reading code may feel boring and monotonous. But it gives one the idea about how professional software developers write code. Spending time reading others' codes would inculcate a habit of using better programming practices and ultimately writing beautiful code.

It may seem a bit random to just open up some code online and begin reading. It's best if you read relevant code. This is easily done if you are trying to contribute to an Open Source project. If not, you can try to read the code of a certain library you are using for a project. This is provided the library has released its code.

For instance, if you are using using TensorFlow, you might want to keep seeing the source code of certain APIs to exactly understand what's going on. Professional code is generally very well thought of and easy to grasp. There is also sufficient documentation for open source projects. On a smaller scale, it's a good idea to browse our community projects. Have a look at Community Projects to begin. You may even contribute if you like!

Standard Coding Practices

Google has very good standards for code quality (obviously). Following some of these links may be helpful for the more experienced programmer.

And for a brilliant exposition of good programming practices written as zen stories, read The Codeless Code.

See Also