In memory row selection

R

Rob

I want to implement a in-memory row selection function. Specifically,
given a table

Name Zip Phone
Tony 98034 127xxxxx
Mike 10023 271xxxx
Jame 10023 253xxxx

and a text box, I want the table automatically show relevant rows and
hide others. Say I type 10023,
only row 2&3 should be shown. Any existing package for doing this?
 
G

GArlington

I want to implement a in-memory row selection function. Specifically,
given a table

Name Zip Phone
Tony 98034 127xxxxx
Mike 10023 271xxxx
Jame 10023 253xxxx

and a text box, I want the table automatically show relevant rows and
hide others. Say I type 10023,
only row 2&3 should be shown. Any existing package for doing this?

XML & XSLT might help you...
If not and you insist on doing it in Javascript I doubt that you will
find any existing package - it is too trivial to package it.
 
R

RobG

At the the top of the scope that the function is within seems to be
widely used. It is also a common convention to declare counters where
they are used, although this may be misleading for those who expect
block scope:

for (var i=0; ...; ...)


What would you counsel in the following situation then ?

var i = 'abc';
// we want i to have value 'abc' here
function myScope() {
var i;
for (i=0;i<myArray.length();++i){}

for (var i=0, len=myArray.length; i<len; i++)


Noting that length is a property, not a method. If order is not
important, it may be more efficient to use:

var i = myArray.length;
while (i--)
// We want i to have the array's length here
}
myScope();
// we want i to have value 'abc' here

Whether you declare the global i before or after the function
declaration is irrelevant to its scope or value. If you want to
access it from inside the myScope function, use window. Note that
declarations are processed before any code is executed.

I think you're confusing Arial with Verdana.

I don't think he is - though he's likely wrong. Mac OS has included
Arial since OS X in 2001, I don't know about Linux.
 
T

Thomas 'PointedEars' Lahn

RobG said:
At the the top of the scope that the function is within seems to be
widely used.

I really don't care what is "widely used" as long there is no sufficient
reasoning for that; experience shows that most of code is written by
in[ae]pt people.

There is exactly no need to rip declaration and initialization apart, and so
to make maintenance of the code a lot harder, in ECMAScript implementations.
It is also a common convention to declare counters where
they are used,
True.

although this may be misleading for those who expect block scope:

Unfortunately, general incompetence cannot be helped.
for (var i=0, len=myArray.length; i<len; i++)

ACK, although I would do a little more pretty-printing.
Noting that length is a property, not a method. If order is not
important, it may be more efficient to use:

var i = myArray.length;
while (i--)

Why not

for (var i = myArray.length; i--;)

which I have recommended here frequently already?

I don't, but that doesn't matter.

http://www.w3.org/TR/CSS21/fonts.html#font-family-prop
I don't think he is - though he's likely wrong. Mac OS has included
Arial since OS X in 2001,

There was a Mac OS before Mac OS X, and although they are both based on the
same typeface, the Arial font on Mac OS X is different from the Arial font
that Microsoft Windows provides.
I don't know about Linux.

Linux, which is "merely" the kernel of the operating system, does not
provide that font (it only provides console bitmap fonts to date). As
for Debian GNU/Linux, XFree86 and the X Window System require licensed
TrueType/OpenType fonts like Arial to be imported from an external
location; they are not provided built-in or with the distribution.
I assume that due to licensing issues that is the same with all GNU/Linux
distributions.

And there are other platforms, operating systems, graphical frameworks,
and window managers than the ones mentioned here.


PointedEars
 
R

RobG

At the the top of the scope that the function is within seems to be
widely used.

I really don't care what is "widely used" as long there is no sufficient
reasoning for that; experience shows that most of code is written by
in[ae]pt people.

I don't think different styles of coding necessarily indicate
ineptitude - someone may follow your style guide slavishly yet still
write complete garbage. Similarly, I wouldn't claim someone was inept
simply because I didn't like where they declared their variables.

There is exactly no need to rip declaration and initialization apart, and so
to make maintenance of the code a lot harder, in ECMAScript implementations.

