Reduce
#1

Is there a way to reduce this code to less lines?

pawn Код:
if(!strcmp(sub_cmd, "joinmsg"))
    {
        switch (Player[playerid][JoinMsg])
    {
        case 0:
        {
            Player[playerid][JoinMsg] = 1;
            SCM(playerid, COLOR_YELLOW, "* You have enabled join messages.");
        }
        case 1:
        {
            Player[playerid][JoinMsg] = 0;
            SCM(playerid, COLOR_YELLOW, "* You have disabled join messages.");
        }
    }  
    }
Thanks.
Reply
#2

Why further reduce the code? Not because it's shorter means it's faster.

replaces "if(!strcmp(sub_cmd, "joinmsg"))" with "if(!strcmp(sub_cmd, "joinmsg", true))"
Reply
#3

Quote:
Originally Posted by Undef1ned
Посмотреть сообщение
Why further reduce the code? Not because it's shorter means it's faster.

replaces "if(!strcmp(sub_cmd, "joinmsg"))" with "if(!strcmp(sub_cmd, "joinmsg", true))"
Код:
replaces "if(!strcmp(sub_cmd, "joinmsg"))" with "if(!strcmp(sub_cmd, "joinmsg", true))"
Why are you advising him to do that? The 'sub command' is specifically 'joinmsg' and not 'JoInMsG' or 'JoinMsg'.
Reply
#4

Exactly.

I wanna reduce it not because i want it faster, but i hate long codes when settings a variable to 0/1...
Reply
#5

Код:
if(!strcmp(sub_cmd, "joinmsg"))
{
    switch (Player[playerid][JoinMsg])
    {
        case 0:
        {
            Player[playerid][JoinMsg] = 1;
            SCM(playerid, COLOR_YELLOW, "* You have enabled join messages.");
        }
        case 1:
        {
            Player[playerid][JoinMsg] = 0;
            SCM(playerid, COLOR_YELLOW, "* You have disabled join messages.");
        }
    }   
}
16 lines.

Код:
if(!strcmp(sub_cmd, "joinmsg"))
{
    if(Player[playerid][JoinMsg])
    {
        Player[playerid][JoinMsg] = 0;
        SCM(playerid, COLOR_YELLOW, "* You have disabled join messages.");
    }
    else
    {
        Player[playerid][JoinMsg] = 1;
        SCM(playerid, COLOR_YELLOW, "* You have enabled join messages.");
    }
}
13 lines.

Код:
if(!strcmp(sub_cmd, "joinmsg"))
{
    Player[playerid][JoinMsg] = !Player[playerid][JoinMsg];
    if(Player[playerid][JoinMsg])
    {
        SCM(playerid, COLOR_YELLOW, "* You have enabled join messages.");
    }
    else
    {
        SCM(playerid, COLOR_YELLOW, "* You have disabled join messages.");
    }
}
12 lines.

Код:
if(!strcmp(sub_cmd, "joinmsg"))
{
    if((Player[playerid][JoinMsg] = !Player[playerid][JoinMsg]))
    {
        SCM(playerid, COLOR_YELLOW, "* You have enabled join messages.");
    }
    else
    {
        SCM(playerid, COLOR_YELLOW, "* You have disabled join messages.");
    }
}
11 lines.

Код:
if(!strcmp(sub_cmd, "joinmsg"))
{
    SCM(playerid, COLOR_YELLOW, (Player[playerid][JoinMsg] = !Player[playerid][JoinMsg]) ? ("* You have enabled join messages.") : ("* You have disabled join messages."));
}
4 lines.

Код:
if(!strcmp(sub_cmd, "joinmsg")) SCM(playerid, COLOR_YELLOW, (Player[playerid][JoinMsg] = !Player[playerid][JoinMsg]) ? ("* You have enabled join messages.") : ("* You have disabled join messages."));
1 line.

Код:
if(!strcmp(sub_cmd, "joinmsg")){switch (Player[playerid][JoinMsg]){case 0:{Player[playerid][JoinMsg] = 1;SCM(playerid, COLOR_YELLOW, "* You have enabled join messages.");}case 1:{Player[playerid][JoinMsg] = 0;SCM(playerid, COLOR_YELLOW, "* You have disabled join messages.");}}}
But this would also be one line. The right balance between legibility and compactness is somewhere in the middle.
Reply
#6

Lmao that's what i was looking for. Thanks.
Reply
#7

Quote:
Originally Posted by ******
Посмотреть сообщение
Код:
Player[playerid][JoinMsg] ^= true;
^=?

Never heard of this. What does?
Reply
#8

its not in it.
i know it has been told somewhere but its not in it.

But since he may had defined it as an integer, I guess ^= will give the ability to set it like a bool?
Reply
#9

Quote:
Originally Posted by GangstaSunny.
Посмотреть сообщение
its not in it.
i know it has been told somewhere but its not in it.
It is, took me 10 seconds to confirm that - 9 of those just for downloading the file.
Reply
#10

Quote:
Originally Posted by ******
Посмотреть сообщение
It is, took me 10 seconds to confirm that - 9 of those just for downloading the file.
founds, thanks.
my smartphone processor was just not fast enough to find the symbol in this big pdf file. lol.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)