Thursday, July 22, 2010

Please don't scribble.... Its all about Keeping the code clean

I was travelling to the past... don't mistake me... I really don't have a time machine... it was only with the help of my memories....

Just went to 1992.... Should be 1992.. I remember my mom was waking me at morning 5:30 for an exam.... Hmmm.... Thats too early.... I still remember how sleepy I felt... She was asking me to write something and I was scribbling.

Mom: Don't scribble (Bang..... a nice hit on my head).
Me: zzzzzzzzzzz........................zzzzzzzzzzz

Mom: Wake up.... Don't sleep....... you have to score first in the class......
Me: I feel sleepy.... What do I want to do now......
--------------------------------------------------
2000.... At the school...
Me: Teacher... All my answers are right... but why have you reduced 1 mark per question?????
Teacher: Don't scribble.... Just present it well to get full marks....

Me: Why do I want to present well, the test is conducted to know that I have learnt it or not.....
Teacher: Ofcourse you are right... but, how can I know that, if I cant read what you have written???

Me: ??????
Teacher: Writing neat is a good practice.. it helps you to read your answer paper at any age. May be now you can read your handwriting but when you grow you may not be able to read the same. So it is better to write neat that you can read it for ever in the lifetime.
--------------------------------------------------

2002.... I am not sure about the date..... At NIIT , Coimbatore
Friend: Sujai...... Could you please help me in debugging this problem.
Me: Yes... Let me look in to the code....

After looking at the code....
Me: ??#?#%$#^$%^$^$^&%&%&
Me: Friend, I think you need to write the code better..... because I can't read it. Programing is not just making things work for now... but writing something that works for ever / maintainable for ever.
--------------------------------------------------
2008... Started my career as Software Engg.

Kari: Sujai.... write the code cleaner. It is always now or it will never happen again.
Sujai: mmmm.....

Kari: Give a space... here.. Why are the variables not understandable......

Yes, I started to learn it better, Still learning to write a neat code, This will evolve for ever...
--------------------------------------------------
Through out the career, I have observed more scribbled code, than neat code. It is because, most of us are not evolving further, after we start our career. We think of making things work on a rough go and finally we scribble to write faster code. Writing a clean code needs time in the early stages to practice but later you are in the same speed, like the rest.

Anonymous: Hey.... Stop saying about clean code.... What do you say it clean or neat..
Me: Basically, a clean code is a code without bug... but am not mentioning that.. I am saying how to write a code that looks beautiful / readable for anyone.

The industry comes up with too many standards to write a good code. I just want the PHP developers to follow one among them rather than scribbling.

Few known standards

We have too many standards... Ofcourse we have our own standard. But it is better that our standard matches the existing standards...

I would recommend to use Zend Coding Style, it explains better than others. I have my own style, which mostly meets to all the above standards with very few changes..

Question: Hey I have a team, How can I insist this to everyone, Things will run out of control. We can't have someone dedicated to look into the code of everyone to keep it better...
Answer: Use phpcs (php code sniffer). Set a standard on your own or derive from industry standards, finally ask everyone to pass the phpcs validation, before they deliver the code.

The coding style is like a handwriting for a person, which need to be readable and understandable by everyone. Don't try to make your coding style unique... try to to match to the standards so it would be easy for everyone to follow when they need to continue your work.....

Hope, This post explains, why and how(refer to the links above) to write a clean code...

Soon, An article on how to use our own phpcs standards and use it in the team will be posted, keep reading.

Saturday, April 10, 2010

Why PHP? When do we need PHP? PHP vs Java vs Ruby vs ASP.......

We often scratch our heads, pluck all the hair left, when it comes to a decision for a project, what to choose as the base development language. What is the best language is often a question for everyone.

The truth is there is no such language which is always best solution for all problems. Going through the search, few say Java is the best(Really? Not always). Ruby is the best(Again, Not always). PHP is the one(Again, not always). We can find a lot of Pro-Java, Pro-Ruby and Pro-PHP posts, fighting within them and saying that they are the best.