That may be your point of view, opinions differ.


[...]
Why not

for (var i = myArray.length; i--;)

which I have recommended here frequently already?

My preference it not to do that, but provided it is used consistently
I would have no problem with it. It may be marginally more difficult
to maintain for an inexperienced developer.

I don't, but that doesn't matter.

http://www.w3.org/TR/CSS21/fonts.html#font-family-prop


There was a Mac OS before Mac OS X,

No doubt that is why it is called OS X, not OS I. :)
and although they are both based on the
same typeface, the Arial font on Mac OS X is different from the Arial font
that Microsoft Windows provides.

Your original statement was that "'Arial' is a font only Microsoft
Windows provides by default" which is wrong. There are various
implementations of Arial:

Linux, which is "merely" the kernel of the operating system, does not
provide that font (it only provides console bitmap fonts to date).

I think any reasonable reader would have understood that statement as
a referrence to the various distributions based on Linux.

As
for Debian GNU/Linux, XFree86 and the X Window System require licensed
TrueType/OpenType fonts like Arial to be imported from an external
location; they are not provided built-in or with the distribution.
I assume that due to licensing issues that is the same with all GNU/Linux
distributions.

And there are other platforms, operating systems, graphical frameworks,
and window managers than the ones mentioned here.

And you are certain that *none* of them provide Arial as a default
font?
 
T

Thomas 'PointedEars' Lahn

RobG said:
That may be your point of view, opinions differ.

That there is no need to rip declaration and initialization apart is not
open for debate; it is a fact, supported by the language specification and
its implementations.

That this makes maintenance a lot harder is also not merely an opinion.
Common sense already tells you that you would have to watch for that all
identifiers of variables that you use are declared a number of lines before
they are actually used; depending on the complexity of the code, that may be
a dozen, maybe a hundred lines. This supports declarations for unused
variables to stay in the code regardless. It is also easy then to forget
that the declaration is recommended, and end up with a property assignment
instead.
`Arial' is a font only Microsoft Windows provides by default. The
`font-family' property value can be a comma-separated list, where
the last item should specify a generic font family (here: sans-serif).
I think you're confusing Arial with Verdana.
I don't, but that doesn't matter.

http://www.w3.org/TR/CSS21/fonts.html#font-family-prop
[...]
I don't know about Linux.
Linux, which is "merely" the kernel of the operating system, does not
provide that font (it only provides console bitmap fonts to date).

I think any reasonable reader would have understood that statement as
a referrence to the various distributions based on Linux.

"Distributions based on Linux"?

Much you have to learn, young apprentice.[tm]
And you are certain that *none* of them provide Arial as a default
font?

I'm not (simply because I don't know all), but as I said, that really does
not matter here. What matters is that there is always the possibility of
this font not to be available, that has to be taken care of, which is what
I meant to say.


PointedEars
 
R

Richard Cornford

Thomas said:
That there is no need to rip declaration and initialization
apart is not open for debate;

That is a very loaded expression. It is debatable whether declaration
and initialisation were ever together in the first place, and so whether
separating them would be 'ripping' them apart. Of course if they were
never together to start with any absence of a need to separate them is
not open for debate, it is just irrelevent.
it is a fact,

The absence of need may be a fact. As may be that absence not being open
for debate. But if declaration and initialisation were never really
together to start with then those facts are just the trivial
consequences of the act of separation being superfluous.
supported by the language specification and
its implementations.

The specification states that variable declarations are acted upon
during variable initialisation, prior to any code in the execution
context being executed, and that assignments (and so what is being
spoken of as initialisation here) are handled when the assignment
expression is evaluated during the execution of the code for the
execution context. This makes the two actions chronologically distinct,
with all of the first (declarations) being handled before any of the
latter (initialisations).
That this makes maintenance a lot harder is also not merely
an opinion.

