Thursday, April 7, 2011

Final is my favorite Java keyword

Final is my favorite keyword and modifier in Java.  For fields, love it even more than private.  Think about it, you don't even need private on your field if it's final.  Not with a truly non-mutable data type at least, so collections and things of that nature excluded of course.

In fact I wish final was the default modifier for all my fields, variables, and parameters.  You should have to announce your intention to overwrite an already initialized object reference.  After years, I find it is a rarity.

There is nothing worse that digging through a large 500 line function and not being able to see what is changed and where.  Even more frustrating when you go to extract a method from said too-large codeblock and cannot do so easily because of the sprawled out variable declarations and initializations and the occasional "I like this variable name so I'll reuse the reference" laziness.

The trouble is it looks so terrible when used properly, which should be almost everywhere!  It looks pretty satisfying on fields, but over all parameters and variables it gets to be a little much.  It can actually make it harder to find the non-final object references.  And if you find one, was it intentional or just overlooked? Change it and find out I guess.

That is why one of my biggest wishes for the Java language is that final be the default and there to be a mutable or similar keyword for the handful of times you need it.  It would be an excellent aid to readers of your code, "watch out this thing is going to change." Compilers could even check to see if you have flagged something mutable and aren't actually mutating it.  That should be a compile error for variables and parameters, perhaps not for fields though.

Obviously we can't exactly do that ... not so directly.

We have some advantages now we didn't have when Java was created.  Annotations.  When Java was created there had to be long and deliberate thinking as to what the defaults should be.  Everyone would have to live with them and they'd last forever.  These days, however, a simple annotation or two on a Java source file could serve as a clean way to change such defaults in an obvious and documented way that can make the code in question far easier to read.  It would be syntactic sugar of course and that annotation and the defaults it specifies would compile away just as imports do.

A small bit of sugar with big payoff in ease and readability.

29 comments:

default said...

Cool, and if all your variables are final, you don't even need to run the code! You can optimize it away completely in the compiler and reduce it to its (constant) result.

tug said...

I agree completely.

Adam Rabung said...

+1000
Two little nits - a private final field is still better than a final field, because it encapsulates/hides the field - minimizes api surface area. Also, "mutable" keyword would not be good because final Objects _are_ mutable, they're just not reassignable.

David Blevins said...

@Adam Good point. Perhaps 'assignable' is the better keyword. In terms of a get method for a final reference to a non-mutable object, was more making a statement of technical necessity rather than one of best practices. Likely high on the list of things we do that hotspot will remove because it isn't technically necessary.

Viktor Klang said...

Ortwin: Well, I kinda need branching, so your fold-into-constant-result will have to wait.

Brian said...

This is exactly the sort of thing you can do for yourself with JSR-308 and the Checkers framework. Write your own @FinalByDefault class annotation and @Mutable field annotation, write a checker to verify your rules, and you're done.

You can start on this today using the JSR-308 prototype now!

timothymattie said...

more or less into collaboration. Wow! Right now probably weigh over head but what do you guess(ebaan) is direction of a WLAn Coova Hotspot is it worth looking up related. API's, FBML's,FBTS'S,and FQL's.Coova has good Open source, and Java credentials! Appreciated!!
timothymattie@gmail.com
twitter.com/timothymattie

Unknown said...

20170619 junda
oakley sunglasses sale
cheap jerseys wholesale
burberry outlet sale
cheap ray ban sunglasses
adidas uk store
michael kors outlet clearance
cheap nba jerseys
true religion outlet
ray ban sunglasses
nike air max 90

Habib Malik said...

North East Post Office Result 2017
Chhattisgarh Post Office GDS Result 2017
Uttarakhand Post Office GDS Result 2017
Haryana Post Office GDS Result 2017
Tamil Nadu Post Office GDS Result 2017

Unknown said...

good post

-----------
شركة تنظيف بالرياض

Danish Ahmed said...

Thanks for sharing this article, now you can also check your Recuritment & Notification 2017-2018 by following below given links...

fci andhra pradesh watchman recuritment 2017

fci tamilnadu watchman recuritment 2017

xat notification 2018

jkbote polytechnic result 2017

Unknown said...

Get the best cleaning service from our company to clean the months and the best cleaning companies in the Kingdom of Saudi Arabia, the company cleaning houses, apartments, villas, companies and factories in all the contents such as floors, walls, windows, kitchens, bathrooms and facades of houses and furniture and bedding and insulation of roofs and reservoirs, you can contact us and request one of our services Get the best cleaning service now
شركة تنظيف بتبوك

شركة تنظيف بضباء

شركة تنظيف ببريدة

شركة تنظيف بأبها

شركة تنظيف بخميس

شركة تنظيف بجده

Unknown said...