Am I gonna say..... PHP is the best? No way...... According to me PHP is a good scripting language, not less to any other language, but not always a right solution for all problems.

In the race of web application development almost 99% of the languages in computer programing started providing web development solutions and frameworks to achieve. The most commonly said in this race are ASP, PHP, ASP.NET, Java (JSP, Struts, JSF, ..... etc), CGI (C++, C, perl, python) and more working on this to make a web application.

During the race of development few languages were optimized specially for web, Some of them are, ASP / ASP.NET and PHP, While Java also added few more web development options and provided wide variety of frameworks.

There is a wide argument between the developer groups on
PHP vs Ruby
PHP vs Java
PHP vs ASP / ASP.NET
PHP vs Python
and even more.....

We can never say something is better than the other because almost all the languages are influenced by one another in some parts and have inherited their features, while choosing them for our requirement may have some factors involved.

People always misjudge and go ahead with a single factor in comparison. When a comparison comes to me, I have a wide scope of factors to choose the best.
  1. How big it is? (How long are we gonna develop this)
  2. Will it keep growing?
  3. How much are we ready to spend for hosting?
  4. Do we need to have custom architecture?
  5. How easy to get the task done by a new programmer?
  6. Will it perform on a huge load ?
How big it is?
Is your application is like a huge meat piece? Are you trying to feed a kid with that?
Or is your Application is a toy for the Kid?
Or is Your application a baby rattle for an Adult? This matters for the application and language we choose.
When the application is huge... an enterprise application which is going to be developed for years... or the application has huge number of modules, services and more.... Forget about PHP, We cannot manage so huge architecture in PHP, If we try to.. we will end up with so much messy code and tough to maintain. In that case Java would be my choice....

Will it keep growing?
 Hmm.... Are we sure our hip size won't change? Do we need a free size pant? or Are we gonna change the dress when the size changes each time? This is similar for our application too, The language decides its growth support.
Today the application is with 20 files, but will it keep growing, on the business enlargement? If no the size of the project matters here...
Is this a static site with very few, dynamic areas? PHP is the right choice....
Is this a content managed site? You can go with PHP.... we have a wide support and user base for this.
It is dynamic, very small, will not grow... Go with PHP
It is dynamic, small, will not grow but needs maintenance, need fast development... Go with Ruby or PHP
This is medium size but will evelove slowly .... Go with PHP it is good have growing sites.

How much are we ready to spend for hosting?
Buying a veicle might be easy sometimes.. But do we have monthly budget to fuel it and maintain it?
Yes... Hosting is such a factor, Sometimes development might be so cheap to spend once... How about spending monthly on the hosting which goes higher than our income from the application?
Hosting is extremely cheap in PHP, Almost 99.99% of the existing web hosting providers support PHP.
Ruby is a good to develop faster but the expense goes higher in hosting.
Java is very expensive when it comes to hosting. Because, we need a dedicated server to run a better web app.

Do we need to have custom architecture?
Are we gonna buy a built flat and stay in it? Or we alter after the purchase? Or we are gonna build our own style of house? House and a Software application have few similarity... Altering them later on a big way is tough and expensive....
Frameworks and Architectures of the application plays a major role in how fast we develop and make the delivery(even maintenance too).... We have many RAD(Rapid Application Development) Frameworks in all the languages mostly. Ruby was one of the major cause to push all the languages for the RAD Frameworks, PHP / Java has RAD frameworks, RoR / JSF / Zend / Symphony / CakePHP and even more. But When we need custom frameworks having things to go beyond the frameworks, It is good to think what supports it? PHP frameworks can be written custom or we can merge couple of frameworks as we do in Java... So this needs to decided for development.

How easy to get the task done by a new programmer?
Is your application going to do some rocket science? Or Gonna play with simple things?
If the application is rocket science, The programmer should be a an expert irrespective of the language...
But if the application is simple, do we need a language that needs experts to work with? How much are we gonna spend for the programmers then? Ruby and PHP plays a major role in getting freshers and new programmers start things and implement faster without much of time in learning....