No, that is a matter of opinion. Doing nothing other than separating
declarations form initialisations may make maintenance harder but in
realty the alternative is to formally declare all local variables at the
start of the containing function's body. This has the advantage that if
you want to verify that a variable used has been declared, or to insure
that it only has one declaration (because javascript could act on each
and every declaration for the same variable, which would make
re-declaring wasteful), then you only have to go to one location to
look. Reducing the time to find a declaration is a plus for maintenance.
Common sense already tells you that you would have to watch for
that all identifiers of variables that you use are declared a
number of lines before they are actually used; depending on the
complexity of the code, that may be a dozen, maybe a hundred lines.

Precisely, so if all declarations are in the same place it does not take
much time or effort to locate that one place to wee whatever you need to
know.
This supports declarations for unused
variables to stay in the code regardless.

Yes it does, but it trades that off against accidentally re-declaring
variables and the time needed to verify that a variable has been
declared locally and so is not (accidentally or intentionally) a global
variable, or one form a containing scope.

In reality all three problems can be solved with something like JSlint,
which will inform you of declared and unused variables, re-declared
variables and references to undeclared global variables.
It is also easy then to forget that the declaration
is recommended, and end up with a property assignment
instead.
<snip>

A question that may be well illustrated with this method from dojo
(which has a line highlighted by me:-

dojo.toJson = function( _indentStr){
_indentStr = _indentStr || "";
var nextIndent = (prettyPrint ? _indentStr +
dojo.toJsonIndentStr : "");
var newLine = (prettyPrint ? "\n" : "");
var objtype = typeof(it);
if(objtype == "undefined"){
return "undefined";
}else if((objtype == "number")||(objtype == "boolean")){
return it + "";
}else if(it === null){
return "null";
}
if(objtype == "string"){ return dojo._escapeString(it); }
var recurse = arguments.callee;
var newObj;
if(typeof it.__json__ == "function"){
newObj = it.__json__();
if(it !== newObj){
return recurse(newObj, prettyPrint, nextIndent);
}
}
if(typeof it.json == "function"){
newObj = it.json();
if(it !== newObj){
return recurse(newObj, prettyPrint, nextIndent);
}
}
if(dojo.isArray(it)){
var res = [];
for(var i = 0; i < it.length; i++){
var val = recurse(it, prettyPrint, nextIndent);
if(typeof(val) != "string"){
val = "undefined";
}
res.push(newLine + nextIndent + val);
}
return "[" + res.join(", ") + newLine + _indentStr + "]";
}
if(objtype == "function"){
return null;
}
var output = [];
for(var key in it){
var keyStr;
if(typeof(key) == "number"){
keyStr = '"' + key + '"';
}else if(typeof(key) == "string"){
keyStr = dojo._escapeString(key);
}else{
continue;
}
val = recurse(it[key], prettyPrint, nextIndent);
////////^^^
if(typeof(val) != "string"){
continue;
}
output.push(newLine + nextIndent + keyStr + ": " + val);
}
return "{" + output.join(", ") + newLine + _indentStr + "}";
}

The highlighted line is an assignment to an Identifier named - val -,
and I was wondering whether it was global or local so I went looking for
its declaration. If I had written the code its declaration would have
been at the top of the function body and I would have found it in a
couple of seconds (if that), but instead its declaration is one level of
indentation deeper than this assignment and it is in a branch of the
code that execution would not enter in the context of my interest in the
function's contents (the dojo.isArray function would have returned
false). The time wasted in shaving to scan every line of such a long
function for a variable declaration is not a contribution to easy
maintenance.

It is a commonly recommended 'best practice' that all variable
declarations and inner function declarations should be made at the start
of a function body. And it is a best practice that I agree with, because
it means you always know where to look for the declarations, the code
structure mirrors the handling of the declarations (that they are acted
upon before any other code is executed), and also the result does not
act to give the false impression, or re-enforce the false impression,
that javascript is block-scoped.

Richard.
 
T

Thomas 'PointedEars' Lahn

$Richard Cornford said:
In reality all three problems can be solved with something like JSlint,
which will inform you of declared and unused variables, re-declared
variables and references to undeclared global variables.

