Wednesday, February 11, 2009

Coupling and Cohesion

As an overview, those two words are the top players in a game where you play with lots of OO cookies, if you can manage those two bad boys, you practically can get rid of all of your books about OO design. And yes, if you could compress all those book in to two little simple word it would be coupling and cohession.

it's about flexibility, reusability, extensibility, maintainability, portability, and so on, when you learn or use patterns, they all come down to these words, so what is coupling and cohession, really ?

coupling is about how much dependency between two separate module, the higher the worst it get. why is high coupling is bad ?, because when two seperate module is too dependent with one another, it would be difficult to modify them, imagine if module A depends on module B, and B on C, and C on D, if you try to modify A, you'll end up modifying most(if not all) other module, its a domino effect, one down, and everything else is just a matter of time.

While cohession is about how solid your module are, solid here means it does one thing it and it's very good at doing it, the higher the better it get, why is low cohesion is bad?, if you make a module that do stuff too much, you'll end up damping lots of logic in one place, which is bad because if any of those logic change, you might breaking other logic that sits in the same module. This is where the separation concern comes in, each module should only have one responsibility, if you have more than one reason to change a module, there is a big chance that module has low cohesion,

Ironic it is, but as you can guess, those two words are complementary, if you strive for one you'll get kick by another. for example if you want to make your module to have high cohession, you'll tend to squeeze it as much as possible, and by the end of the day that module will need to communicate to many other module to accomplish a bigger task, and then you'll realize that you had a high coupling. In other hand if you want to make less chit chat between modules, you'll end up with one big chunk of module which mean it has low cohession.

A good design is a harmonic balance between coupling and cohession.

whhatt is that? how you do "harmonic balance" ? how much is high, and how less is low? well I cant really tell as I'm still learning, but one thing I can tell is that a harmonic balance is not a 50:50 share, it depends on a dozen of variable, necessity, point of view, probability of change, difficulty, scenario of use, etc.. sometime you get to sacrifice one for another, and sometime we just can't figure it out...

Tuesday, February 10, 2009

User Input From Vim to Python script

hi, ever wondering how to prompt a user for some input from python script within vim, but first make sure you have imported the vim module

def python_input(message = 'input'):
vim.command("call inputsave()")
vim.command("let user_input = input('" + message + " : ')")
vim.command("call inputrestore()")
return vim.eval('user_input')

now everytime you wanna prompt user, in your python script do something like

input = python_input('input something')

it will prompt the user to input something with 'input something' as the caption of the prompt, in the command-line

for more detail

:help input()

hope, this would help.

Sunday, January 11, 2009

DBGpClient v1.0.2: Vim PHP debugger update

