Error Reporting

Posted by Montu on March 25, 2009

PHP has come a long way. I used to program with Global vars then $HTTP_POST_VARS and then $_POST and even did a foreach to convert all Posted/Get-ed vars to their respective variable names. But I never faced any issue till the time I started using error_reporting(E_ALL).
My life changed. Initially I started pulling my hairs and then I realised that I will be bald very soon and then started concentrating on writing better code. Avoiding on the fly variables. Yesterday at work, I again realised the value of  error_reporting(E_ALL).I created a small piece of code and tested it in a temp php file. It looked superb. I ported the code to production environment and it started breaking. I thought for a while. May be I have introduced some parse errors. I ran a php -l test and didnt see any syntax error. I read the whole code line by line. Even did a diff. Still the temporary php code was running fine and the code in production environment was not working fine. :-/

I decided to look at the code bit later. After a break of 15 minutes I decided to write a unit test and capture everything. And then I found a notice message telling me I was accessing an index that didn’t exist. I looked at my data set. It was a long range of number, 6184 to be precise and an index value 280 was not set. I looked at my temp file and found my code was not going thru error reporting and production environment was set up to halt at all notices and warnings. Damn it!

Drop down menus with jQuery

Posted by Montu on March 18, 2009

After attempting sliding menus with jQuery I developed drop down menus using lists.

Here is the code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html>
  <head>
  <title>Drop Down Menus With jQuery</title>
  <script type="text/javascript" src="js/jquery.js“></script>
  <script type=”text/javascript”>
  function mainmenu(){
  $(”#nav ul” ).css({display: “none”}); // Opera Fix
  $(”#nav>li” ).hover(
function(){
$(this).find(’ul:first’).css({visibility: “visible”,display: “none”}).show(400);
},
function(){
$(this).find(’ul:first’).css({visibility: “hidden”});
});
}
$(document).ready(function(){
mainmenu();
});
</script>
<style type=”text/css”>
#nav, #nav ul{
margin:0;
padding:0;
list-style-type:none;
list-style-position:outside;
position:relative;
}
#nav>li{
float:left;
position:relative;
}
#nav>li>a{
font-size:12px;
font-family: arial;
color:#000000;
padding:5px;
text-decoration:none;
border: 1px solid #ddd;
background:#eee;
}
#nav ul {
position:absolute;
dispay:none;
}
#nav a{
display:block;
}
#nav>li>ul>li>a{
font-size:12px;
font-family: arial;
color:#000000;
padding:3px;
text-decoration:none;
border: 1px solid #aaa;
background:#bbb;
}
</style>
</head>
<body>
<ul id=”nav”>
<li><a href=”">HTML</a></li>
<li><a href=”#” >CSS</a>
<ul>
<li><a href=”#”>CSS 1</a></li>
<li><a href=”#”>CSS 2</a></li>
<li><a href=”#”>CSS 3</a></li>
</ul>
</li>
<li><a href=”#” >Javascript</a>
<ul>
<li><a href=”#”>Mootools</a></li>
<li><a href=”#”>Prototype</a></li>
<li><a href=”#”>jQuery</a></li>
<li><a href=”#”>Yahoo! UI</a></li>
<li><a href=”#”>Dojo</a></li>
<li><a href=”#”>Mochi Kit</a></li>
</ul>
</li>
</ul>
</body>
</html>

Sliding menus with Jquery

Posted by Montu on February 26, 2009

I have been avoiding Jquery for quite some time. I was just turning myself blind to Jquery. Jaquery? What Jquery? :-/

Damn it! I have been spelling jQuery as Jquery. :D

I wanted some thing similar to accordion effect. There are tons of JavaScript frameworks offering this but I didnt want myself to be again copying huge/minified file/s and then reading hell lot of documentations figuring out how and why and ….

Phew #:-S

Now following is a snippet of code from my dear Kaddu. Obviously you will need jQuery.