But using something like JSlint would not be necessary if the code was
written with future maintenance costs in mind in the first place.
It is also easy then to forget that the declaration
is recommended, and end up with a property assignment
instead.
<snip>

A question that may be well illustrated with this method from dojo
(which has a line highlighted by me:-
[...]
if(dojo.isArray(it)){
var res = [];
for(var i = 0; i < it.length; i++){
var val = recurse(it, prettyPrint, nextIndent);
if(typeof(val) != "string"){
val = "undefined";
}
res.push(newLine + nextIndent + val);
}
return "[" + res.join(", ") + newLine + _indentStr + "]";
}
if(objtype == "function"){
return null;
}
var output = [];
for(var key in it){
var keyStr;
if(typeof(key) == "number"){
keyStr = '"' + key + '"';
}else if(typeof(key) == "string"){
keyStr = dojo._escapeString(key);
}else{
continue;
}
val = recurse(it[key], prettyPrint, nextIndent);
////////^^^
if(typeof(val) != "string"){
continue;
}
output.push(newLine + nextIndent + keyStr + ": " + val);
}
return "{" + output.join(", ") + newLine + _indentStr + "}";
}

The highlighted line is an assignment to an Identifier named - val -,
and I was wondering whether it was global or local so I went looking for
its declaration. If I had written the code its declaration would have
been at the top of the function body and I would have found it in a
couple of seconds (if that), but instead its declaration is one level of
indentation deeper than this assignment and it is in a branch of the
code that execution would not enter in the context of my interest in the
function's contents (the dojo.isArray function would have returned
false).


But that code is not really a good example of what I was aiming at. Because
there *is* a declaration but that declaration is not only in another block
statement but it is wrapped in conditional statement. I regard *that* as
bad style (even when knowing that variable instantiation comes before
execution), and I would concede in *this* case that it is better to do the
declaration on top of the execution context's body. Re-use of an identifier
in another block statement is the exception that I am willing to make here.
[...]
It is a commonly recommended 'best practice' that all variable
declarations and inner function declarations should be made at the start
of a function body. And it is a best practice that I agree with, because
it means you always know where to look for the declarations, the code
structure mirrors the handling of the declarations (that they are acted
upon before any other code is executed), and also the result does not
act to give the false impression, or re-enforce the false impression,
that javascript is block-scoped.

Then we would have to agree to disagree here, because firstly I don't agree
with that so-called "best practice" in all cases and secondly I really don't
care about the misconceptions looking at my code might create as there are
far too many possibilities for that.


PointedEars
 
R

Richard Cornford

Thomas said:
But using something like JSlint would not be necessary
if the code was written with future maintenance costs
in mind in the first place.

There are 3 variable declaration problems; failing to declare a variable
that should be local, providing multiple declarations for the same
variable and declaring a variable but never using it. The differing
formal 'best practices' address at most two of those, leaving at least
one needing handling in some other way (such as using JSLint).
It is also easy then to forget that the declaration
is recommended, and end up with a property assignment
instead.
<snip>

A question that may be well illustrated with this method
from dojo (which has a line highlighted by me:-
[...]
if(dojo.isArray(it)){
var res = [];
for(var i = 0; i < it.length; i++){
var val = recurse(it, prettyPrint, nextIndent);
if(typeof(val) != "string"){
val = "undefined";
}
res.push(newLine + nextIndent + val);
}
return "[" + res.join(", ")+ newLine+_indentStr + "]";
}
if(objtype == "function"){
return null;
}
var output = [];
for(var key in it){
var keyStr;
if(typeof(key) == "number"){
keyStr = '"' + key + '"';
}else if(typeof(key) == "string"){
keyStr = dojo._escapeString(key);
}else{
continue;
}
val = recurse(it[key], prettyPrint, nextIndent);
////////^^^
if(typeof(val) != "string"){
continue;
}
output.push(newLine + nextIndent+keyStr+ ": " + val);
}
return "{" + output.join(", ")+newLine+ _indentStr + "}";
}

