Some questions
#1

Hello.

I have few questions here. Some of the questions can be retardish but I just want to get good answers.

#1:
I know this question has been asked before but I am still not sure about it. Does the code lines really matter?
Call me a dumb but do these 2 ways of creating variables make any difference?

Like you know some people do
PHP код:
new
    
somevar,
    
somearray,
    
shits
or some simply do
PHP код:
new somevarsomearrayshits
The first one would take few more lines while the second one just takes one.
I am pretty obsessed with designing the code layout so I was wondering if this matters.

#2: Is it ok if i use 'continue' for the following purposes? Like instead of doing this

PHP код:
if (something == 1)
otherthing 0
Using continue for it like
PHP код:
if (something == 0) continue;
otherthing 0
If there's any difference between these 2 method, which would be more efficient to use?

#3: Is creating huge cell for dialog strings a good idea? For example; I need a dialog that contains player's stats and total string length for it would be around 1500. I even have some other dialogs that needs 3000 cell of string to work.

#4:
Among these 2 method, which one would be a good way to do so
PHP код:
if (something == 0)
    
// Do stuff
else
    
SendClientMessage(...); 
or
PHP код:
if (something == 1)
    return 
SendClientMessage(...);
// Do stuff 
I am sorry if these questions are really lame. Also, sorry if this isn't a correct section to ask something like this, as I didn't find any other suitable sections. Thanks.
Reply
#2

1. No
2. Do not use continue with it, absolutely no reasons to, the script knows it should continue the code there.
3. Its not good to use huge string sizes but they are needed sometimes therefore if you can find a better way, do it that way if not do it with a huge string. Bare in mind dialogs are limited to certain string size
4. No difference. Depends what do you want to do. I usualy check for false first and then continue. Ie:

PHP код:
if(GetPlayerMoney(playerid) < 500) return Msg(playerid,COLOR_RED,"You dont have enough $$"); //check for false
Msg(playerid,COLOR_RED,"You have 500$ or more"); 
Just makes it easier for me. But there aint much difference. Else can sometimes be a bitch when fucking around with many variables
Reply
#3

Good, I hope I can give you good answers.

#1:
This won't change the performance, except the row number is different. The performance is the same.

#2:
continue will brings you nothing because continue; is going on to the next, for example:
PHP код:
for(new i;i<MAX_PLAYERS;i++)
{
    if(!
IsPlayerConnected(i))continue;
    print(
"playerid %i is online",i);

When ID 1 isn't online, the loop will go to ID 2 because the continue is going to the next one.

#3:
When you need the huge string, then you can use it. It takes some memory, but 1500 is well. If you use 6000 it will be bad.

#4:
The second way is good, because you have fewer rows then method 1.

__________________

Sorry, when I wrote in a bad english, but I try to help you.
Reply
#4

1. It doesn't matter how you do it, your gamemode is basically one line.
2. You can't even do that, you'll get an error ("error 024: "break" or "continue" is out of context"). Use it in loops.
3. If you really need it to be that large, then there's no problem with that. But keep in mind that the larger a string's cells are, the slower it takes to process them (e.g. format).
4. However you like to do that.
Reply
#5

Thanks for answering people. Appreciated.

Quote:
Originally Posted by SickAttack
Посмотреть сообщение
2. You can't even do that, you'll get an error ("error 024: "break" or "continue" is out of context"). Use it in loops.
I know I have to use it inside a loop. That was just an example. However, can you tell me in which condition continue; is needed? I kinda didn't understand Mencent's answer.

Also, do I have to use break; to stop every loop? Even while using foreach. No matter if it's a small code or a large one.
Reply
#6

Quote:
Originally Posted by Sjn
Посмотреть сообщение
Thanks for answering people. Appreciated.



I know I have to use it inside a loop. That was just an example. However, can you tell me in which condition continue; is needed? I kinda didn't understand Mencent's answer.

Also, do I have to use break; to stop every loop? Even while using foreach. No matter if it's a small code or a large one.
Credits to Dice7 for this infinite loop but I hope you understand from this code what would it do
PHP код:
new var = -1;
while(
1//unlimited loop
{
    var++;
    if(var == 
5//if var is 5, then the printf and the "if var =10" check will be skipped
    
{
        continue;
    }
    
printf("%d", var);
    
    if(var == 
10//if var is 10, the loop will break
    
{
        break;
    }

Reply
#7

Hi Sjn,

I agree with most of Twinki's points.

To add:


1. To the best of my knowledge, these compile down to the same thing. It's a matter of readability and what you prefer.


2. I don't think you can use continue like that - it's usually used to skip an iteration in a loop. I'm not sure it would compile like that, and if it did, I'm not sure it would actually do anything.

Additionally, if statements without curly brackets imply that only the next instruction is the only instruction executed if the condition returns true, for example:

PHP код:
some_function() 

    if (
something == 0) continue; 
     
otherthing 0

is the same as

PHP код:
some_function() 

    if (
something == 0)
    {
        continue;
    }
    
otherthing 0
3. I think you can encounter some issues around absurdly large array sizes - ie. compiler crashes or the flow suddenly stopping without explanation or throwing an exception. Try it: if it works, then great.

All in all, I wouldn't be overly concerned with memory usage. With respect to the rest of the community, I think that there's an unhealthy obsession with optimising memory. Nero_3D gives a great explanation here as to how Pawn handles volumes of memory.


4. Once again, this is a matter of preference. Neither is more efficient than the other.

Professionally I've found that companies tend to push nested ifs more over breaking out early as they argue it is more readable.


Edited: code was wrong, idfk where I got that else from
Reply
#8

1-2-4 are already answered well in the topic which pretty much turns to one simple sentence, your own preference, use whichever you like.
However for 3, I would like to give you a little more explanation, a global string for strings you use ? BAD IDEA, Using too much unnecessary cells ? bad idea( There is an example in Godfather script, defining STRING_LENGTH to 256 and using that for everything, it's simply bad)
If you need 1000, it's safe to go for a 1500 cell string, if you need 500, it's alright to have a 800 string size, but using 5000 just because you are lazy to count how many you need is a bad coding habit, and will turn into something bigger later, try to count a string size's max, and always use that( so for example if you need a "%s(%i) has left the server.", This will never go higher than MAX_PLAYER_NAME+1+3(ID)+24 (I usually round it up to closest hundred or 2^x I can tho, so I'm not really great at efficiency, but I still won't use more than 20-30 extra ones.)
Reply
#9

That's neat, thank you all for your kind answers.

Well yeah, I don't really create huge cells. There's only 1 dialog that needs 3000 cell to work, that's why I was asking.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)