SA-MP Forums

Go Back   SA-MP Forums > Other > Everything and Nothing

Reply
 
Thread Tools Display Modes
Old 24/03/2017, 02:20 PM   #31
nggroup247
Little Clucker
 
Join Date: Mar 2017
Posts: 6
Reputation: 0
Default Re: Why do people do this?

Quote:
Originally Posted by Arithmetic View Post
Shut the fuck up you son of a bitch piece of shit motheawrfucker KYS fucking KYS or ill fucking kys you my self you faggot motherfucker what the fuck are you looking at
Calm down my boy
nggroup247 is offline   Reply With Quote
Old 24/03/2017, 07:19 PM   #32
Y_Less
Spam Machine
 
Y_Less's Avatar
 
Join Date: Jun 2008
Location: 629 - git.io/Y
Posts: 14,509
Reputation: 2850
Default Re: Why do people do this?

Since you mentioned YSI, I'll tell you why I do it that way. Say you want to declare five variables, there are three main ways to do it (ignoring semi-colon placement):

PHP Code:
main()
{
    new 
var1var2 42var3[6], var4[32] = "hello"var5[] = { 012345678};
    
printf("hi");

PHP Code:
main()
{
    new 
var1;
    new 
var2 42;
    new 
var3[6];
    new 
var4[32] = "hello";
    new 
var5[] = { 012345678};
    
printf("hi");

PHP Code:
main()
{
    new
        
var1,
        
var2 42,
        
var3[6],
        
var4[32] = "hello",
        
var5[] = { 012345678};
    
printf("hi"); 
Version 1 is very messy - it is hard at a glance to see how many variables there are, what their values are, etc.

Version 2 is much clearer, but quite verbose - repeating "new" many times.

Version 3 is in my experience uncommon in other languages, but to me has the clarity of version 2 (assuming your indentation is consistent) without the verbosity. Some people like to put the first variable on the same line as "new", but that assumes that they are using "new" and have a tab size of 4 (the default, but not required). This leads to inconsistencies when other people view your code with different tab settings or you change the scope:

OK:

PHP Code:
main()
{
    new 
var1,
        
var2 42;

Inconsistent:

PHP Code:
main()
{
  new 
var1,
    
var2 42;

Inconsistent:

PHP Code:
main()
{
        new 
var1,
                
var2 42;

Inconsistent:

PHP Code:
main()
{
    static 
var1,
        
var2 42;

Inconsistent:

PHP Code:
main()
{
    static const 
var1,
        
var2 42;

Without putting the first variable on the same line, you get:

OK:

PHP Code:
main()
{
    new
        
var1,
        
var2 42;

OK:

PHP Code:
main()
{
  new
    
var1,
    
var2 42;

OK:

PHP Code:
main()
{
        new
                
var1,
                
var2 42;

OK:

PHP Code:
main()
{
    static
        
var1,
        
var2 42;

OK:

PHP Code:
main()
{
    static const
        
var1,
        
var2 42;

The variables' alignments become independent of the length of the declaration. This becomes even clearer in something like C++:

PHP Code:
std::vector<unsigned int *, memory::StaticPoolAllocator<sizeof (unsigned int *), 1000> >
    
listOfIntPointers1,
    
listOfIntPointers2
Instead of:

PHP Code:
std::vector<unsigned int *, memory::StaticPoolAllocator<sizeof (unsigned int *), 1000> > listOfIntPointers1listOfIntPointers2
Or:

PHP Code:
std::vector<unsigned int *, memory::StaticPoolAllocator<sizeof (unsigned int *), 1000> > listOfIntPointers1;
std::vector<unsigned int *, memory::StaticPoolAllocator<sizeof (unsigned int *), 1000> > listOfIntPointers2
Or:

PHP Code:
std::vector<unsigned int *, memory::StaticPoolAllocator<sizeof (unsigned int *), 1000> > listOfIntPointers1,
    
listOfIntPointers2
To me, the benefits of having variables on a new line relative to their declarations are huge in terms of clarity. Some people go one step further and line up all tags and values as well:

PHP Code:
main()
{
    new
                
var1,
        
Float myLongVar2 42,
                
var3       50,
                
var4[32]   = "hello",
        
bool  var5[]     = { truefalsefalsefalsetrue };
    
printf("hi"); 
That will still correctly indent regardless of tab settings if you use tabs for indentation (up to the edge of the tags) and spaces for alignment. With IDE support, that would be quite nice, but otherwise I find maintaining the spacings too tedious to justify the clarity.

So to your original question - why do it for single variables? Consistency. I admit that for just one variable it can look a little odd, but I don't mind a little oddity at one variable for the huge benefits on multiple variables. When writing code, sometimes I don't know if I will need one or more variables declaring; if I declare a single variable in one style then add a second, I have to go back and edit the style of the first instead of just appending. If I don't know how many variables I need, and had different styles depending on the number, then that would also actually slow me down - type "new" then stop and think, do I need to press enter now or not? Instead, type new, hit enter without even thinking, automatically indent to the right place, add the first variable, and by then I might know if I need a second or not, but it doesn't matter either way. Plus, as I said, consistency - if all the variables are on a new line, they all look the same and it becomes easier to parse code quickly; variables are (usually) a very short line followed by an indented block of text - that is a nice general pattern that is easy to spot from a distance without having to read too closely. Putting a single variable on one line, while surrounded by other code, just becomes another long line of text in a load of long lines of text. Yes, syntax highlighting helps, but so does this for the same reasons - pattern matching is faster than reading.
Y_Less is offline   Reply With Quote
Old 24/03/2017, 07:36 PM   #33
Spydro
Big Clucker
 
Spydro's Avatar
 
Join Date: Oct 2015
Location: United Kingdom
Posts: 131
Reputation: 15
Default Re: Why do people do this?

Quote:
Originally Posted by Y_Less View Post
-snip-
He lives.
Spydro is offline   Reply With Quote
Old 24/03/2017, 08:25 PM   #34
Troydere
Gangsta
 
Troydere's Avatar
 
Join Date: Feb 2017
Location: Venezuela
Posts: 644
Reputation: 117
Default Re: Why do people do this?

Quote:
Originally Posted by Y_Less View Post
So to your original question - why do it for single variables? Consistency. I admit that for just one variable it can look a little odd, but I don't mind a little oddity at one variable for the huge benefits on multiple variables. When writing code, sometimes I don't know if I will need one or more variables declaring; if I declare a single variable in one style then add a second, I have to go back and edit the style of the first instead of just appending. If I don't know how many variables I need, and had different styles depending on the number, then that would also actually slow me down - type "new" then stop and think, do I need to press enter now or not? Instead, type new, hit enter without even thinking, automatically indent to the right place, add the first variable, and by then I might know if I need a second or not, but it doesn't matter either way. Plus, as I said, consistency - if all the variables are on a new line, they all look the same and it becomes easier to parse code quickly; variables are (usually) a very short line followed by an indented block of text - that is a nice general pattern that is easy to spot from a distance without having to read too closely. Putting a single variable on one line, while surrounded by other code, just becomes another long line of text in a load of long lines of text. Yes, syntax highlighting helps, but so does this for the same reasons - pattern matching is faster than reading.
That's a valid reason. However, as you stated they do still look odd, even if they follow the same pattern than multiple lines/declarations.

This is from your y_playerset.inc file (which at this point i don't know who wrote it)

PHP Code:
...
        ++
__ps_pointer;
        new
            
begin __ps_data[e_PLAYER_SET_HACK_DATA][0];
        
// Is this a single value element (group or ID).
        
if (_:addr == begin)
... 
It looks weird, completely out of place and disturbing in my opinion.

This is the same file few lines below
PHP Code:
...
    static
        
sFake[MAX_PLAYERS] = {(INVALID_PLAYER_ID << 8) | 0x0F0000F0};
    new
        
ret = (__ps_data[e_PLAYER_SET_HACK_DATA] = sFake),
        
e_PLAYER_SET_HACK:e_PLAYER_SET_HACK:0;
    foreach (new 
playerid Player)
... 
Although there is a 'new line single declaration', it is followed by a multiple line-declaration pattern which makes it look good, or not too bad. That does backup what you explained above.

However, in the first example it does look not only not good but bad, because it is the only declaration inside that function, there's no pattern to follow there or a near structure of which you can compare them, so it just does look bad.
Troydere is offline   Reply With Quote
Old 06/04/2017, 12:04 PM   #35
Runn3R
Huge Clucker
 
Runn3R's Avatar
 
Join Date: Dec 2013
Location: Croatia, Split
Posts: 378
Reputation: 35
Default Re: Why do people do this?

I was a person like you before but when i saw y_less's and emmets code i decided to switch to that hence it looks more cleaner and easier to read.
__________________
SA-MP 0.3.8 release date == Half Life 3 release date
Runn3R 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


Similar Threads
Thread Thread Starter Forum Replies Last Post
for all you people who -rep pepper Everything and Nothing 13 05/02/2013 01:53 AM
Why some people lost all their things when login and some people not? CTCCoco Help Archive 1 07/07/2011 10:05 AM
SA-MP people plz help me! KILLER190 Client Support 5 29/10/2010 09:35 PM
People Scaming People's Severs coutanche7 General 19 28/12/2008 04:06 PM


All times are GMT. The time now is 04:44 PM.


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