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]

> 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.
> > 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
> 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.
; )
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
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;
}