Reducing no. of braces

Hi,

I have a code like this

publicstatic ACCommunicator getInstance()

{

//PI001468

if(inst ==null)

{

synchronized(ACCommunicator.class)

{

if(inst ==null)

{

try

{

inst =new ACCommunicator();

}

catch(RemoteException e)

{

trace.errorm("Remote exception: ", e);

TraceController.setProperty("TraceBuffer_dump","true");

}

}

}

}

return inst;

}

Now can i have the same code with less number of open braces !! Max allowed is 4. How can i do it without compromising the sync feature.

Thanx in advance

[1447 byte] By [hi_alla] at [2007-11-26 16:32:55]
# 1

> Now can i have the same code with less number of open

> braces !! Max allowed is 4. How can i do it without

> compromising the sync feature.

>

> Thanx in advance

What do you think could be the solution? It's better to leave the braces as they are.

prometheuzza at 2007-7-8 22:57:31 > top of Java-index,Java Essentials,Java Programming...
# 2

> > Now can i have the same code with less number of

> open

> > braces !! Max allowed is 4. How can i do it

> without

> > compromising the sync feature.

> >

> > Thanx in advance

>

> What do you think could be the solution? It's

> better to leave the braces as they are.

Actually, were he to synchronize the method he could leave out the synchronize block in the method. In fact, is that not the recommended way of doing something like this?

Edit: typos

masijade.a at 2007-7-8 22:57:31 > top of Java-index,Java Essentials,Java Programming...
# 3

> Actually, were he to synchronize the method he could

> leave out the synchronize block in the method. In

> fact, is that not the recommended way of doing

> something like this?

>

> Edit: typos

Yes, sorry for the confusion, I meant to make a point that:if(boolean) {

doA();

}

doB();

is better thanif(boolean)

doA();

doB();

IMHO.

; )

prometheuzza at 2007-7-8 22:57:31 > top of Java-index,Java Essentials,Java Programming...
# 4

I assume this is part of an assignment of some sort? I'll point you in the right direction - you don't need braces when the section of code consists of one line...

for example

for(int i = 0; i < 10; i++)

{

doSingleOperation....

}

does not need braces and will work fine as:

for(int i = 0; i < 10; i++)

doSingleOperation;

it is a big risky though - you can easily add more lines of code to the loop and forget to add in the brackets as needed.

AS

AberStudenta at 2007-7-8 22:57:31 > top of Java-index,Java Essentials,Java Programming...
# 5
Point taken and agreed upon. ;-)Edit: From prometheuz (is that correctly spelled, probably not).
masijade.a at 2007-7-8 22:57:31 > top of Java-index,Java Essentials,Java Programming...
# 6
It looks like you are trying to implement the singleton design pattern with lazy loading. There is an easier way to do it. See the example implementation here: http://en.wikipedia.org/wiki/Singleton_pattern#Java
jsalonena at 2007-7-8 22:57:31 > top of Java-index,Java Essentials,Java Programming...
# 7
@Op. You have implemented double-checked locking, and that isn't good.Kaj
kajbja at 2007-7-8 22:57:31 > top of Java-index,Java Essentials,Java Programming...
# 8

I would do it like this (after only a quick glance so don't crucify me, but feel free to criticize me):

public static synchronized ACCommunicator getInstance() {

//PI001468

if(inst == null) {

try {

inst = new ACCommunicator();

} catch(RemoteException e) {

trace.errorm("Remote exception: ", e);

TraceController.setProperty("TraceBuffer_dump", "true");

}

}

return inst;

}

masijade.a at 2007-7-8 22:57:31 > top of Java-index,Java Essentials,Java Programming...