Saturday, May 7, 2016

which, whereis & locate

"which" command to find out if a relevant binary is already in your search path. For example: which php

"whereis" searches a broader range of system directories and is independent of your shell's search path.

"locate" consults a precompiled index of the file system to location filenames that match a particular pattern. "locate" can find any type of files as well, and its database is updated periodically by the updatedb command.

Friday, October 19, 2012

NETWORKING:packet addressing

network packets must be properly addressed in order to reach their destinations. Several addressing schemes are used in combination:
• MAC (media access control) addresses for use by hardware
• IPv4 and IPv6 network addresses for use by software
• Hostnames for use by people

Hardware (MAC) addressing
Each of a host’s network interfaces usually has one link-layer MAC address that distinguishes it from other machines on the physical network, plus one or more IP addresses that identify the interface on the global Internet. This last part bears repeating: IP addresses identify network interfaces, not machines.

IP addressing

The mapping from IP addresses to hardware addresses is implemented at the link layer of the TCP/IP model

Hostname “addressing”

IP addresses identify a machine’s network interfaces, but they are not specific enough to address individual processes or services, many of which may be actively using the network at once. TCP and UDP extend IP addresses with a concept known as a port


TCP/IP is a protocol “suite,” a set of network protocols designed to work smoothly together. It includes several components:

• IP, the Internet Protocol, which routes data packets from one machine to another (RFC791)
• ICMP, the Internet Control Message Protocol, which provides several kinds of low-level support for IP, including error messages, routing assistance, and debugging help (RFC792)
• ARP, the Address Resolution Protocol, which translates IP addresses to hardware addresses (RFC826)2
• UDP, the User Datagram Protocol, which provides unverified, one-way data delivery (RFC768)
• TCP, the Transmission Control Protocol, which implements reliable, full duplex, flow-controlled, error-corrected conversations (RFC793)

These protocols are arranged in a hierarchy or “stack”, with the higher-level protocols making use of the protocols beneath them.

Saturday, March 3, 2012

PHP5.4 short array syntax

We used to define an array using the array() language construct:
 key => value,
 key2 => value2,
 key3 => value3,

As of PHP 5.4 you can also use the short array syntax, which replaces array() with [].
$array = [
 "foo" => "bar",
 "bar" => "foo"
Good to see that we have another flexibility and can be lazier. But, to developers who highly agree with Python's philosophy 'There should be one-- and preferably only one --obvious way to do it', this may not be necessarily good. Another rule must be added in their development teams to specify which style should be used in projects.

Monday, November 21, 2011

find tables of a particular storage engine in mysql

If you have a database with some tables using MyISAM and some using InnoDB, you probably want to find out which tables are using InnoDB. 

SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'database_name' AND ENGINE = 'InnoDB';

It is also easy to find out a specific table's storage engine:

SELECT ENGINE FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'database_name' AND TABLE_NAME= 'table_name';

Wednesday, October 12, 2011

linux format xml command line

i simply use xmllint --format source.xml --output output.xml

php refactoring: Sprout Method

Sprout Method is a concept from the book 'Working Effectively with Legacy Code'. The method is particularly useful when working with legacy PHP code. I use this method quite often although i never know this technique is called 'Sprout Method' until i read the book. 

The basic idea of Sprout Method is, when we need to add a feature to a system and it can be formulated completely as new code, write the code in a new method. Call it from the places where the new functionality needs to be. Theoretically, the new code should be testable, although it may still be hard to get the calling points under test easily.

I've seen quite a lot legacy PHP applications. One feature of these legacy code is large long procedures full of dependencies. Developers tend to keep injecting new logic into the procedure when they need to add or change something, because that seems to be the fastest and safest way to make a change. But, obviously, that is also why and how large long procedures are created. 

We should use Sprout Method whenever we can see the code that we are adding as a distinct piece of work or we can't get tests around a method yet. It is far preferable to adding code inline. The steps of using Sprout Method is:

1.Identify where we need to make our code change.

2.If the change can be formulated as a single sequence of statements in one place in a method, write down a call for a new method that will do the work involved.

3.Determine what local variables we need from the source method, and make them arguments to the call.

4.Determine whether the sprouted method will need to return values to source method. If so, change the call so that its return value is assigned to a variable.

5.Develop the sprout method. If one method is too big, then break it into several smaller methods until we think each method's logic is so simple and clear that we can easily test them. 

The advantage of using Sprout Method is quite obvious. When we use Sprout Method, we are clearly separating new code from old code. Even we still can't get the old code under test immediately, we can at least see our changes separately and have a clean interface between the new code and the old code. A method/function actually creates a local scope, whatever we do inside there, we don't have to worry that our local variables and changes in the method could probably get messed up with the old procedure.

Everything has two sides. The disadvantage of Sprout Method is the source method might contain a lot of complicated code and a single sprout of a new method. Sometimes it isn't clear why only that work is happening someplace else, and it leaves the source method in an odd state. But at least that points to some additional work that we can do when we get the source class under test later.

We probably can't see the benefit of a Sprout Method immediately. In fact, we probably will never see it if we never have to come back and make changes. But as long as we need to come back and make changes, the good side of Sprout Method will come up immediately. A developer will feel much more safer and comfortable working with a Sprout Method than making changes in the long procedure.