Saturday, December 27, 2014

Ruby Program including basic method & ping command

puts 'hello world'
name = "Solution"
print name + "\n"

puts name.reverse
puts name.center(30)
puts name.upcase
puts name.downcase
puts name.ljust(40,'*')
puts name.class
puts Time.new
puts Time.class
puts "We are part of #{name}"
puts name.include?("u")
puts name.start_with?("s")
name[8] ="s"
puts name
number = 12
puts "Number convert into string  #{number.to_s}"
puts "number.class #{ number.class}"
ip = "192.168.1.1"
x = `ping -c 4 -i 0.2 -s 4 192.168.0.1`

Friday, December 26, 2014

How to write best code?

1. Use a Coding Standard

It’s easy to write bad, unorganized code, but it’s hard to maintain such code. Good code typically follows some standard for naming conventions, formatting, etc. Such standards are nice because they make things deterministic to those who read your code afterwards, including yourself.

2. Write Useful Comments

Comments are crucial. You won’t appreciate them until you leave your thousand-line script for a couple of days and return to and try and make sense of it. Useful comments make life easier for yourself and those after you who have to maintain your code.

3. Refactor

Code refactoring is the eighth habit of highly effective developers. Believe it or not, you should be refactoring your code on a daily bases or your code is not in good health! Refactoring keeps your code healthy, but what should you refactor and how?

4. Avoid Global Code

Global variables and loops are a mess and can prove problematic when your application grows to millions of lines of code (which most do!). They may influence code elsewhere that is difficult to discern, or cause noisy naming clashes. Think twice before you pollute the global namespace with variables, functions, loops, etc.

5. Use Meaningful Names

Never use names like $k$m, and $test for your variables. How do expect to read such code in the future? Good code should be meaningful in terms of variable names, function/method names, and class names. Some good examples of meaningful names are: $request$dbResult, and $tempFile(depending on your coding style guidelines these may use underscores, camelCase, or PascalCase).

6. Use Meaningful Structures

Structuring your application is very important; don’t use complicated structures, always stick to simplicity. When naming directories and files, use a naming convention you agree upon with your team, or use one associated with your coding standard. Always split the four parts of any typical PHP application apart from each other – CSS, HTML Templates/Layouts, JavaScript, PHP Code – and for each try to split libraries from business logic. It’s also a good idea to keep your directory hierarchy as shallow as possible so it’s easier to navigate and find the code you’re looking for.

7. Use Version Control Software

In the old days, good development teams relied on CVS and diff patches for version control. Nowadays, though, we have a variety of solutions available. Managing changes and revisions should be easy but effective, so pick whatever version control software that will work best with the workflow of your development team. I prefer using a distributed version control tool like Git or Mercurial; both are free software/open source and very powerful.

8.Use Automated Build Tools

Try to use tools like Ant or Phing to get your source prepared, compressed, and deployed. Building your whole application with a single command is a marvelous way to prevent errors and omissions that are inherent when performing repetitive tasks, and is a generally core pre-requisite for automated testing strategies. I recommend using Phing, it’s a well-supported build tool for PHP written to mimic Ant; if you aren’t familiar with it, check out Shammer C’s article Using Phing, the PHP Build Tool and Vito Tardia’s article Deploy and Release Your Application with Phing.

9. Use Code Documenters

For large applications spanning several classes and namespaces, you should have automatically generated API documentation. This is very useful and keeps the development team aware of “what’s what.” And if you work on several projects at the same time, you will find such documentation a blessing since you may forget about structures switching back and forth between projects. One such documenter you might consider using isDocBlox.

10. Use a Testing Framework

There are a plenty of tools that I really appreciate, but by far the ones I appreciate the most are the frameworks that help automate the testing process. Testing (particularly systematic testing) is crucial to every piece of your million dollar application. Good testing tools are PHPUnit and SimpleTest for unit testing your PHP Classes. For GUI testing, I recommend SeleniumHQ tools.