Will it perform on a huge load ?
Hmm............. Are we trying to pull a truck full of load in a motor bike??? Or is our truck is overloaded? If yes do our truck can be energized to pull the overload? The language performance plays a role here, and the support for optimizing the language servers plays next role, The architecture for load balancing plays further? But the performance is not only for some places where the load plays a role... it is on even a small application too...

There is wrong way of looking into anything results in unexpected results...
Is PHP fast or slow? fast and slow never means anything without numbers...
Some Performance test gives very less score for PHP - Is PHP slower than Ruby? Another one here
But the way language works and the bench marks done may not be the right way of usage, If one of the performance says C++ is fastest, PHP uses inbuilt functions with C++ library calls, but the test was not done, by implementing the inbuilt function redone with pure PHP code... which means the test is done for a different purpose, but used for a different purpose.

OK..... OK..... What is the solution? Are we here to discuss PHP is fastest? No way... Its, just to say the benchmarks actually don't make a sense as the way it was done, is never real-time scenario.... Yes, Wikipedia / Facebook are few simple examples, where PHP can take the overload when the application has much load, like Java

    Here are some points saying why PHP is a good option.
    1. Easily available to host (Almost 100% of the web servers provide support for PHP)
    2. Cheaper in cost than other solutions (A shared CPanel hosting cost just cost 20$ / year)
    3. We can get wide support, as the community is very huge.
    4. PHP is open source, don't need to worry about the licenses.
    5. IDE / Webservers are also freely available to support the development.
    6. Light weight - Can host "n" websites on single server, unless the traffic is huge for all sites.
    7. Better productivity - Easy and faster to develop, provides a range of in-built libraries.
    8. Very good for small and medium scale apps, while large scale apps needs a different/deeper analysis.
    Not all of the above are available with its competitors, PHP stands unique in this, which says it is a best solution on many cases(remember not always).

    Java is good as a language, but heavy to use, needs experts to develop, costly to deploy.
    Ruby is good in fast development with its frameworks, but has very few hostings to support it.
    ASP.NET needs expensive Microsoft platform investments, cannot be compared even with others.

    Note: We have not spoken about ASP / ASP.Net over here, because of two reasons. We can't speak about something that we don't know... the other is we have only compared with open-source languages which are available for free(free beer) not with something which has to be compared after a purchase...

    Hope we have a crisp and short information, why to choose PHP over other different languages.
    Keep commenting if you feel otherwise. ;-)

    Friday, April 2, 2010

    What is Undressing PHP all about?

    The reason for the blog... stays in the past, While the goals stay in future

    Long ago, some time in 2004. Remembering a talk with my friend Chakku.
    Chakku, a Java Developer, working in web technologies. We feel so happy about Java for its own advantages. I had a small web site coming up for my uncle's company, We bought a space of 20MB for the cost of Rs.1000 from a CPanel Reseller. The only option for a dynamic content site over there is with PHP4 . The dream weaver helped to achieve what is needed for the website.

    We started debating on why was Java not chosen for the site, while it is best of the technologies? The answer from is PHP is so simple and cheaper to go for the time.....

    Yes, PHP is better than Java..... for the problem that came for the website. Later, after working in Java, I returned to PHP for a need. That gave me a push, to look for standards and architectures in PHP, which was lagging in the PHP community, very few have the standards in the way they deliver with PHP, PHP itself was not mature to handle enterprise applications, but was moving towards that, while the developer community (not all, but most of them) was just in a hurry to deliver products just like a code written in scratch pad. Applications are un-maintainable and lagging standards......

    This urged to start a blog and explain:
    What wonders can PHP do.
    How to make the wonder stay for a long run......
    PHP is not less to any language..... It just has its own advantages....

    So, we are here to discuss on how things can be achieved in PHP, in the right way, Undressing and revealing secrets, powers of PHP

    This blog will contribute and gather ideas from various members of the web community, providing better solutions and ideas towards the improvement of our-self in PHP, taking ourself to the next level, finally undressing PHP to make everything about PHP, visible to everyone.