Raised This Month: $51 Target: $400
 12% 

SMLIB 0.11 BETA (over 300 Function Stocks) | updated 15.07.2011


Post New Thread Reply   
 
Thread Tools Display Modes
hleV
Veteran Member
Join Date: Mar 2007
Location: Lithuania
Old 08-05-2014 , 07:19   Re: SMLIB 0.11 BETA (over 300 Function Stocks) | updated 15.07.2011
Reply With Quote #361

Quote:
Originally Posted by Powerlord View Post
It's... not like min or max are difficult functions to write...
Exactly, imagine my surprise when I found out SourceMod doesn't have those. Not necessarily SMLib's fault, I just don't get why wouldn't Math_Min()/Math_Max() work the way I expected them to.

Quote:
Originally Posted by Powerlord View Post
PHP Code:
new min b;
new 
max b
I don't think writing up such functions in your plugins (or making an .inc) should be something you need to do. Using inline conditionals instead of functions isn't an option neither because a and b may be something that uses functions for calculation, etc. and thus need to be cached (due to being referenced twice in the line), effectively increasing the amount of lines required to perform a simple min()/max() check.
__________________

Last edited by hleV; 08-05-2014 at 07:23.
hleV is offline
berni
SourceMod Plugin Approver
Join Date: May 2007
Location: Austria
Old 08-05-2014 , 12:35   Re: SMLIB 0.11 BETA (over 300 Function Stocks) | updated 15.07.2011
Reply With Quote #362

I also agree that having functions with good names increases the readability allot, even if it just replaces a 1 line inline-condition.
__________________
Why reinvent the wheel ? Download smlib with over 350 useful functions.

When people ask me "Plz" just because it's shorter than "Please" I feel perfectly justified to answer "No" because it's shorter than "Yes"
powered by Core i7 3770k | 32GB DDR3 1886Mhz | 2x Vertex4 SSD Raid0
berni is offline
friagram
Veteran Member
Join Date: Sep 2012
Location: Silicon Valley
Old 08-07-2014 , 01:03   Re: SMLIB 0.11 BETA (over 300 Function Stocks) | updated 15.07.2011
Reply With Quote #363

The ternary operators are way faster than a function call like using math min or whatever the hell you are trying to do..

You don't even make a new var, you use the operator inline, unless you need to use that value later. A and b is a generic expression. You don't assign crap to them.

myhats > yourhats ? myhats : yourhats

And no, having well named functions to replace simple statements is foolish. It just makes code harder to read for experienced coders, because they have to see what your superfluous function is doing (and they need your library). Also, it's slower.

If you want have to use it a ton, and you don't want to keep duplicating code, use the preproccessor, that's what it's for. It's by far better to do a minmax type deal with the preproccessor than a function call.

I could see an exception here, where you have some storage limitation on like a 32k rom or some other tiny solid state device with small memory space or something, where you would need to save every kb. In that case, duplicating code would be bad. Don't see anything like that ever being a problem here. You wouldn't be using a language like this anyway.
__________________
Profile - Plugins
Add me on steam if you are seeking sp/map/model commissions.

Last edited by friagram; 08-07-2014 at 01:12.
friagram is offline
zeroibis
Veteran Member
Join Date: Jun 2007
Old 08-07-2014 , 03:49   Re: SMLIB 0.11 BETA (over 300 Function Stocks) | updated 15.07.2011
Reply With Quote #364

Yea, I have had to translate 1 line renames a few times and it was always annoying b/c I was just like well you just changed the name of the function and now I got 2 functions with different names that do the same thing.

Readability is about reducing the number of lines of code by creating methods for common operations not by creating a dictionary for the production of Shakespeare.
__________________

Last edited by zeroibis; 08-07-2014 at 03:51.
zeroibis is offline
rhelgeby
Veteran Member
Join Date: Oct 2008
Location: 0x4E6F72776179
Old 08-07-2014 , 05:14   Re: SMLIB 0.11 BETA (over 300 Function Stocks) | updated 15.07.2011
Reply With Quote #365

Avoid doing micro-optimizations if not really necessary. Code readability is important, and it doesn't matter if you're an experienced developer or not.

If you need to check the function's code to understand what it's doing, it's badly named or not doing one thing. Readability is optimal if you can almost read the code as a book. This doesn't mean you should make functions for every "word", but for every logical operation.

The Math_Min and Math_Max should be renamed to have the word "clamp" in them.
__________________
Richard Helgeby

