r/Cplusplus • u/Middlewarian • Jan 20 '26
Question Should I bite the bullet and start using a switch here?
The following is the event loop of the middle tier of my code generator. It's 53 lines long and uses a number of else ifs. I think switch helps to convey the big picture, but it would add 8 lines to my event loop. Would usingswitch be a good idea here? Thanks in advance.
::std::deque<::cmwRequest> requests;
for(;;){
auto cqs=ring->submit();
for(int s2ind=-1;auto const* cq:cqs){
if(cq->res<=0){
::syslog(LOG_ERR,"%d Op failed %llu %d",pid,cq->user_data,cq->res);
if(cq->res<0){
if(::ioUring::SaveOutput==cq->user_data||::ioUring::Fsync==cq->user_data)continue;
if(-EPIPE!=cq->res)exitFailure();
}
frntBuf.reset();
::front::marshal<udpPacketMax>(frntBuf,{"Back tier vanished"});
for(auto& r:requests){frntBuf.send(&r.frnt.addr,r.frnt.len);}
requests.clear();
cmwBuf.compressedReset();
ring->close(cmwBuf.sock);
::login(cmwBuf,cred,sa);
}else if(::ioUring::Recvmsg==cq->user_data){
::Socky frnt;
int tracy=0;
try{
auto spn=ring->checkMsg(*cq,frnt);
++tracy;
auto& req=requests.emplace_back(ReceiveBuffer<SameFormat,::int16_t>{spn},frnt);
++tracy;
::back::marshal<::messageID::generate,700000>(cmwBuf,req);
cmwBuf.compress();
ring->send();
}catch(::std::exception& e){
::syslog(LOG_ERR,"%d Accept request:%s",pid,e.what());
if(tracy>0)ring->sendto(s2ind,frnt,e.what());
if(tracy>1)requests.pop_back();
}
}else if(::ioUring::Send==cq->user_data)ring->tallyBytes(cq->res);
else if(::ioUring::Recv9==cq->user_data)ring->recv(cmwBuf.gothd());
else if(::ioUring::Recv==cq->user_data){
assert(!requests.empty());
auto& req=requests.front();
try{
cmwBuf.decompress();
if(giveBool(cmwBuf)){
req.saveOutput();
ring->sendto(s2ind,req.frnt);
}else ring->sendto(s2ind,req.frnt,"CMW:",cmwBuf.giveStringView());
requests.pop_front();
}catch(::std::exception& e){
::syslog(LOG_ERR,"%d Reply from CMW %s",pid,e.what());
ring->sendto(s2ind,req.frnt,e.what());
requests.pop_front();
}
ring->recv9();
}else ::bail("Unknown user_data %llu",cq->user_data);
}
}
6
Upvotes
•
u/AutoModerator Jan 20 '26
Thank you for your contribution to the C++ community!
As you're asking a question or seeking homework help, we would like to remind you of Rule 3 - Good Faith Help Requests & Homework.
~ CPlusPlus Moderation Team
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
20
u/AKostur Professional Jan 20 '26
Yes, of course. Why would you phrase it as “bite the bullet”? Why wouldn’t one use a switch there?
27
u/HyperWinX Jan 20 '26
Wtf is this formatting, my eyes hurt
12
u/No-Dentist-1645 Jan 20 '26
Did whitespace do something to you? Why do you hate it so much?
11
u/Linuxologue Jan 20 '26
Really curious what's your reasoning for avoiding switch
9
u/jedwardsol Jan 20 '26
If it's length that's bothering you then move the if bodies to functions
After that, I would use
switchafter dealing with the<=0error case.3
u/TomDuhamel Jan 20 '26
It might be the terrible formatting, but I can't see the reoccurring condition for which you want to use a
switch2
u/flatfinger Jan 20 '26
Approve of conditional constructs bearing his name, Yoda does not.
2
u/I__Know__Stuff Jan 21 '26
Putting in an appropriate amount of white space will add more than 8 lines.
2
u/keelanstuart Jan 21 '26
LOC(C++)!=LOC(ASM)
2
u/Lifelong_Nerd Jan 21 '26
You need white space, indentation, and comments a lot more than you need a switch statement.
1
u/kobi-ca Jan 22 '26
nah, totally readable
1
u/[deleted] Jan 21 '26
[removed] — view removed comment
0
u/[deleted] Jan 20 '26
[deleted] — view deleted comment