Thursday, June 27, 2013

JavaScript debugging in Chrome

This seems to me more natural. The only trick was finding the source lines: (three bars on upper right of Chrome) | Tools | Developer tools | Sources | "|>" | file name.

This makes eminently more sense with EclipsePDT than Aptana. EclipsePDT can transfer all the files to the web server and single-step PHP. Then let Chrome single-step the JavaScript. Actually I haven't tested the PDTEclipse-Chrome solution all the way through but each piece works separately. I'll need to integration test them

JavaScript on Aptana

I looked for a way to first write JavaScript in an IDE. I found Aptana which looks like Eclipse with the Aptana plugin pre-installed. It's a lovely concept, providing JavaScript (and PHP) syntax highlighting and intelli-sense (or what it is called when you hit "."). The problem with Aptana is I see how to debug JavaScript in it. I mean, why would I want to do that anyhow where there is Chrome debugging of JavaScript?

Wednesday, June 26, 2013

Curious JavaScript form validation

Here are two curios things about JS: 1) need for "return" for onsubmit attribute and 2) anything but "false" is accepted as "true".


<!DOCTYPE html>
<html>
<head>
<script>
function validateForm()
{
var x=document.forms["myForm"]["fname"].value;
if (x==null || x=="")
  {
  alert("First name must be filled out");
  return false;
  }
}
</script>
</head>

<body>
<form name="myForm" action="demo_form.asp" onsubmit="return validateForm()" method="post">
First name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
</body>

</html>

Tuesday, June 25, 2013

Oh...JavaScript

I'm breezing through JavaScript and came upon this notation:

person=new Object();
person.firstname="John";
person.lastname="Doe";
person.age=50;
person.eyecolor="blue";

Now this strikes me as one of those error-prone sorts of things that hazardous for PHP: creating object fields on the fly.

Monday, June 24, 2013

PHP vs. Javascript for input validation

I've just moved into Javascript, the first day, and found probably the only piece of Javascript I really need for now. Namely, input validation.

But PHP can also do input validation as the code below suggests.

<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PHP Form Validation</title>
</head>
<body>



<form action="valid_handler.php" method="POST">
<fieldset>
<legend>Enter a quantity and email address</legend>
<p>Quantity : <input type="text" name="quantity"></p>
<p>Email Address : <input type="text" name="email"></p>
</fieldset>
<p><input type="submit" ></p>
</form>
</body>
</html>

The same (sort of) thing in JavaScript is:
<!DOCTYPE html>
<html>
<body>
<h1>My First JavaScript</h1>
<p>Please input a number.</p>
<input id="demo" type="text">
<script>
function myFunction()
{
var x=document.getElementById("demo").value;
if(x==""||isNaN(x))
 {
 alert("Not Numeric");
 }
}
</script>
<button type="button" onclick="myFunction()">Click Me!</button>
</body>
</html>

Without the benefit of other, more informed PHP-JavaScription opinions, I opinion is JavaScript is better to attempt this than PHP. Why? Because as a general rule, we prefer to do input validation as close to the source as possible. Rather than waiting for the HTML to get to the server, test it in the browser, save Internet latency, and conserve server cycles for when they are really needed.

Off by one errors

I received a phone call this morning from my step-mother. It wasn't even 8 AM and I assumed it was bad news, in this case, about my father. No, the flight reservations I had made for them in February had a bug. The conference they were attending ended on 18 Jul which was the date of departure. But they needed to depart on 19 Jul to make that last day of the conference.

I assumed I had gotten it wrong; rather, this departure date was the info she had given me. I know this because she told me today that she had also made the hotel reservations herself for the same (buggy) departure date. I didn't think at the time to ask about this last day. Then, it occurred to me, this is a boundary condition and an off-by-one-error.

Computer science is probably the formal place where I learned about off-by-one-errors. They come in the form of algorithm flaws and most notably crashes or "array bound exceptions."

I suspect these OBO errors are everywhere in life and the way we deal with them is the same way. If we catch them by testing beforehand, we're not lucky, just diligent. Indeed, there's a methodology called "boundary value analysis" for identifying such bugs. Even if we're diligent, we could still be unlucky. That is, we'll get an exception and deal with it in LA and worse, the day of departure.

Monday, June 17, 2013

LOC and complexity

Conventional wisdom, and dare I say "common sense," says that LOC and complexity are highly correlated. See here. The early data I'm getting for Scala and functional programming suggests there may, MAY, be more to this story. Maybe.

Tuesday, June 11, 2013

Primary, unique, and key constraints

The following query creates a primary key for the id column:

CREATE TABLE IF NOT EXISTS products
(
  id INT UNIQUE AUTO_INCREMENT ,
  code INT NOT NULL ,
  name VARCHAR(25) NOT NULL ,
  quantity INT NOT NULL ,
  price DECIMAL(6,2) NOT NULL
) ;

So the question is does every unique column a primary key? No.

The reason is the primary key is a type of unique column. A more complete explanation is here.

Monday, June 10, 2013

PHP and JSP

As I have moved further into PHP programming, I'm getting a feeling as to why PHP is so popular.

I, for one, cannot see much difference between PHP and JSP except for one thing: it's PHP and not Java. Duh.

PHP does not have THE "accident" of Java, namely, needing to specify types. PHP uses dynamic typing, that is, the type depends on the context. Part of the problem here for Java is that it is not a scripting language but may need to be one for JSP.

Whereas Java was ready for the Internet when it burst on the scene some 20 years ago, PHP is ready for the web server. Super globals like $_FILE and $_POST, associative arrays, etc. -- all these things seem to make PHP more productive than Java.

