unused statics?

B

bob smith

So, I found this in one of my classes:

static int ctr = 0;

It turns out it wasn't being used anywhere. I thought Eclipse would warn
me about this, but it didn't. I looked thru the options, and I couldn't find anything to change.

Is there any way to get warned about those unused statics?

Thanks.
 
L

Lew

bob said:
So, I found this in one of my classes:

static int ctr = 0;

It turns out it wasn't being used anywhere. I thought Eclipse would warn
me about this, but it didn't. I looked thru the options, and I couldn't find anything to change.

Is there any way to get warned about those unused statics?

Declare it private.

There's no way for a system to know if an accessible element is being accessed outside the project.

Why was it assigned its default value redundantly?
 
A

Arne Vajhøj

So, I found this in one of my classes:

static int ctr = 0;

It turns out it wasn't being used anywhere. I thought Eclipse would warn
me about this, but it didn't. I looked thru the options, and I couldn't find anything to change.

Is there any way to get warned about those unused statics?

I think you can only do that for private.

Eclipse have no way of knowing if are going to email
me the class file and I will write a program that use it.

A non final field should be private anyway according to
good OO practice.

Arne
 
M

markspace

I often do that as a note to myself. It means I do expect to use the
initial value, have considered what the initial value should be, and
have chosen 0.


Assignment is also required if the field is final, even if you intend to
use the default value.
 
A

Arne Vajhøj

I often do that as a note to myself. It means I do expect to use the
initial value, have considered what the initial value should be, and
have chosen 0.

I think that it is easier to read code that are consistent
in initialization.

A class where all fields are initialized in declaration,
a class where all fields are initialized in constructor or a
class where all fields get default value is easy to understand
in my opinion.

If 1/3 of fields are are initialized in declaration, 1/3 in
constructor and 1/3 get get default value then the reader will
spend the first 10 minutes trying to guess what the developers
reason for doing so is.

Arne
 
L

Lew

Not this time. Was it the OP's intention? Why not let the OP answer?

Was that the OP's intention? Why not let the OP answer?

In general, I agree. That didn't seem to apply here, so I asked the OP specifically
why in this case they did that.
I think that it is easier to read code that are consistent
in initialization.

A class where all fields are initialized in declaration,
a class where all fields are initialized in constructor or a
class where all fields get default value is easy to understand
in my opinion.

If 1/3 of fields are are initialized in declaration, 1/3 in
constructor and 1/3 get get default value then the reader will
spend the first 10 minutes trying to guess what the developers
reason for doing so is.

There are engineering reasons for each of the three choices. Sometimes
those reasons vary within the same class. So then you use comments to
explain the divergence. Whenever I deem it worthwhile to take the tiny
performance hit of redundantly assigning the same value to a member twice,
I comment why it's done.

So I think you'd find that readable, too.

Here's why you need more than one way to do things:

public class Foo<T>
{
private final Class<T> rttt;
private boolean initialized = false; // explicitly initialized to emphasize the logic
private Bar bar;

public Foo(Class<T> rttt)
{
if (rttt == null) {throw new IllegalArgumentException("null rttt");}
this.rttt = rttt;
}

public void setBar(Bar bar)
{
this.bar = bar;
}

public Bar getBar()
{
return bar;
}
}

I see no readability problems with this.
 
A

Arne Vajhøj

Not this time.

I am not able to say whether a certain construct improve
readability or not based on a single line.

And as I explained later then my main criteria is
what the other lines do.

Do you use mind reading to see what the other lines are
or?
Was it the OP's intention?


To write readable code? I assume so!
Why not let the OP answer?

It is rather common to comment on comments here.
There are engineering reasons for each of the three choices. Sometimes
those reasons vary within the same class. So then you use comments to
explain the divergence. Whenever I deem it worthwhile to take the tiny
performance hit of redundantly assigning the same value to a member twice,
I comment why it's done.

So I think you'd find that readable, too.

