How to Write Effective Code

download How to Write Effective Code

of 14

Embed Size (px)


Description to how to write source code

Transcript of How to Write Effective Code

How to write Effective code

How to write Effective code

How to write Effective code

By Martin Nad [email protected]


In this documentation, I will give you some important tips to write effective code.

Here I will discuss about principles and rolls to think about before you begin to write your application.

To have a good structure and design for your application is the most important task in your application. It can help you to easier to maintain it.

As we know about 90% of time of an application owns by maintains. To try to have right codes from beginning with right structures and architecture, you can reduce the time of maintained.

Every manager and absolutely the programmer should think about it.

Table of context:




SRP: The single responsibility principle .6

The law of demeter7

practical tips.....8

My other documentation.10

How to write Effective code


I was writing on a documentation about how and why refectory an application, and I found that the discussion about refactoring is much closer to how to write your code in right way to have a good structure.

Refactoring is just about to improve your code and internal structure. If you from beginning thinking about good structure and design you should spare lot of the time to refactoring and debugging.

And it should be a good idea to try to have a good structure and design (relative, Im agree that we cannot have an right application from beginning, we need some reviewing when we are finished with developing the application) already from beginning.

One of the differences between software engineering and the other engineering as building is, you havent any determined module to build an application.

To write codes from beginning in an effective stile needs mostly experience and it cant to explain very shortly all detail and fast.

There are some principles that you should follow and mostly you are done.

Here I m going to describe very clearly all principles that we know about them with very practical examples, and I will look at them case by case.

For a software company, it is essential to think about those terms, and it should be important to other company that try to develop theirs softer by them self too. Good software shouldnt be very costly when you want add features or debug it or for any changing that you need.


Some of those principles are:


Try to write your code simple and find simple solution for every complex problem.

Perfection is in simplicity

There was a time ago; I thought if I wrote my codes in very complex case, it would show how good I am. But after a time, I found that it is more difficult to think simple and find simple solutions and write in some way that everything should be clear and everybody can understand.

Try to write very simple, clear and everybody can very easy follow your codes/application and understanding the application.

It can help you even to find very easy bugs and put new features to the application.

I believe that always there is one simple solution to every complex problem.

SRP: The single responsibility principleEvery class, function should just have one responsibility and it should serve just that responsibility. Open close principleA function, class or software should be open for extending and close for modification and changing.

Here there are two different way to work

1- Using abstraction or

2- Using inheritance

In both methods you should avoid to have global variables and all members variables should be private.

If you using C++ you should think about RTTI (Run Time Type Identification)

The law of Demeter :More formally, the Law of Demeter for functions requires that a method M of an object O may only invoke the methods of the following kinds of objects: O itself M's parameters any objects created/instantiated within M O's direct component objectsOne of the problems here is chaining access:

someValue = objectA.getSomeFunction().getSomeFunctionFromObjectB();

and this situation is exactly as the same as



Here it is harder to find the Demeter problem.

This case can be one of most important in your application or software. It can open your system for hacking. You should be very careful about this part in your business logic; I will show by an example what I mean with it.

Actually every application should refactor and finds this issue in application.

The first principles Simplicity It doesnt describe how you should write correct your code, it is about how you should think and you should resolve your problem and make a structure or structure.

If you have those 4 principles, in your mind before you beginning to write any application from scratch, your after work will be very easier and cost effective.

practical tips

And there are some practical tips to writing a well understandable application and codes

And some of them are:

- using the standard structures. And be sure it doesnt get to complex structures.

For example if you are using database-connection you should have a directory for database-connection, and inside it you should have a collection of interfaces and for example a new directory for implementation. If you havent too many files, it is not necessary to have a new directory for implementation of interfaces.

- Chose right class, function, variables name from beginning.

The class, function and variables name should exactly tell you what they are about and nothing more or less.

You can save lot of unnecessary comments here.

- Avoid to use break, go to, continue and the other statement that destroy your internal structure.

- Avoid using anonymous classes

-function shouldnt be big

-define a new class for if-else statement or switch-statement

- Think about the length or your line

-Think about the numbers of line in your function and class.

- Think about the number of argument of a function, one argument is perfect, two arguments is ok, but more than tree argument is not recommended.

- try to use an object as argument if the numbers of argument is too many (more then 3).

-None of functions should return null

To be continuing...

My other papers

Properties in Java and Spring by Martin NadSpring and Cxf by ExampleJboss and Perm Gen ErrorUsing Ftp Service With SpringJunItHow to use MavenReFactoringMaven and Custome ArchetypeHow to Write Effective CodeUsing Generic in Java Why and whereJava Cachingwhat is new in java 5 Page 12