JSP and servlets were after-thoughts for Java as an alternatives to Perl and CGI--or at least that's my memory of things. With garbage collection and simple inheritance, Java was definitely an advance over C++ for the tasks for which it was designed. In the same spirit, PHP seems to me an advance compared to Java for server side programming.

And I haven't even gotten to the database tie-in which I'm just about to take up in my learning.

Sunday, June 9, 2013

Eclipse PDT

As I have written earlier, modern software development for me is to automate the "accidents" of computing to reduce errors. Toward this end, I had installed NetBeans which worked beautifully with PHP. However, I failed to get NetBenas to work with IDE debugging of PHP scripts. It was hugely disappointing because NB came with a recommendation and I liked the refactoring of PHP variables. I wasn't crazy about how NB ran the script in the browser window instead in the IDE window but...nobody is perfect.

Thus, I abandoned NetBeans on the Mac and instead installed Eclipse PDT and EasyPHP on Windows 8. I still struggled a bit but managed to get single-step debugging working.

The trick is to put the project on the local server. Unfortunately there are no instructions as to how to do this. Yet after floundering around, I found the option under Preferences | PHP Servers which positions all project scripts under the web server root. Once that is set up, Eclipse will breakpoint every time at the first statement of the PHP script (this is configurable, I believe) and furthermore, the web page displays in the Eclipse IDE.

Variable inspection is available as well as variable inspection.

Unfortunately, Eclipse PDT does not do refactoring except for the script as a whole.

Saturday, June 8, 2013

An elemental Scala parser

I've been writing a parser for Scala to gather complexity information. It doesn't need to "compile" Scala, just extract method information. Thus, I'm duly impressed that anyone could possibly parse Scala, given the difficulties and doubts I have that I've gotten it right. Indeed, some people seem to think that one cannot write a parser for Scala without, in effect, running it to compute the types and sort out implicits. However, as I was cleaning house, I came across an CACM article, Inexact Design--Beyond Fault Tolerance. It deals with hardware but nevertheless struck me as a cool idea, that is, of "inexact," "probabilistic," or "approximate" computing. I think that's kind of what my Scala parser is doing.

Wednesday, June 5, 2013

PHP class properties

The possibility of dynamically creating new properties in a PHP class seems like a slippery slope. Nixon (2012) says to avoid it because of the potential to create bugs. I agree. Then, why is the possibility there. A programming language's job, among other things on the path of automation, is to automate the detection and elimination of errors.

Tuesday, June 4, 2013

PHP super class constructors

In C++, Java, and Scala, instantiating an object automatically invokes the parent class's constructor. Not in PHP 5. Instead you have to invoke it yourself. I am surprised. This potentially a huge source of awful errors.

And for a silly reason because PHP creators couldn't put in the automatic invocation? Is there some other explanation?

Then again, Obj-C doesn't automatically invoke it's parent constructor. I've been waist deep in Obj-C for the past few years and while I won't say that never caused a problem, I've had far bigger issues with Obj-C (e.g., not crashing when accessing uninitialized variables).

Sunday, June 2, 2013

PHP: First impressions

I had heard from others not good things about PHP, how it is used, at least. In fact, it seems that some websites want to hide the PHP behind them.

Working with a beautiful, Edward-Scissorhands-like language like Scala and recently exploring Haskell which I don't get, PHP feels to me like a throwback to another era, specifically C, which was one of the first languages I learned. After completing chapter 3 of Nixon (2012), I've concluded so far that PHP is not as elegant as Awk but (thankfully) appears to lack the obscurity of Perl.  Forget Basic, Fortran, Scheme, and Prolog. It is perhaps closest to Korn shell scripting. The point it seems to me is not what PHP is but what it can do. Scala is my first choice language. Scala is wonderful for what it was designed to do. But I must remember one size does not fit all. If PHP is going to make me more productive, reduce errors, and help deliver innovative, original solutions, what does functional programming have to do with it?

Saturday, June 1, 2013

NetBeans with Zend


First thing is to download to right NB, the "All" version which supports PHP.

Did that.

Next to create a PHP project in NB:

File | New Project… | PHP | PHP Application  | (use default Name and Location) | (Run Configuration) Project URL = http://localhost:10088/testing/PhpProject2, check “Copy files from…” = /usr/local/zend/apache2/htdocs/testing/PhpProject2 | Finish

That worked. Meaning, the PHP runs fine.

Now for debugging. Unfortunately I get "Waiting For Connection (netbeans-xdebug)" on the NB status line. Nothing happens.

The URL is localhost:10088/testing/PhpProject2/index.php?XDEBUG_SESSION_START=netbeans-xdebug

which looks correct. My sense is that there is something trivially wrong in the connection between NB and Xdebug.

This link here is not very hopeful. I'll have to decide what to do.

NetBeans vs. Eclipse PHP development

Some developers like to work directly with the source editor, in some cases without even syntax highlighting. Call it old school. Purists. Definitely not me. For electrical engineers it might be designing circuits at the transistor level. Or programmers working only in assembly language. These things might some day be useful but they are not very productive.

An IDE allows the programmer to focus more on the problem at hand, automating more of the "accidents,  as Brooks (1986) observed.

So when I learned there were Eclipse plugins, PHP Development Tools (PDT) and PHPEclipse, I was naturally relieved.

However, I stumbled upon this page comparing Eclipse and NetBeans and this fellow definitely gave NB the edge.

This in turn lead to a more systematic discover of IDEs for PHP here. I was surprised. NB is free and supports not only a PHP debugger but refactoring, too. NB (All option) comes ready "out of the box," where the Eclipse plugins seem to need more configuration.

I've decided to try NB for these reasons.