PDA

View Full Version : Silly JSP code : any hints ?


DarkZell666
14th June 2006, 08:28
Hi everyone,

I have a question : In what particular case would adding 0 to a number be of any use ?

I'm taking over a project left by someone else, and i've come across this :


int whateverVariable1 = 0;
int whateverVariable2 = 0;
int whateverVariable3 = 0;
...

... *scrolls 500 lines* ...

whateverVariable1 = whateverVariable1 + 0;
whateverVariable2 = whateverVariable2 + 0;
whateverVariable3 = whateverVariable3 + 0
...


What on earth is this ? o_O
Hints welcome ...

Drifterz
14th June 2006, 08:42
Really don't have a clue but...

First of all if you mentioned what language you were using might help. Throw in your compiler exact error message and generally more information.

Second of all you might get more attention in the programming topic, being thats where more programmers are likly to be, just an idea i wouldn't know.

Third. I have VERY basic programming skills and is it possible that you cannot reassign a varible using the same variable. For example you could have the variable on both sides of the equal sign? Like I said just a wild guess.

It might try somthing like:
whateverVariable1b = whateverVariable1a + 0;
whateverVariable2b = whateverVariable2a + 0;
whateverVariable3b = whateverVariable3a + 0
...

Oh also 0+0=0 could that be a problem. Probally not dosn't make much sense but I'm all out.

foxyshadis
14th June 2006, 11:15
DarkZell: Someone else's code is always full of bizarre and useless stuff; it may have been for debugging or had real values once (in which case it should have been commented out), it may have come from someone who brought Javascript experience and cruft to the table, or he/she might've just been a superstitious head case.

Drifterz, the code works, he's just wondering about the pointlessness of it.

unmei
14th June 2006, 11:21
@Drifterz JSP = JavaServerPages, i assume (its in the thread topic, not the post text)

@DarkZell666
I wouldn't know JSP, but my guess is the original programmer used some other constant than zero before and replaced it, or intended to change it to another constant later on. I'd probably just delete/comment out those lines and check if it breaks :)
I really can't imagine than making sense *as it is now*..

[edit] OK, foxy had the same thoughts quicker :)

Doom9
14th June 2006, 13:59
in Java (and a bunch of other programming languages) an int's default value is 0 so there's no need to explicitly set it. And needless to say that adding 0 to an int does nothing.. it's basic math.
I have found a couple of myvar=myvar lines in my projects and have wondered what the heck I was thinking.. it may have been a scope issue and the global variable had since been renamed resulting in the assignment having no effect, but I can't claim senseless lines of code have never happened to me.

I don't know exactly when, but there are a couple instances where the java compiler (or was it the C# compiler?) would complain about using a potentially unassigned variable.. then again, since an unassigned variable of a class type is by default null, that complaint isn't really telling me anything.. whether you declare the variable and set it to null, or wheter you just declare it, in the end it's all the same and if you try the access it without having assigned something to it, then either way you're going to run into a nullpointerexception.

mpucoder
14th June 2006, 15:58
With interpretive languages, like JSP and PHP, variables are not allocated when declared, but when they are assigned a value. Performing some otherwise useless operation on a variable gets it allocated, and doing this in advance potentially speeds things up, and avoids out-of-memory problems later in the code where an exception might cause data corruption (aborting mid way through some process).

DarkZell666
14th June 2006, 17:28
Rofl, just as I thought, useless ... xD

I was starting to wonder if I was going mad ^^


I wouldn't know JSP, but my guess is the original programmer used some other constant than zero before and replaced it, or intended to change it to another constant later on.
a friend of mine suggested the same ;)

Thx 4 ur help all of you, I just needed confirmation I wasn't going mad ;)

Lines deleted and vanished into hell :D

bb
14th June 2006, 23:00
First of all JSP is not an interpreter language. It is basically a mixture of Java code and HTML markup, and JSP pages are compiled to Java servlets.

In a JSP page there might be some code like
<% int value = someBean.computeSomeValue(); %>
<p>My value is: <%= value %></p>
Let's assume the bean method computes a value of 0, then value is 0, too, and in the server response the paragraph is evaluated to
<p>My value is: 0</p>

This kind of evaluations can appear anywhere on a JSP page; it may be used inside JavaScript blocks, too. Thus some code like
<script language="JavaScript">whateverVariable1b = whateverVariable1a + <%= value %>;</script>
may evaluate to
<script language="JavaScript">whateverVariable1b = whateverVariable1a + 0;</script>
That's what you see in the response, i.e. in the HTML markup you receive from the server.

If the zero assignment is not in the JavaScript, but in the Java code block of the JSP, then it's indeed useless (and silly, too).

bb

Shinigami-Sama
16th June 2006, 21:15
umm, just looking at my unix book, it said something about if you add zero to a variable it turns it from a string into an int
so that may be it, iuno

foxyshadis
16th June 2006, 21:35
In Bash, Javascript, and sometimes Perl and PHP, that works, but doesn't really apply to the amazingly overspecified type syntax of Java.