Why use line when many line do trick
-
Was recently porting some old code and came across this beautiful bit of coding
if(BlueJunkAPI.needsAttention(msgId)) { switch(msgId) { case BlueJunkAPI.ATTENTIONREQUIRED: socketError = handleAttentionReq(messageOut, messageIn); } }
public static boolean needsAttention(int messageId) { switch(messageId) { case ATTENTIONREQUIRED: return(true); default: return(false); } }
Yes, ladies and gentlemen, that's 18 lines of code, including an IF statement and not one, but two SWITCHes to compare two integers and then make a function call
-
@campkev said in Why use line when many line do trick:
switch(BlueJunkAPI.needsAttention(msgId)) { case true: switch(msgId) { case BlueJunkAPI.ATTENTIONREQUIRED: socketError = handleAttentionReq(messageOut, messageIn); } }
FTFY
-
@error said in Why use line when many line do trick:
@campkev said in Why use line when many line do trick:
switch(BlueJunkAPI.needsAttention(msgId)) { case true: switch(msgId) { case BlueJunkAPI.ATTENTIONREQUIRED: socketError = handleAttentionReq(messageOut, messageIn); } }
FTFY
I can't help but notice a potential race condition in your code:
switch(BlueJunkAPI.needsAttention(msgId)) { case true: switch(msgId) { case BlueJunkAPI.ATTENTIONREQUIRED: if(BlueJunkAPI.needsAttention(msgId)){ socketError = handleAttentionReq(messageOut, messageIn); } } }
-
@boomzilla said in Why use line when many line do trick:
@error said in Why use line when many line do trick:
@campkev said in Why use line when many line do trick:
switch(BlueJunkAPI.needsAttention(msgId)) { case true: switch(msgId) { case BlueJunkAPI.ATTENTIONREQUIRED: socketError = handleAttentionReq(messageOut, messageIn); } }
FTFY
I can't help but notice a potential race condition in your code:
switch(BlueJunkAPI.needsAttention(msgId)) { case true: switch(msgId) { case BlueJunkAPI.ATTENTIONREQUIRED: if(BlueJunkAPI.needsAttention(msgId)){ socketError = handleAttentionReq(messageOut, messageIn); } } }
You still haven't locked BlueJunkAPI. And if
msgId
is volatile so aremessageIn
andmessageOut
, need to kill myself now, brb.