I've said it before, that I'm not able to work with this script at least for next month, sorry.. cant keep my promise ~_~ .. heres an update (the urge's really annoying, cant help it :p)

###############################################################################
1.0.2 changelog from 1.0.1
###############################################################################

changes :
- add '!' on function declaration to override any declarated function with the
same name thx to Michael Bahnmiller
- add new path to check for a windows system. If you are using windows put the
files (debugger.vim and debugger.vim) your $VIM/vimfiles/plugin this usually
"C:\Program Files\Vim\vimfiles\plugin" thx to ning

bug fixes :
- bugs on windows system regarding the file path (from xdebug xml message)
which append '/' at the beginning, thx to Ning for notified me
- cannot set breakpoint when the debugger isn't running (my bad) thx to Michael
Bahnmiller

known issue :
- do not reorder tab while debugging session is running!, unless you want to
see crazy things

as usual, you've got something to say, drop em here.. thx !

download update

Friday, January 09, 2009

DBGpClient v1.0.1 : Vim PHP Debugger Update

Been working all day for this :

###############################################################################
1.0.1 changelog from 1.0.0
###############################################################################

new :
- add option to use dedicated debugging tab (this way you dont have to worry
bout your undo history in other tab would get lost) suggested by Michael
Bahnmiller
- add option to set socket timeout

changes :
- update file header in debugger.py and debugger.vim
- if a debugging command reach code end, and the debugger stop gracefully, the
debugging interface will be closed/restore automatically, user doesnt have to
explicitly press F6
- the TRACE_WINDOW will only visible if the debugger is in debugging mode, or
when g:debuggerDebugMode=1, cause it's the only purpose of a trace window
(logging and tracing), in normal use you wouldn't use this window, it will
save more space
- "Not Connected" message is change to "Debugger is not running" message
- the F5 and F6 doesnt get map directly to python function, but to a VimScript
function, this way user can write a preparation for each debugging start, or
cleanups for every debugging ends
- add "except: NotConnectedException" for every try..except.. wrapper function
which display Not Running message, to make sure each of this command only
used when debugging session is started

bug fixes :
- remove error message duplication
- remove bug when timeout happen(trace window shown up, with a some exception
error) and show "Connection Timeout.." message instead


now we have g:debuggerDedicatedTab which will decide whether debugging session will use it's own tab, so the other tab (the ones to work with) doesnt lost its undo history, if you dont want an extra tab just for debugging set this var to 0, this will lost undo history on the tab where you start the debugging session, but note! if you want to use this *new feature and you are using gVim or vim.gui put this into your ~/.vimrc file

if has("gui_running")
if has("gui_gtk2")
set showtabline=2
endif
endif

the snippet above will make the showtabline option set to 2 (always show) this is needed because theres a bug about vim and gtk (don't know exactly) when vim window get resized

theres also g:debuggerTimeout which will decide how long vim will wait a debug session requested, I've set it to default by 10 second

and the last one is g:debuggerDebugMode, if you want to see what happen between Vim and xDebug engine, you might wanna set this variable to 1 which then will show trace_window, if it set to 0 the trace_window wont be visible

Perhaps I'm not going able to work with this script at least until next month, as usual if theres a bug, diffs, or idea, drop em here... thx !

oh, btw how do I report bugs on vimonline ? I cant seem to update my script page detail ?

download plugin update

Tuesday, January 06, 2009

Vim PHP Debugger Update

My previous post was talking bout how you'd use Vim with plugin as a debugger client for xdebug remote engine. If you already try it, there are some problem you might encounter, especially if you are using Vim 7.x.

The most annoying one is the lack of tab-page editing support, because original plugin is wrote based on vim 6.0 which didn't have tab-page support. when my Vim or gVim (I'm using 7.1) load several tab-page and I start a debugging session, everything still can work as expected as long I didnt change my working tab, but when the session ended, my tab-page goes like hell!. Moreover if I did change my working tab when the debugging session still running and continue tracing the code (by F2, F3, or F4), the window layout just went crazy, this is really a PITA..

below is list of changes I've made to the python script
  • when multiple tab-page loaded, it will remember the initial tab-page which start the debugging session. every debugging command will make sure that you are debugging on that initial tab, so debugging session wont mess other tab.
  • when debugging session ends on multi tab-page window, other tab-page is no longer being duplicated, it will only restore debugging tab
One thing to note, the undo history on the debugging tab would lost, while luckily other tab undo history is retain (even the previous version of plugin would lost all of its undo history), until Bram decided to make Vim able to store its undo history, everytime you want to debug your code just create a new tab and press F5 (or whatever you bind the command to), you can always use other tab to set breakpoint and such.

As I'm learning it, I'm planning to add more common debugging feature to this plugin, please if you find any bug, made a patch, diffs, etc, let me know..

vim plugin update

hope it helps..

Monday, January 05, 2009

Easy Remote PHP Debugging with VIM and Firefox

Happy new year !

I've write a post bout how you can setup debugging environment for PHP using xdebug, so now assuming you have xdebug setup and loaded, I'm going to write bout how you actually debug your PHP code.

Basically there are 2 type of debugging, local or remote debugging, the difference is that whether or not the debug engine separated with the debug client. If you debug a desktop Java code using IDE like Eclipse, most likely you are doing local debugging, where as the debug engine comunicate directly to the eclipse interface (there is no need of debug client). XDebug is a remote debug engine, which mean to actually debug some code we need a debug client to send command to and receive debugging information from the debug engine. From XDebug page we can get alot information bout how this can work, and some option of debug client that you can use (this page should be your first reference).

Now lets get to the chase, I'll write about how to have a remote debugging session with Vim (with plugin) and firefox to debug your PHP code easily.
  1. After you have successfully load xDebug module into php, enable remote debugging in your php configuration (php.ini) by adding
    xdebug.remote_enable = "1"
  2. download the debugger plugin (or watch my blog for an update)
  3. extract the file (debugger.vim and debugger.py) to your home vim plugin directory
    $ unzip debugger.zip
    $ cp plugin/debugger.* ~/.vim/plugin/
  4. fire up Vim!, make sure your build equiped with signs and python support or the plugin won't work!, I'm using ubuntu and a Vim-full package for this, no hush, no mush..
  5. if vim loaded without any error it means the plugin already loaded correctly, if not check your vim build, make sure the +signs and +python is there when you type in vim command
    :version
  6. open firefox, and install this addon
  7. open the PHP application you wish to debug using firefox, make sure the source code is accessible. for example if the source is in (assume you are using xampp)
    /opt/lampp/htdocs/testdebug/index.php
    then you can type in firefox url
    http://localhost/testdebug/index.php
  8. click the xdebug helper icon (the one you've just add) on firefox status bar (default on lower right corner), now the icon should flash green
  9. switch to your vim window and press F5 (the message window would say waiting for connection...)
  10. switch to your firefox window and reload by pressing F5,
  11. now switch to your vim window (again) and there you have it, a debugging interface of your application in vim.
let me explain a few of those steps a little bit (step 1 to 7 should be clear enough), step 8 means you have start a debugging session for the next http request, it is the same as if you append the url with XDEBUG_SESSION_START=1 so the url would be
http://localhost/testdebug/index.php?XDEBUG_SESSION_START=1
ofcourse clickin on xdebug helper icon is alot more convenience than appending the url each time we start a debugging session.

now for step 9 we prepare our beloved Vim to wait a connection from the debug engine at this point vim will kind of hang (waiting mode), the script default timeout is 5 second, if within that there are no debugging session requested, vim will return to normal mode. This means than we have to quckly run step 10, switch to firefox and reload, but again make sure the xdebug helper icon is already flash green (activated/clicked). After you have reload the page if you notice the loading status of the page is keep on spinning, the page wont finish loading until the debugging session stopped or vim is closed or request timeout

Now if we switch back to Vim, our debugging interface should up and running there are source, watch, help, stack and trace window. From this interface you can trace the code using F2 (step into), F3 (step over), F4 (step out). To end a session you can press F6. One of most common task is to put a breakpoint in our code, to do that open the file you wish to debug, position your cursor on a line you wish to stop and type :Bp in vim command window, now everytime the debugging session start, just press F5 to run until execution point reach the breakpoint. You can see other debugging command from the help window.

one thing about this vim plugin, there aren't that much debugging command available, either from the plugin or xdebug itself, from what I can tell it is more than enough to do my daily-debugging-basis. There are some lacks though, but I'll address this issue later on, considering this post is already bloated enough to make people sick from reading it :p

happy vimming (and debugging) !

keyword: ubuntu, vim, firefox, xdebug, php

Friday, December 12, 2008

Holy Crap, I got 7 ??

Just this afternoon, we have this small session of sharing in my Labs, anything related to IT and it is a weekly routine, we call this small session: RPL's BrainSpark.. (cool heh..)

And the spark of this week is about SEO (Search Engine Optimization) brought by Satrio Wahyu a.k.a yoyoK (a very friendly and funny guy actually :p)

Naaaa don't worry I'm not going detail on it, the idea is to get your webpage some significant ranking on most of search engine, and bla, bla, bla.... ( cant remember most of 'em)

And there's a part where he talks bout PageRank used by google to measure the importance of a web page for google's search , just download google's toolbar and you can see the PageRank of webpage you are browsing.

When I browse to this blog guess what ? I got 7 out of 10 !!, holly crap!, how on earth it gets to that ? and if I'm not mistaken, I could make some fortune out of it :P

yeah right, keep writing this kind of useless post, and won't be long till I get my blog kissing the floor again ~_~

nite..