473,407 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Deferred Final Automatic Variables

I've been reading the Java Language Specification, and in Chapter 16 there's
an interesting topic called Definite Assignment.

http://tinyurl.com/3fqk8

I'm wondering about the idea of "Deferred Final Automatic Variables" like
the following:

void unflow(boolean flag) {
final int k;
if (flag) {
k = 3;
System.out.println(k);
}
else {
k = 4;
System.out.println(k);
}
}

Most developers I know avoid the final keyword completely. They also try to
always assign an initial value when they define the variable. So instead of
the above example, I'm used to seeing this:

void unflow(boolean flag) {
int k = -1;
if (flag) {
k = 3;
System.out.println(k);
}
else {
k = 4;
System.out.println(k);
}
}

I like the first example better because it's more clear to me. It's saying
that k can only be assigned once, and at a glance, I know the assignment can
only be 3 or 4, nothing else. Even if the method goes on for many
screen-fulls, this assignment is set in stone, even though there's a
condition involved.

My point is, however, that I rarely see deferred assignment in real
projects. The final keyword like in the first example is almost never used
even though, unlike the other uses of final, it has no negative drawback to
inheritance.

Is the first example a better coding practice? If so, why isn't it done
more?
Anthony

--
Got a blog? Post it:
http://www.martin-studio.com/weblog-index/

Jul 17 '05 #1
1 3404
Anthony Martin wrote:
I've been reading the Java Language Specification, and in Chapter 16
there's an interesting topic called Definite Assignment.

http://tinyurl.com/3fqk8

I'm wondering about the idea of "Deferred Final Automatic Variables" like
the following:

void unflow(boolean flag) {
final int k;
if (flag) {
k = 3;
System.out.println(k);
}
else {
k = 4;
System.out.println(k);
}
}

Most developers I know avoid the final keyword completely.
Most developers are wrong. Using final in this way will never do harm, and
may do some good; it's a useful hint to both the human reader and the JIT
compiler.
They also try
to
always assign an initial value when they define the variable.
So they never get any compiler errors resulting from the lack of a definite
assignment before use. ;> This too is a form of laziness.
So instead
of the above example, I'm used to seeing this:

void unflow(boolean flag) {
int k = -1;
if (flag) {
k = 3;
System.out.println(k);
}
else {
k = 4;
System.out.println(k);
}
}

I like the first example better because it's more clear to me. It's
saying that k can only be assigned once, and at a glance, I know the
assignment can
only be 3 or 4, nothing else. Even if the method goes on for many
screen-fulls, this assignment is set in stone, even though there's a
condition involved.

My point is, however, that I rarely see deferred assignment in real
projects. The final keyword like in the first example is almost never
used even though, unlike the other uses of final, it has no negative
drawback to inheritance.
All I can say is: thanks for reminding me of the virtues of 'final' in this
context. I'll try to use it more often, and teach other people to use it
more often, and then maybe you'll see it being used more often. :)
Is the first example a better coding practice? If so, why isn't it done
more?
Anthony


--
Chris Gray ch***@kiffer.eunet.be

Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bryan | last post by:
i was intrigued with a recently posted cookbook recipe which implements deferred results with decorators: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/355651 the problem i see with...
193
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
8
by: Frank van Vugt | last post by:
Hi, If during a transaction a number of deferred triggers are fired, what will be their execution order upon the commit? Will they be executed in order of firing or alfabetically or...
7
by: news | last post by:
Recently our mail from our e-commerce site has been rejected by AOL due to an IP block because someone was using our PHP scripts to send spam. Well, I got that fixed. But our legitimate...
6
by: main() | last post by:
I'm a newbie. These questions arose out of my curiosity, Please pardon me if this questions sound silly. 1. Why are the automatic variables are left uninitialized by default( as i understand...
58
by: Jorge Peixoto de Morais Neto | last post by:
I was reading the code of FFmpeg and it seems that they use malloc just too much. The problems and dangers of malloc are widely known. Malloc also has some overhead (although I don't know what is...
4
by: acw | last post by:
I am trying to understand how IE handles automatic tag variables. I know that IE will create a global variable each time it finds a tag with its name or id attribute set. If you have more than...
25
by: sidd | last post by:
In the following code: int i = 5; ---it goes to .data segment int j; ---it goes to bss segment int main() { int c; int i = 5; ---stack
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.