Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Code Styling Debate: DOs and DON'Ts (https://forums.graalonline.com/forums/showthread.php?t=134265283)

Crow 12-16-2011 01:16 PM

Code Styling Debate: DOs and DON'Ts
 
So, browsing the scripting forums, this post got me thinking:
Quote:

Originally Posted by cbk1994 (Post 1678116)
It's only necessary to use temp the first time, but I'd highly recommend using it throughout.

When are things acceptable, and when are they not? I know that many code styling related topics are completely based on preference. Nevertheless, I'd like to talk about your general preferences, what is acceptable to you and what is not.

Let's start off with the temp prefix Chris was talking about. I mostly agree with always using it, but I sometimes don't do it in generally long lines/function calls because I believe it destroys readability.

What are your thoughts on this?

cbk1994 12-16-2011 01:42 PM

I used to only use it the first time, and eventually transitioned to always using it since it gets rid of a lot of ambiguity (is it a global variable or a temp variable?). I don't really see how it kills readability at all.

Crow 12-16-2011 04:06 PM

As I said, I almost always do it that way as well. But when I'm doing some math in a function call, lines tend to get long, so I leave it out there.

fowlplay4 12-16-2011 04:15 PM

I do it in fear of the variable getting hijacked by a global version, that same fear is also why I would using avoid player attributes/keywords when naming variables.

Crono 12-16-2011 04:39 PM

i like to use as many colors as possible so my coding is fun and easy to look at!! xoxo

Crow 12-16-2011 04:50 PM

Quote:

Originally Posted by fowlplay4 (Post 1678136)
I do it in fear of the variable getting hijacked by a global version, that same fear is also why I would using avoid player attributes/keywords when naming variables.

I always use different variable names, no matter the scope. If a name is being used globally, I won't be using it in any other scope, and so on.

TSAdmin 12-16-2011 04:50 PM

I tend to be consistent throughout the entire script. If I create something with a prefix, the prefix will appear every single time unless it explicitly requires dropping, but that's never been something I was forced into doing, if there's an existing reason to force the dropping of a prefix at all.

Skyld 12-16-2011 05:14 PM

I always include the relevant prefix (i.e. temp or player prefix; even player.clientr.foo) etc, because that way there is no ambiguity. It is good practice to do that, not least because people do not have to guess as much when reading your code, but it prevents confusion between scopes (i.e. is your temp variable actually a global variable in some unusual circumstances?). It forces you to think about the scope you are dealing with, leaving less chance of conflicts occurring.

ffcmike 12-16-2011 06:55 PM

Quote:

Originally Posted by Skyld (Post 1678150)
I always include the relevant prefix (i.e. temp or player prefix; even player.clientr.foo) etc

Same for me aswell as the this. prefix for functions.

It would however be interesting to know if this has any bearing on script time, albeit the difference would probably be negligible.
On a similar note I compared script time between the use of the this. or player. prefix in classes joined to the player, if my results were accurate then it was always faster using this. as opposed to player. with variables and functions Serverside, however Clientside only with variables was the use of the this. prefix faster. And yes the difference was tiny.

Tolnaftate2004 12-16-2011 07:21 PM

Quote:

Originally Posted by Crow (Post 1678118)
Let's start off with the temp prefix Chris was talking about. I mostly agree with always using it, but I sometimes don't do it in generally long lines/function calls because I believe it destroys readability.

What are your thoughts on this?

As long as functions cannot be closed over, I would always use the temp prefix because it clearly defines the lookup chain.

This is especially true of code that uses a with block or GUI controls just because a lot of people like to drop the this prefix in those cases, so it actually becomes something of a burden for the reader.

However, removing the temp prefix has no ramifications as far as efficiency is concerned (save for 5 keystrokes).

e:

Quote:

Originally Posted by ffcmike (Post 1678163)
On a similar note I compared script time between the use of the this. or player. prefix in classes joined to the player, if my results were accurate then it was always faster using this. as opposed to player. with variables and functions Serverside, however Clientside only with variables was the use of the this. prefix faster. And yes the difference was tiny.

Could you post the code you used to test? This really seems unbelievable to me.


All times are GMT +2. The time now is 01:51 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.