The highlighted line is an assignment to an Identifier named
- val -, and I was wondering whether it was global or local
so I went looking for its declaration. If I had written the
code its declaration would have been at the top of the
function body and I would have found it in a couple of seconds
(if that), but instead its declaration is one level of
indentation deeper than this assignment and it is in a branch
of the code that execution would not enter in the context of
my interest in the function's contents (the dojo.isArray
function would have returned false).


But that code is not really a good example of what I was
aiming at.


Maybe not, but it illustrates the application of a 'best practice'
principle to code design. I chose it precisely because dojo has a formal
code style document and it asserts that the declaration of a variable
should be at the point of the first assignment to that variable. Above
we see the application of that principle (though possibly its blind
application). We also see that the application of that principle does
not necessarily result in code that is easy to understand, and so is not
necessarily contributing to easy maintenance of that code.
Because there *is* a declaration but that declaration is not
only in another block statement but it is wrapped in
conditional statement.

And more deeply indented than the assignment that attracted my
attention.
I regard *that* as bad style (even when knowing that variable
instantiation comes before execution),

So would I, but it is the result of the application of what the dojo
authors consider formal good style (and promote to others as such).
and I would concede in *this* case that it is better to
do the declaration on top of the execution context's body.
Re-use of an identifier in another block statement is the
exception that I am willing to make here.

So you are saying that 'declare variables at the point of their first
initialisation' is inadequate as a formal style rule and needs at least
some context related qualification before it can be proposed as a 'best
practice'? I would agree, but I think that the level of qualification
might easily reduce the result to little more than vague guidance for a
programmer and so not achieve the sort of general code consistency that
does contribute to easier maintenance.
[...]
It is a commonly recommended 'best practice' that all
variable declarations and inner function declarations should
be made at the start of a function body. And it is a best
practice that I agree with, because it means you always know
where to look for the declarations, the code structure mirrors
the handling of the declarations (that they are acted upon
before any other code is executed), and also the result does
not act to give the false impression, or re-enforce the false
impression, that javascript is block-scoped.

Then we would have to agree to disagree here, because firstly I
don't agree with that so-called "best practice" in all cases
and secondly I really don't care about the misconceptions
looking at my code might create as there are far too many
possibilities for that.

That is fine by me. I have never believed that any 'best practice' was
worth more than the arguments used to justify it. If those arguments are
not good enough to convince then the odds are that the 'best practices'
in question are little more than opinions, and possibly uninformed
opinions.

However, my own preference here is partly driven by the realisation that
many of individuals who propose declaring variables at their first
assignment are doing so precisely because they have the erroneous
impression that javascript is block-scoped (mostly because they are
assuming that it will be like Java in this respect). We have seen such
advice on this group often enough, and when pursued that misconception
has usually be revealed in its justification.

So where advice of this sort persists I suspect that it may be
persisting without technical justification. For example, in text
supposedly intended to justify not declaring all local variables at the
top of a function body in a 'best practice' page at:-

<URL: http://www.dhtmlkitchen.com/learn/js/perf/variables.jsp >

- we find the statement:-

| The general rule of thumb is to declare and assign a variable
| with one statement in the narrowest possible scope.

- which might reflect the programming axiom that no variable should ever
be given more scope than it absolutely needs. But the fact that it
appears in the context of an argument against declaring all variables at
the top of a function body implies that its author thinks that a
variable declared at the start of a function body might have less narrow
scope than one declared later in the body. Without the author having
that misconception the statement should have been seen as irrelevant in
its context and so have never been made.

The impression of misconception there is re-enforced by the statement:-

| Declaring and assigning a variable in the same statement also
| offers a slight performance boost because the variable is
| referenced only once.

- which is obviously not true given that variable instantiation happens
before any code is executed.

The arguments in favour of declaring all variables at the top of a
function body have technical substance (even if they are not seen as
sufficient to be adopted as a general coding style by everyone), while
the arguments for declaring at the point of the first assignment seem to
be being influenced by much misconception and bogosity.

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,216
Latest member
topweb3twitterchannels

Latest Threads

Top