<html>
<head><title>Tree</title>
<script type="text/javascript" language="javascript" src="js/jquery.js“></script>
<script type=”text/javascript” language=”javascript”>
$(document).ready(function(){
$(’.level-1′).hide();
$(’.top’).css(’cursor’,'pointer’).bind(’click’,viewList);
});
this.viewList = function (){
var id	 = $(this).attr(’id’);
var spId = id.split(’_');
$(’#ul_’+spId[1]).slideToggle();
}
</script>
</head>
<body>
<ul>
<li class=”top” id=”li_1″> 1
<ul class=”level-1″ id=”ul_1″> <li>1.1</li>
<ol>
<li>1.1.1</li>
<li>1.1.2</li>
<li>1.1.3</li>
</ol>
</ul>
</li>
<li class=”top” id=”li_2″> 2
<ul class=”level-1″ id=”ul_2″> <li>2.1</li>
<ol>
<li>2.1.1</li>
<li>2.1.2</li>
<li>2.1.3</li>
</ol>
</ul>
</li>
<li class=”top” id=”li_3″> 3
<ul class=”level-1″ id=”ul_3″> <li>3.1</li>
<ol>
<li>3.1.1</li>
<li>3.1.2</li>
<li>3.1.3</li>
</ol>
</ul>
</li>
</ul>
</body>
</html>

Now, you can change lis to divs to spans to ps or what not. You just need IDs and thats it. You have a sliding menu. :-)

Are design patterns silver bullets?

Posted by Montu on December 17, 2008

I hate text books thicker than half an inch. These books are, first hard to read, hard to understand. I am a man who will do it and understand it rather than read it and understand it. But today I decided to read and do and tried to understand. The object in question was a Frame Work which allowed me to build a SQLite based guestbook using MVC pattern, oops!, MVC Design Pattern. #-o

The question is do I really need to code more than 3 pages to develope a guestbook?

My answer is, no. Ofcourse you dont need a abstract knife factory which will give you a factory method that will let you create a kitchen knife to cut vegetables. I simply want to cut a piece of vegetable, say want to chop an onion to cook my ultimate Indian gravy dish. I need a knife. Now there are hell lot of knives in kitchen. Butcher knife, Kitchen knife, Chef’s knife and this and that and oh my god so many knives. So if I am planning to cook chicken dish I will need a butcher knife but also a kitchen knife to chop onions, green chillies and other stuff. So we have a abstract knife factory. Which in turn owns a factory method which in turns gives me an appropriate knife at the last momentdepending on what I am going to chopor cut.

Geez!!!

Come to procedural approach. I will develop a library of knives or say a knives holder. I will stack all my knives here and will pick up the knife which I need or say call the function whichever I need.

Aren’t design patterns leading to bloats or sometimes they turn into an overkill or…?

Are design patterns silver bullets?

Design Patterns - Singleton is evil?

Posted by Montu on December 14, 2008

To hell with everything. Why the hell use singleton or even why the hell it is described as a pattern by GOF?

I don’t know. I have been reading, studying and even trying to code and use the same design patterns in real life. The very first and easy to understand pattern is Singleton. This pattern was defined/recognised/identified as a pattern which assures creation of one and only object throughout the life cycle of a program. Common or say classic example given is error logging mechanism. And that’s the only real world example. Rest of the examples such as file or database handles are not suitable examples. They can be said as good example but not perfect.

Why singleton is evil?

I don’t want to reinvent wheel. There are n number of reasons. The reason I found is, tight coupling. Coupling in OOPS or even Software Development is discouraged. Loosely coupled systems allow more robust testing. You can replace the modules with dummy modules, equivalent modules and test the system. In this scenario a file logging mechanism is a perfect example of singleton pattern. The mechanism can be file logger, database logger or any other logger and will not affect our system if we replace it. Coz its not tightly coupled to system. Or as I read some where it simply “gets” the information and doesn’t “set” anything in our system. On the other side a database layer can’t be singleton pattern. What if I replace my MySQL layer with PGSQL? The system may not perform as expected or worst may break.

I suggest using singleton pattern shamelessly if your system is going to be a very tightly coupled and is custom and specific to a requirement. My argument is performance counts more than portability. Ofcourse Bullet 350 is no match to Splendor and Splendor is no match to Bullet 350. So, singletons are evil but so is fire, if you use it to burn your neighbour’s house. Use fire to light up. Use singleton wisely. :-)


Fine Print: In very short I own my views & if you don't like something on my blog, I am sorry, I can't help you. In fact I am not even sorry. Ch33r5.