Você está na página 1de 14

HOW TO WRITE

EFFECTIVE CODE

By Martin Nad martin_m_nad@yahoo.com

How to write Effective code

OVERVIEW

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.

Page 2

How to write Effective code

TABLE OF CONTEXT:

FORWORD.3
PRINCIPELS.4
SIMPLICITY5
SRP: THE SINGLE RESPONSIBILITY PRINCIPLE .6
THE LAW OF DEMETER7
PRACTICAL TIPS.....8
MY OTHER DOCUMENTATION.10

Page 3

How to write Effective code

How to write Effective code


FORWROD

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

Page 4

How to write Effective code

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.

Page 5

How to write Effective code

Page 6

How to write Effective code

PRINCIPLES

Some of those principles are:


- Simplicity
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.

Page 7

How to write Effective code

- SRP: The single responsibility principle


Every class, function should just have one
responsibility and it should serve just that
responsibility.
- Open close principle
A 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)

Page 8

How to write Effective code

- 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
o
o
o

O itself
M's parameters
any objects created/instantiated within M
O's direct component objects

One of the problems here is chaining access:


someValue =
objectA.getSomeFunction().getSomeFunctionFromO
bjectB();
and this situation is exactly as the same as
someValueA=objectA.getSomeFunction();
somValueB=someValueA.getSomeFunctionFromObj
ectB();
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.

Page 9

How to write Effective code

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.

Page 10

How to write Effective code

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 databaseconnection 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.

Page 11

How to write Effective code

- 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...

Page 12

How to write Effective code

MY OTHER PAPERS

Properties in Java and Spring by Martin Nad


Spring and Cxf by Example
Jboss and Perm Gen Error
Using Ftp Service With Spring
JunIt
How to use Maven
ReFactoring
Maven and Custome Archetype
How to Write Effective Code
Using Generic in Java Why and where
Page 13

How to write Effective code

Java Caching
what is new in java 5

Page 14

Você também pode gostar