Archive for the 'programming' Category

Creating Action Controller in PHP

We could handle actions in PHP like this…

<?php

$actions = $_GET['action'];

switch( $actions ) {

case ‘hello’:

say_hello();

break;

case ‘bonjour’:

say_bonjour();

break;

default:

echo ‘Unknown action ‘.$actions;

}

?>

But this way is hard to extend and hard to maintain. How about adding say_konichiwa() action? We need to add a function and need to add a case in switch statement. For database management system, we need at least 6 actions. Show List View, Add New, Show, Edit, Update and Delete. If we put all those actions to one page like about example, it will be ugly and hard to read. And, we definitely will need to add another action.

We could make Ruby on Rails style Action Controller in easy way. First we need to create .htaccess for RESTful url.

.htaccess

RewriteEngine on
RewriteRule ^action/([a-z0-9\-\_]*)$ index.php?do=$1 [NC]
RewriteRule ^action/([a-z0-9\-\_]*)/$ index.php?do=$1 [NC]

Then, we need to create three scripts. index.php, actions.php and action_controller.php.

index.php

<?php include(‘actions.php’); ?>
<html>

<head><title>Index</title></head>

<body><?php actions(); ?></body>

</html>

actions.php

<?php

include(‘action_controller.php’);

function say_hello(){

echo ‘Hello World!’;

}

function say_bonjour(){

echo ‘BONJOUR !!!’;

}

?>

action_controller.php (the trick)

<?php

function actions(){

$action = $_GET['do'];

if($action == ){

if(function_exists(‘index’)){

index();

} else {

echo ‘There is no index action !’;

}

} else {

if(function_exists($action)){

//calling function same name with $action

$action();

} else {

echo ‘Unknown Action ‘.$action;

}

}

}

?>

That all…

Now, we can access our say_hello actions with…

http://domain/project-dir/action/say_hello/

If we want to add say_konichiwa action, we can simply put say_konichiwa function to actions.php and we can call our action from browser with ( http://domain/project-dir/action/say_konichiwa ). That is easy to maintain, easy to extend and nice (i think :D).

Have a nice day…

Compiled Languages vs. Scripting Languages

Compiled Languages
Computers do not actually understand the code we write. We need to translate our human-readable code to machine-readable code. Our code need to be translated into bits and bytes that can be executed by computers. This translation process is called compilation. Languages that require compilation are called compiled languages. C, C++ and Java are compiled languages.

Scripting Languages
Languages that not require compilation are called scripting languages. Perl, PHP, Python and Ruby are scripting languages. Those languages rely on our source-code all the time. Scripting languages didn’t have compiler or a compilation process. Those languages used interpreters to translate our source-code to machine executable code on the fly.

Compiled Languages vs. Scripting Languages
Mostly, we have been using compiled languages like C, C++, Java and Visual Basic. Java has an interpreter but it need to compile first. Earlier versions of Visual Basic were interpreted. Visual Basic 5 and later came with compiler. So Java and Visual Basic should be called as compiled languages.

Imagine; you wrote an application with Java. Then you compile your application with javac. After compiling completed, you run your application. When running your application, you notice a bug. To fix it, you have to stop your application, go back to source code, fix the bug, wait for the code to recompile, and restart your application to confirm that it is fixed. And if you find another bug, you’ll need to repeat that process again.

In a scripting language, you can fix the bug and just need to reload your application —no need to restart or recompile anymore. It’s as simple as that.

But, the scripting languages have a big performance problem. Translating on the fly can affect the application’s performance. Typically, the pre-compiled code is faster than on the fly translating. But, good news is, there are ways to speed up scripted languages, including techniques such as code caching and persistent interpreters.

Why shouldn’t beginner learn VB first?

First of all, I need to say that I’m neither a guru nor expert. I have no idea to consider and compare about programming languages. But I think I can give some advice to beginner.

Yesterday, my friend asked me that he wanted to start learning programming. He wanted to know which language he should learn first. He heard about some visual languages. Someone told him, those visual languages are very easy to learn and will give him great productivity. “Is that true?” He asked. How should I say?

In my opinion, if the beginner learns Visual Basic first, it can misunderstand about the programming. I don’t mean that Visual Basic is a bad language. Visual Basic is the language of choice to develop desktop application for me.

Visual Basic is a useful language. But beginner shouldn’t learn that language first. It can only be wasting their time by trying to understand textbox instead of trying to understand string processing. They will make double click on a command button to write a command1_click() procedure but they can’t clear understand about object, method and event. They think Visual Basic itself a programming language and they also can’t clearly understand what programming language is and what IDE is. They will blame other languages for not having great drag and drop IDE. And then they won’t try to understand memory management, threading, garbage collection and other important programming feature. They will think those features are not necessary to understand.

After having some experiences in developing applications, they can develop some windows-base small applications. But they will face some difficulties in learning other languages like Java, PHP. Because they can’t develop without drag and drop IDE. And they will blame those languages again.

In the real programming world, it is impossible to emphasize on only one language. We must know various languages. If beginner learn VB and love it, they will find that difficult to learn other language. If s/he learns a language like C or Java first, studying other language will be easy-going.

Believe it or not, I have never heard about regular expression within 2 years experiences as a professional because I’m a VB developer.

Top tips every programmer should follow

10 Programming Languages You Should Learn Right Now

  1. PHP
  2. C#
  3. Ajax
  4. Javascript
  5. Pearl
  6. C
  7. Ruby and Ruby on Rails
  8. Java
  9. Python
  10. VB.NET

Top Ten of Programming Advice NOT to follow

  1. Use error codes instead of exception
  2. Use unsigned integers for values that can only be positive
  3. Design classes parallel to their physical counterparts
  4. Make sure your team shares a common coding standard
  5. Write lots of comments
  6. Use accessors or properties rather than public fields
  7. Use the singleton pattern for variables that you KNOW you should have only one instance of
  8. Be tolerant with input and strict with output
  9. Code all the corner cases immediately, cause otherwise you’ll never go back and fix things
  10. Design first, then code

If you are a project manager or team leader, you should answer those question.

The Joel Test: 12 Steps to Better Code

  1. Do you use source control?
  2. Can you make a build in one step?
  3. Do you make daily builds?
  4. Do you have a bug database?
  5. Do you fix bugs before writing new code?
  6. Do you have an up-to-date schedule?
  7. Do you have a spec?
  8. Do programmers have quiet working conditions?
  9. Do you use the best tools money can buy?
  10. Do you have testers?
  11. Do new candidates write code during their interview?
  12. Do you do hallway usability testing?

Six ways to write more comprehensible code

  1. Comment like a smart person
  2. Use #define a lot. No, a LOT
  3. Don’t use variable names that will mock you
  4. Do error checking. You make errors. Yes, you
  5. “Premature optimization is the root of all evil.” - Donald Knuth
  6. Don’t be too clever by half