SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 02/09/2019, 12:43 PM   #1
ShadowMortar
Big Clucker
 
ShadowMortar's Avatar
 
Join Date: Jul 2016
Location: Croatia
Posts: 139
Reputation: 20
Default Which is better?

The command is just an example, I'm asking which is better, faster/better... Because once happened to me when I returned a lot of time like in the first example, someone told me not to do that, while others are telling me it's better. I'm confused.
First?
Code:
CMD:hello( playerid, params[ ] ){
	new id, str[ 30 ];
	if( !IsPlayerAdmin( playerid ) ) return SendClientMessage( playerid, -1, "Insufficient privilegies." );
	if( sscanf( params, "i", id ) ) return SendClientMessage( playerid, -1, "Usage: /hello [ID]" );
	if( id == 5 ) return SendClientMessage( playerid, -1, "No" );
	id = 2*id;
	format( str, sizeof( str ), "%d", id );
	SendClientMessage( playerid, -1, str );
	return 1;
}
OR
Second?
Code:
CMD:hello( playerid, params[ ] ){
	new id, str[ 30 ];
	if( !IsPlayerAdmin( playerid ) ) SendClientMessage( playerid, -1, "Insufficient privilegies." );
	else if( sscanf( params, "i", id ) ) SendClientMessage( playerid, -1, "Usage: /hello [ID]" );
	else if( id == 5 ) SendClientMessage( playerid, -1, "No" );
	else{
		id = 2*id;
		format( str, sizeof( str ), "%d", id );
		SendClientMessage( playerid, -1, str );
	}
	return 1;
}
ShadowMortar is offline   Reply With Quote
Old 02/09/2019, 12:47 PM   #2
Walker420
Banned
 
Join Date: Aug 2019
Posts: 4
Reputation: 0
Default Re: Which is better?

Both are good but first one is more efficient to save time when you are writing lots of commands and result will be same of both
Walker420 is offline   Reply With Quote
Old 02/09/2019, 12:51 PM   #3
ShadowMortar
Big Clucker
 
ShadowMortar's Avatar
 
Join Date: Jul 2016
Location: Croatia
Posts: 139
Reputation: 20
Default Re: Which is better?

Quote:
Originally Posted by Walker420 View Post
Both are good but first one is more efficient to save time when you are writing lots of commands and result will be same of both
So what are you trying to say that both examples should work the same, the first one is better if you want to save time?
ShadowMortar is offline   Reply With Quote
Old 02/09/2019, 12:55 PM   #4
Walker420
Banned
 
Join Date: Aug 2019
Posts: 4
Reputation: 0
Default Re: Which is better?

Quote:
Originally Posted by ShadowMortar View Post
So what are you trying to say that both examples should work the same, the first one is better if you want to save time?
Yeah result of both's will be same so you may use your first code to save time
Walker420 is offline   Reply With Quote
Old 02/09/2019, 12:57 PM   #5
ShadowMortar
Big Clucker
 
ShadowMortar's Avatar
 
Join Date: Jul 2016
Location: Croatia
Posts: 139
Reputation: 20
Default Re: Which is better?

Quote:
Originally Posted by Walker420 View Post
Yeah result of both's will be same so you may use your first code to save time
Alright, thanks.
ShadowMortar is offline   Reply With Quote
Old 02/09/2019, 04:56 PM   #6
Markski
Big Clucker
 
Markski's Avatar
 
Join Date: Apr 2015
Location: La Plata, Argentina
Posts: 119
Reputation: 37
Default Re: Which is better?

IMO having a return end a function execution when something is done is a better way of doing things than nesting if's and else's per every individual scenario when possible. It makes code easier to read and mistakes less likely to happen when changing the code in the future. So the first solution, to me, is the best one.
Markski is offline   Reply With Quote
Old 02/09/2019, 05:47 PM   #7
Jonny
Huge Clucker
 
Jonny's Avatar
 
Join Date: Nov 2009
Location: Italy
Posts: 281
Reputation: 38
Default Re: Which is better?

The first one, the second will take more time since it's checking through every else if while if would be immediately considered
__________________
Jonny is offline   Reply With Quote
Old 02/09/2019, 05:55 PM   #8
Markski
Big Clucker
 
Markski's Avatar
 
Join Date: Apr 2015
Location: La Plata, Argentina
Posts: 119
Reputation: 37
Default Re: Which is better?

Keep in mind "longer" in this case is checks that take less than a microsecond (1/1000 of 1/1000 of a second), so don't punish yourself too much trying to aim for the most optimal thing when it comes to simple checks like this. You should only try to optimize things within reason when it comes to functions which involve short timers or long iterators.

For stuff like this, mostly worry about making the code easy to read and understand. In this particular case, that one is also the first one.
Markski is offline   Reply With Quote
Old 03/09/2019, 01:09 AM   #9
CheezIt
Big Clucker
 
CheezIt's Avatar
 
Join Date: Apr 2017
Posts: 181
Reputation: 17
Default Re: Which is better?

Follow your heart and own style, doesn't matter
__________________
I'd like to bring back my old CNR server that was created from scratch & hosted years ago. I'll host & update everything (including the website & forum). You would pay for hosted tab and help manage the community. PM me if you're interested!
CheezIt is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



All times are GMT. The time now is 10:29 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.