A leading company in the field of cleaning as it is doing all kinds of cleaning such as villas, apartments, houses and companies, and we also clean carpet and carpet all with the best equipment and the best workers, we work under a business plan that makes us the best in this area, do not hesitate to contact us and be one of our customers Valued We guarantee your working speed and high quality.
شركة تنظيف بتبوك

شركة تنظيف بضباء

شركة تنظيف ببريدة

شركة تنظيف بأبها

شركة تنظيف بخميس

شركة تنظيف بجده

Unknown said...

A leading company in the field of cleaning as it is doing all kinds of cleaning such as villas, apartments, houses and companies, and we also clean carpet and carpet all with the best equipment and the best workers, we work under a business plan that makes us the best in this area, do not hesitate to contact us and be one of our customers Valued We guarantee your working speed and high quality.
شركة تنظيف بمكة

شركة تنظيف بالقطيف

شركة تنظيف بجازان

شركة تنظيف بنجران

شركة تنظيف بعنيزة

شركة تنظيف ببيشه

Bilibili said...

Pandora Jewelry
Jordan 11
Adidas Yeezy
Pandora Jewelry
Kyrie 3 Shoes
Retro Jordan 11
Air Jordan Retro 9
Nike Air Max 270
Red Bottom Shoes For Women
Rodney20181226

Bilibili said...

Air Jordan 11
Jordan 4
Pandora
Red Bottom Shoes For Women
Yeezy boost 350 v2
Jordan Retro
Pandora Jewelry Outlet
Kyrie Shoes
Nike Air Max 270
Air Jordan 9
Rodney20181231

Bilibili said...

Kyrie Irving Shoes
Air Jordan 11
Air Jordan 9
Jordans 11
Yeezy boost 350 v2
Jordan 4 Retro
Jordan 11
Pandora Jewelry Outlet
Air Max 270
Rodney20190313

Hualala said...

Yeezy boost 350 v2
Jordan 11
Jordan Retro 11
Air Max 270
Air Jordan
Red Bottom Shoes
Air Jordan 4 Retro
Pandora Charms
Pandora Outlet
Paul20190318

Coqicoqi said...

Red Bottom Shoes For Women
Pandora Jewelry
Pandora
Pandora Outlet
Jordan Retro 11
Air Max 270
Jordan 11
Kyrie Irving Shoes
Adidas Yeezy
Air Jordan Retro 11
Ryan20190325

PalaPala said...

Kyrie Irving Shoes
Jordan 11
Jordan Retro 11
Air Jordan Retro 9
Pandora Jewelry
Air Jordan Retro 11
Red Bottom Shoes For Women
Yeezy boost
Jordan 4
Latrice20190330

oveiya krishnasamy said...

Nice tips. Very innovative... Your post shows all your effort and great experience towards your work Your Information is Great if mastered very well.
cloud computing online training

Kiruthiprabha said...

This is very good content you share on this blog. it's very informative and provide me future related information.
Oracle DBA Online Training

rithiaanandh said...

Thanks for sharing the informative concept. Your data visualization contents are really good. You will explain the concept is a very clear and detailed explanation.
Sql server dba online training

jothikumar said...

This is such a good post. One of the best posts that I've read in my whole life. I am so happy that you chose this day to give me this. Please, continue to give me such valuable posts. Cheers!
Software testing online training
Software testing certification training
Software testing online course
Software testing training course

Darren Demers said...

The trouble is it looks so terrible when used properly, which should be almost everywhere! It looks pretty satisfying on fields, but over all parameters and variables it gets to be a little much. It can actually make it harder to find the non-final object references. And if you find one, was it intentional or just overlooked? Change it and find out I guess. cheap t shirts wholesale , wholesale bulk t shirts free shipping , jersey style shirts wholesale , local wholesale t shirts , best bulk t shirts , bulk buy raglan t shirts , wholesale childrens clothing in bulk , designer baby clothes online , cheap dresses for kids , where can i buy baby clothes wholesale

bandarq228 said...

manfaatkan gadget anda dan hasilkan uang kapanpun dan dimananpun dengan mudah hanya dengan bermain dominoqq atau bandarqq di situs terbaik asikbandarqq
daftarkan diri anda sekarang juga hanya di http://180.215.13.115

Escorts Service Kolkata said...

escorts service in kolkata |
escorts service in kolkata |
escorts service in kolkata |
escorts service in kolkata |
escorts service in kolkata |

neausmey said...

pop over to these guys replica ysl bags discover this replica designer bags see page gucci replica handbags

Anonymous said...

Look At This f8z82d5t22 cheap designer bags replica replica bags paypal accepted replica bags canada gucci replica s2r88y1j71 replica bags lv weblink y9n26g1w05 replica bags replica bags blog y8k49u7z90