Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Writing code with a lot of spaces

Discussion in 'General Discussion' started by kevint12, Jul 31, 2014.

  1. kevint12

    kevint12

    Joined:
    Jul 12, 2014
    Posts:
    2
    I've been going through a few tutorials recently to enhance my knowledge in Unity, and noticed a few people who write their code in the fashion presented below. It seems those who do it just want their scripts to look bigger by having more spaces. Does it serve any purpose or is it just an annoyance those people enjoy doing?
    using UnityEngine;



    using System.Collections;



    public class XXXXX : MonoBehaviour
    {




    public float X;



    public float XX;


    void Start()

    {


    // Code X



    // Code XX


    }


    }
     
    Meltdown likes this.
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    I would guess it is formatting issues when pasting the code turning a single newline into multiple.
     
  3. Graph

    Graph

    Joined:
    Jun 8, 2014
    Posts:
    153
    it's \r\n being interpreted separately instead of being "read" as a single lineBreak
     
  4. thxfoo

    thxfoo

    Joined:
    Apr 4, 2014
    Posts:
    515
    It's a windows user problem (like Graph said, \r\n vs \n). I use Unix line endings everywhere, Visual Studio has no problems with that.
     
  5. deram_scholzara

    deram_scholzara

    Joined:
    Aug 26, 2005
    Posts:
    1,043
    What you posted looks like issues with copy-pasting or with writing it in different programs (as others have said above).

    I have, however, noticed that some people do write code like this intentionally - usually because they're mainly visual thinkers and thus they like a lot of negative space to help block things out. It's my opinion that writing like that is fine if you're certain you'll be the only person to see your code, but it's inappropriate when writing code that is intended to be shared or which has the possibility of being taken over by somebody else.
     
  6. zDemonhunter99

    zDemonhunter99

    Joined:
    Apr 23, 2014
    Posts:
    478
    You can have as much space between your code as you heart desires. Spacing and commenting will not effect the performance whatsoever.
     
  7. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    It can effect the programmer, however.

    It doesn't really matter how you use your white space as long as it's a) consistent and b) easily readable. Bad use of whitespace can lead to lowered productivity, and that can indirectly lead to inferior code and all of the negatives that may entail.
     
    NomadKing and zDemonhunter99 like this.
  8. AnomalusUndrdog

    AnomalusUndrdog

    Joined:
    Jul 3, 2009
    Posts:
    1,543
    Since we're talking about readability, for me, more than spacing, I find also adding horizontal rules as comments help delineate parts that I intend to be considered separate, as in:
    Code (CSharp):
    1. // =============================================
    2. // initialization part
    3.  
    4. ...code here...
    5.  
    6. // =============================================
    7.  
    8. ...other code here...
     
    Last edited: Aug 2, 2014
  9. CaoMengde777

    CaoMengde777

    Joined:
    Nov 5, 2013
    Posts:
    813
    one thing i HATE!!
    and ive NEVER seen it until i started trying to learn Unity, and ive coded in multiple different languages before..

    is when code is like this {
    --do stuff--​
    }

    it should be like this
    {
    --do stuff--​
    }

    ...
    its infinitely easier to read....
    is it because alot of indie Unity users are noobs? lool..
    But... monodevelop ENFORCES it by default... WTF!!
    every textbook ive ever read about coding does it the way i said should be...

    ive read that it is just another style... but its really Really dumb...
    the WORST part i hate about coding is when you miss those brackets...

    when its written like i had said, you can look thru like 10 codes and see they all have their brackets, and what belongs in what, .. in like 5 seconds..
     
  10. iBinary

    iBinary

    Joined:
    Jul 20, 2014
    Posts:
    5
    Many of the best books out there uses the style you hate. For example, The C Programming Language K&R. In fact the first style is as much used as the second one. I don't get how you are coming at these conclusions.
     
  11. CaoMengde777

    CaoMengde777

    Joined:
    Nov 5, 2013
    Posts:
    813
    experience... i litterally NEVER seen any book use that style... but i havent read ALL the books lol...
    and that style is straight up ANNOYING
     
  12. UnknownProfile

    UnknownProfile

    Joined:
    Jan 17, 2009
    Posts:
    2,311
    That is how I learned to program, even before Unity. Many books do use that style (which I prefer the look of, personally), along with many video lessons and actual programming instructors. When learning C++ that's how I was taught.
     
  13. RockoDyne

    RockoDyne

    Joined:
    Apr 10, 2014
    Posts:
    2,234
    Sounds like you've got issues bro. I don't really care that much about open brackets.

    I rarely, if ever, have issues with open brackets, so not having them on a separate line has ever been a problem. I am a thousand times more worried about where closing brackets are though.
     
  14. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    It's less annoying than the alternatives. Aside from many books, the code examples in the Unity docs use that style.

    --Eric
     
    ippdev likes this.
  15. Sebioff

    Sebioff

    Joined:
    Dec 22, 2013
    Posts:
    218
    It's a matter of taste - I prefer the first style, because if your code is properly formatted you don't hunt down where the braces are anyways. For me it's much quicker to grasp the code structure by looking at the beginning of the lines and the indentation of the blocks. I don't care about the brace, because I KNOW it's there due to the indentation. So it's superfluous to waste an extra line for it.
    I use empty lines for grouping lines of code that logically belong together; a brace on its own line can be mistaken for an empty line on a quick glance, disturbing this structure and making it harder to see which things belong together.
     
    ippdev likes this.
  16. yoonitee

    yoonitee

    Joined:
    Jun 27, 2013
    Posts:
    2,364
    It







    looks







    fine








    to





    me





    ?
     
  17. c-Row

    c-Row

    Joined:
    Nov 10, 2009
    Posts:
    827
    iBinary likes this.
  18. Graph

    Graph

    Joined:
    Jun 8, 2014
    Posts:
    153
    do not worry yourself with such superficial matters, you can ctr k d and the world will be a better place for you again;
    in 16 years you'll be browsing some coding related forum and someOne will be alienated and unfathomably disturbed by the way you format code, and then you'll think "must be projecting something, the good fellow"
    just be glad you didn't learn coding on a vTech laptop with basic :p
     
  19. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    It all depends on what you're used to: each style has its pros and its cons. And if you can read code quickly only if it's done in your style you're in trouble and should read more :p
     
  20. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I agree, I detest it. Luckily I write my own code and don't have to endure it. We all have our own preferences.

    There's no logical reason to favour one or the other, it's functionally the same. For me, I prefer { on it's own line though, personal preference.
     
    landon912 likes this.
  21. tiggus

    tiggus

    Joined:
    Sep 2, 2010
    Posts:
    1,240
    Yep, that's where I first learned it, if it's good enough for K&R it's good enough for me.
     
    iBinary likes this.
  22. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Could you please take care to make it at least consistent and use the same amount of spaces everywhere. Pff, this is ridiculous ;)
     
    c-Row likes this.
  23. GibTreaty

    GibTreaty

    Joined:
    Aug 25, 2010
    Posts:
    792
    I use the first way.
    1. You can compare the first line to what's inside the brackets way faster
    2. Less whitespace for me to wonder off into
    For me, the less line used, the better. Except in this case...
    Code (csharp):
    1.  
    2. bool value = x ? a : y ? b : c;
    3. //or
    4. bool value = x ? a : (y ? b : c);
    5.  
    Now that takes me a few seconds to figure out, even with parenthesis on the second "if...then...else".

    The funny thing is though, when I do change how I structure the code or what the colors of the keywords are, I may or may not be able to decipher things anymore. It usually takes me awhile to get used to the new looks. But I'm happy and content with how I code so I have no reason to change.

    Code (csharp):
    1.  
    2. public class PerfectClass {
    3.     public void SighOfRelief() {
    4.         Thread.Sleep(5000);
    5.     }
    6. }
    7.  
     
  24. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    Why?

    I've changed styles enough that neither bother me any more. I find newline braces slightly easier to read, but it's mostly a style choice. I've never understood the strong hatred some people seem to exhibit towards one style or the other.
     
  25. spraycanmansam

    spraycanmansam

    Joined:
    Nov 22, 2012
    Posts:
    254
    I strongly dislike the same-line bracket. I think code should be in clear blocks, not top-heavy chunks of methods and keywords. But that said, I think code-indentation is a lot more important than which line the bracket is on.. In my own experience I've found that whenever I've read code and there's been incorrect indentation, there'll be a bracket on a same line somewhere! :p

    I know this was tongue-in-cheek, but I have to say that when I read nicely formatted code it becomes more shape recognition than reading. When I'm reading other peoples code with a different format, I find I actually have to 'read' the code. Kind of like trying to read doctors writing... sure it's legitimate english, but you can't read it anywhere near as fast! :p


    I think like a lot of things, coding is an art form, everyone can do it.. with different levels and personal tastes.
     
  26. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    Haha, but of course - inconsisntent code is... inconsistent! Plus, the opposite is also true: when the formatting is inconsistent there's usually some braces on a new line!

    So really it's not too useful to associate inconsistent formatting with the same line braces.
     
    spraycanmansam likes this.
  27. spraycanmansam

    spraycanmansam

    Joined:
    Nov 22, 2012
    Posts:
    254
    I associate the devil with same-line braces :p
     
  28. Bladesfist

    Bladesfist

    Joined:
    Jan 3, 2012
    Posts:
    107
    The same line bracketing is one of the many terrible things you will find in the Mono coding conventions. Seriously the thing seems to be a document of all the style choices I dislike. 8 space tabs! A space between method name and open parenthesis, use of Var whenever you are writing the type twice. You can find the document here: http://www.mono-project.com/Coding_Guidelines
     
  29. Sebioff

    Sebioff

    Joined:
    Dec 22, 2013
    Posts:
    218
    Yeah, if there's something like a coding style I "hate" it is using spaces for indentation instead of tabs. That's just pointless.
     
  30. yoonitee

    yoonitee

    Joined:
    Jun 27, 2013
    Posts:
    2,364
    I think when you get to the point that you're getting annoyed by braces... }-o_O-{ ...it's time to have a holiday.
     
  31. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    I've invented a new coding style, where the braces are separated by whitespace to make them really stand out, and are aligned to the end of the longest line.

    Code (javascript):
    1.  
    2. function Foo ()
    3.  
    4.                     {
    5.  
    6.     // code here
    7.     // more code here
    8.  
    9.                     }
    This is clearly superior because of reasons, and I will have to insist that everybody adopt this style immediately. Thank you.

    P.S. I wrote to Microsoft to demand that they make Visual Studio handle this style automatically, and they said they'd get right on it. Well, actually they said "totally", which I assume means they will totally get right on it. Well, actually they said "toad", which I assume is some kind of internet slang for "totally". You know, like "toadally" or something. Hence, "toad". Well, actually actually they said "foad", but I assume that's a typo and they meant to type "toad", since "foad" doesn't appear to be a word. But they did seem enthusiastic about it, given their use of all-caps.

    --Eric
     
    giyomu, iBinary, carking1996 and 2 others like this.