Raised This Month: $ Target: $400
 0% 

Coding issue.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
fezh
Veteran Member
Join Date: Dec 2008
Location: BANNED
Old 07-04-2010 , 16:17   Coding issue.
Reply With Quote #1

I'm wondering why I usually see this coding style instead of the one I used my whole life, example:
What I do:
PHP Code:
public MySuperFuncid 
But instead, I see:
PHP Code:
public MySuperFunc( const id 
Is there any efficiency issue or it's just a readability thing?
__________________
"There is no knowledge, that is not power"
fezh is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 07-04-2010 , 16:22   Re: Coding issue.
Reply With Quote #2

Consistency

But is not really worth it because single cell values are passed by value so, even if you change id by mistake, it won't be a big problem because you are changing it only in the function scope.
__________________
joaquimandrade is offline
Alucard^
AMXX Moderator: Others
Join Date: Sep 2007
Location: Street
Old 07-04-2010 , 17:59   Re: Coding issue.
Reply With Quote #3

I thinked that are used only for strings, i am wrong? sry for doing this question but i didn't understand at all, what joaquim said.
__________________
Approved Plugins - Steam Profile

Public non-terminated projects:
All Admins Menu, HLTV parameters, Subnick,
Second Password (cool style), InfoZone,
Binary C4 plant/defuse, and more...

Private projects:
NoSpec (+menu), NV Surf Management,
PM Adanved System, KZ longjump2, and more...
Alucard^ is offline
Send a message via Skype™ to Alucard^
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 07-04-2010 , 18:02   Re: Coding issue.
Reply With Quote #4

If you use "const id" then you can't do "id = 2" and then use it as 2. Strings are passed by reference.
__________________
fysiks is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 07-04-2010 , 18:37   Re: Coding issue.
Reply With Quote #5

When a single cell is passed to a function it is passed by-value (by default) meaning only the value stored by the variable is sent to the function; the actual memory/variable passed will remain unchanged if the function happens to alter the value. Arrays/strings on the other hand are always passed by-reference meaning the memory address is passed to the function so if the function alters the value, the original array or string that was passed will be changed.

If you understand that clearly you will realize const is only necessary when a var is passed by reference which will prevent the function from altering the actual variable/memory that was passed. As quim said, it does not hurt to use const for the sake of consistency and readability.
__________________

Last edited by Bugsy; 07-04-2010 at 18:49.
Bugsy is offline
Reply



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

Forum Jump


All times are GMT -4. The time now is 07:11.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode