border
Login  |  Register
border leftborder right
ProgBlogs Directory  - Article Details
CATEGORIES
Statistics
  • Active Links: 11
  • Pending Links: 25601
  • Todays Links: 3
  • Total Articles: 29
  • Total Categories: 7
  • Sub Categories: 21
top left cornertop right corner

General Programming Principles

Date Added: June 08, 2008 12:28:21 AM
Author: admin
Category:
This article will give the reader some insight about the general programming principles.  Learning how to code is the first step to become a software developer but being able to code doesn’t mean that you can write clean and maintainable code.

The fact is that there are many ways to code an application that can perform the same functionality, such as an algorithm to search and sort data from a text file or draw graphics on the screen. The code that is written by you doesn’t always mean that the code can be easily understood by another developer. You may know what your code does but other developers will not if your code is either:

- not formatted -  it’s very important to format your code for readability. Use tabs and enters where needed but don’t clutter your code into one long text of code or use unnecessary tabs and enters.

{
PS: All examples in this article are in Java but the concepts apply to programming in general.}

Example #1 (bad):
public class HelloWorld {public static void main(String[] args)
{System.out.println("General Programming principles article");}}


Example #2 (bad):
public class HelloWorld
public static void main(String[] args) {
System.out.println("General Programming principles article");
}
}

Example (good):
public class HelloWorld {
public static void main(String[] args){
System.out.println("General Programming principles
article");
}
}

Notice the long text of code in example #1 and the massive use of tabs and enters in example #2

- self explaining object names - do you know what I mean by:

public class HelloWorld {
String tmp = "dog";
public static void main(String[] args){
HelloWorld HW = new HelloWorld();
HW.setTheString("cat");
System.out.println(HW.getTheString());
}
public void setTheString(String tmp){
this.tmp = tmp;
}
public String getTheString(){
return tmp;
}
}

Notice this: String tmp = “dog”; ? setTheString(String tmp) ? , getTheString() ?
I have no idea what this application should do by judging the object names for this application.

But ok, just to be fair I can see that the String tmp holds the value “dog” and the getTheString(String tmp) will do something with a String?  Wouldn’t be easier to read if the names were changed into:

public class Animal {
String AnimalStr = "dog";
public static void main(String[] args){
Animal animalObject = new Animal();
System.out.println(animalObject.getTheAnimal());
animalObject.setTheAnimal("cat");
System.out.println(animalObject.getTheAnimal());
}
public void setTheAnimal(String Animal){
this.AnimalStr = Animal;
}
public String getTheAnimal(){
return AnimalStr;
}
}

With a little refactoring I can see that this is an Animal class that sets and gets an animal. Imagine how the code would looks like in a real world application with thousands and thousands lines of code with object names like: tmp, String2, intNumber, aDouble, useThisString etc. Well, I would go to the programmer that programmed this and ask for explanation because he has a lot of explaining to do.

- comments - when writing large blocks of code comment it!

Even the best developer won’t know what the code will do after months of not touching the code. Without comments to clarify what the code does the developer will easily lose track what it’s supposed to do and spent more time to figure out what it’s supposed to do than necessary.

When I write large blocks of code I write comments what the method is doing above the methods that I start creating like this:

// set the animal
public void setTheAnimal(String Animal){
this.AnimalStr = Animal;
}

or if you need multiple lines:

/*
* set the animal
*/
public void setTheAnimal(String Animal){
this.AnimalStr = Animal;
}

Of course there isn’t always a need to comment a method if it’s already obvious what it will do like
my example above.

Understanding the problem
Another important thing to remember is the ability to fully understand the problem. You might think that’s it’s better to code and see what the code does but you can’t just code something without fully understanding the specs of the application because if you don’t know what you are doing how are you supposed to write software that does what it needs to do?

What I usually do is to write down the problem step by step on a piece of paper and try to figure out how to solve the problem without a computer. After I’ve managed to write down the steps it’s only a matter of converting the steps into Java or any other programming language.

Conclusion
While there are many ways a developer can code software, there are guidelines to remember so that it makes the code easier to read. Another important thing is to understand the basic OO concepts that I haven’t discussed about it yet but understanding the OO concept is crucial in programming as everything can be assumed as an object .

Ratings:

You must be logged in to leave a rating.

Average rating: ( votes)

Comments:

No Comments Yet.

You must be logged in to leave a comment.


bottom corner leftbottom corner right
Latest
Articles
WordPress Platform to Mobile Site
Mobile sites have become the part and parcel of WordPress development with the rising trend of users accessing Internet using their smartphones and tablets. WordPress plugins play a vital role in mobile website development.
Android vs Windows Apps
Android development is well ahead of Windows and others in the race. This is due to large number of applications available in the market on the Android platform. Most of these applications are freely available unlike Windows.
Tips to Send Bulk of Messages from Your Blackberry Mobile Phones
How can you send thousands of text messages from your computer devices to multiple users’ cell phones? This article provides you the important tip to compose and forward bulk of messages from your computer and mobile phones in just few minutes.
Why WordPress Is Best For Business
WordPress is one of the best solutions when it comes to a business website. It is robust in nature and offers great customization options to suit the needs of each and every business.
WordPress Search Engine Marketing Plugin
Search engine marketing plugins are very important in WordPress development as they help in increasing the viewership of a website.