Zombie:Reloaded | PawnUnit | Object Library
(Please don't send private messages for support, they will be ignored. Use the forum.)
rhelgeby is offline
Send a message via MSN to rhelgeby
hleV
Veteran Member
Join Date: Mar 2007
Location: Lithuania
Old 08-07-2014 , 05:58   Re: SMLIB 0.11 BETA (over 300 Function Stocks) | updated 15.07.2011
Reply With Quote #366

Quote:
Originally Posted by friagram View Post
You don't even make a new var, you use the operator inline, unless you need to use that value later. A and b is a generic expression. You don't assign crap to them.

If you want have to use it a ton, and you don't want to keep duplicating code, use the preproccessor, that's what it's for. It's by far better to do a minmax type deal with the preproccessor than a function call.
PHP Code:
#define MIN(%1,%2) ((%1) < (%2) ? (%1) : (%2))

min(new x, new y)
{
    return 
y;
}

new 
GetX() < GetY() ? GetX() : GetY();
new 
MIN(GetX(), GetY());
new 
min(GetX(), GetY());

GetX()
{
    
// Some huge calculation.
}

GetY()
{
    
// Some huge calculation.

GetX() or GetY() will need to be called twice when both manually using ternary operator or a macro, but not when using a function, so either I don't understand you or you're wrong.
__________________

Last edited by hleV; 08-07-2014 at 06:04.
hleV is offline
friagram
Veteran Member
Join Date: Sep 2012
Location: Silicon Valley
Old 08-07-2014 , 06:34   Re: SMLIB 0.11 BETA (over 300 Function Stocks) | updated 15.07.2011
Reply With Quote #367

No

Also, do you think a function call just magically happens?
The subroutine gets called each time, and likely other things happen too in sm
http://en.m.wikipedia.org/wiki/X86_calling_conventions
__________________
Profile - Plugins
Add me on steam if you are seeking sp/map/model commissions.
friagram is offline
hleV
Veteran Member
Join Date: Mar 2007
Location: Lithuania
Old 08-09-2014 , 18:24   Re: SMLIB 0.11 BETA (over 300 Function Stocks) | updated 15.07.2011
Reply With Quote #368

Using the function, GetX() and GetY() will only be called once and stored to x and y variables respectively.
__________________
hleV is offline
berni
SourceMod Plugin Approver
Join Date: May 2007
Location: Austria
Old 08-13-2014 , 07:16   Re: SMLIB 0.11 BETA (over 300 Function Stocks) | updated 15.07.2011
Reply With Quote #369

Things I agree on:
  • Really basic things like getting the lower value of two vars don't necessarily need a function - yeah
  • The functions Math_Min and Math_Max should be renamed - yes, those are not good function names. Function names should always be self-explaining.

Things I don't agree on:
  • Not creating functions because they are slower - The advantage of readability (by far!) outreaches the disadvantage of being slower. As long as you don't do calls to native functions, you should never break your head on things like this. I consider myself a performance-nerd, but what's the sense of having super-optimized code, when nobody can read it? Or even the author himself doesn't know anymore after 1 year.
  • Functions make code harder to read - No, they don't! They only do if you create bad names for them, like "IsPlayerValid" (and doing checks like IsPlayerAlive() inside). A function for 1 line of code is perfectly valid. In theory, a program where every line of code is inside a function is the easiest to read. In practice nobody likes to create that many functions because it's time consuming.

In 10 years of professional programming experience I have learned one thing: Code readability is one of the most important things. Encapsulate your code in functions as much as possible. Write good self-explaining symbol names in camel case. Comment your code.
__________________
Why reinvent the wheel ? Download smlib with over 350 useful functions.

When people ask me "Plz" just because it's shorter than "Please" I feel perfectly justified to answer "No" because it's shorter than "Yes"
powered by Core i7 3770k | 32GB DDR3 1886Mhz | 2x Vertex4 SSD Raid0
berni is offline
rhelgeby
Veteran Member
Join Date: Oct 2008
Location: 0x4E6F72776179
Old 08-13-2014 , 16:01   Re: SMLIB 0.11 BETA (over 300 Function Stocks) | updated 15.07.2011
Reply With Quote #370

Wise words.

There is too much focus on performance. If it really is an issue, we can optimize just that code.
__________________
Richard Helgeby

Zombie:Reloaded | PawnUnit | Object Library
(Please don't send private messages for support, they will be ignored. Use the forum.)
rhelgeby is offline
Send a message via MSN to rhelgeby
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:49.


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