But having to comment on something that can be written so no
comments are necessary does not seem optimal. And one could still
wonder why.
Here's why you need more than one way to do things:

public class Foo<T>
{
private final Class<T> rttt;
private boolean initialized = false; // explicitly initialized to emphasize the logic
private Bar bar;

public Foo(Class<T> rttt)
{
if (rttt == null) {throw new IllegalArgumentException("null rttt"); }
this.rttt = rttt;
}

public void setBar(Bar bar)
{
this.bar = bar;
}

public Bar getBar()
{
return bar;
}
}

I see no readability problems with this.

I do.

Not a big problem - far from.

But the asymmetry still gives a desire to spend more time to
read the code to try and understand why it was done this way.

Arne
 
A

Arne Vajhøj

I am not able to say whether a certain construct improve
readability or not based on a single line.

And as I explained later then my main criteria is
what the other lines do.

Do you use mind reading to see what the other lines are
or?



To write readable code? I assume so!


It is rather common to comment on comments here.


But having to comment on something that can be written so no
comments are necessary does not seem optimal. And one could still
wonder why.


I do.

Not a big problem - far from.

But the asymmetry still gives a desire to spend more time to
read the code to try and understand why it was done this way.

I would have done it as:

public class Foo<T> {
private final Class<T> rttt;
private boolean initialized;
private Bar bar;
public Foo(Class<T> rttt) {
if (rttt == null) {throw new IllegalArgumentException("null rttt"); }
this.rttt = rttt;
this.initialized = false;
this.bar = null;
}
....

A quick glance on the constructor will tell what everything is
and because it seems consistent, then one would not worry and
check if there is something funky going on.

Arne
 
M

Magnus Warker

There are engineering reasons for each of the three choices.

This has absolutely nothing to do with "engineering reasons" or large
software projects. It's just the bullshit of your personal microcosm.

Magnus
 
G

Gene Wirchenko

[snip]
I think that it is easier to read code that are consistent
in initialization.

Sure, but you are using a different sort of consistency.

My consistency is to initialise as close to where the variable is
used. If I need to change the initialisation for a section of code,
it is right there.

That said, I prefer to have my declarations near the related
code. I dislike languages that require all declarations before all
code in a block.
A class where all fields are initialized in declaration,
a class where all fields are initialized in constructor or a
class where all fields get default value is easy to understand
in my opinion.

If 1/3 of fields are are initialized in declaration, 1/3 in
constructor and 1/3 get get default value then the reader will
spend the first 10 minutes trying to guess what the developers
reason for doing so is.

If they are all initialised in declarations, then the reader will
have to determine if that initialisation is really it.

Sincerely,

Gene Wirchenko
 
R

Roedy Green

It turns out it wasn't being used anywhere. I thought Eclipse would warn
me about this, but it didn't. I looked thru the options, and I couldn't find anything to change.

It has been a long time since I used Eclipse, but in IntelliJ you must
run the Inspector/lint. you static is legal Java, just nugatory.

googling "Eclipse Lint" gave me several packages, but did not clarify
if there is one built-in.
--
Roedy Green Canadian Mind Products http://mindprod.com
The first 90% of the code accounts for the first 90% of the development time.
The remaining 10% of the code accounts for the other 90% of the development
time.
~ Tom Cargill Ninety-ninety Law
 
A

Arne Vajhøj

It has been a long time since I used Eclipse, but in IntelliJ you must
run the Inspector/lint.

No tool should warn against this construct.
you static is legal Java, just nugatory.

No. There can be lots of code using that static.

Arne
 
A

Arne Vajhøj

[snip]
I think that it is easier to read code that are consistent
in initialization.

Sure, but you are using a different sort of consistency.

My consistency is to initialise as close to where the variable is
used. If I need to change the initialisation for a section of code,
it is right there.

That said, I prefer to have my declarations near the related
code. I dislike languages that require all declarations before all
code in a block.

????

I am talking about fields not local variables.

Arne
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top