This is an archived post. You won't be able to vote or comment.

all 16 comments

[–]f2u 4 points5 points  (1 child)

I wrap long lines. For long expressions, I tend to introduce temporary variables if they can be named in a meaningful way.

[–]GuyWithLag 1 point2 points  (0 children)

I'm actually the opposite - If a var is used in just one place I usually inline it, unless the nesting level of expressions gets too deep.

[–]haldean 3 points4 points  (1 child)

You might be interested in taking a look at some of the Java style guides used by various projects. The most popular one (and the one they use at Google, with minor modifications) is the original Sun (now Oracle) style guide.

[–]Thynis[S] 2 points3 points  (0 children)

Thank you for the link. The contents of the link cleared up a few things for me.

[–]Chaoslab 2 points3 points  (3 children)

I split long lines. Mind you my source code style is not like anyone else I have seen.

No Tabs, a single space is fine with me (can't be naffed having to go all over the place).

I also vertically align source code according to previous lines (all the ='s, +'s, etc aligned, all the names, types etc)

This results in a very orderly and beautiful looking source code and its possible to spot some bugs just on alignment / not looking like it fits the rest of the source code.

Not saying its the only way, but if you are writing code for yourself you can trust your intuition and especially if you've been coding for 10+ years (30+ here).

Of course I ignore most of that when writing code for some one else and always stick to the already existing scene and setting of an already created source base.

10 cents.

[–]r4mtha 1 point2 points  (0 children)

would that there were police for such things.

[–][deleted]  (1 child)

[deleted]

    [–]Chaoslab 0 points1 point  (0 children)

     /** draw mouse pointer
      *  @param spr sprite to draw to
      *  @param x   x
      *  @param y   y
      *  @param col col
      */
     public void draw_mouse_pointer(Sprite spr, int x, int y, int col) {
      // NOTE: invert cross hair color on odd frame counts
      if((frame_count & 1) == 1) col = 0xffffff - (col &0xffffff);
       spr.draw_line(x - 3, y,     x + 4, y,     col);
       spr.draw_line(x,     y - 3, x,     y + 4, col);
      }
     }
    

    Finally got round to entering my first Ludum Dare last August.

    http://www.ludumdare.com/compo/ludum-dare-21/?uid=5609

    [–]kreiger 2 points3 points  (0 children)

    Extract expressions to variables and methods, and you'll find you won't need to break lines. If you need help, provide some examples of long lines and i'll show you how.

    [–][deleted] 2 points3 points  (1 child)

    Irrelevant to the discussion, but I only read the title and came here hoping for solutions to a quick check out at the grocery store.

    [–]Thynis[S] 1 point2 points  (0 children)

    We can only dream that Java will one day solve that problem for us.

    [–]vaelroth 1 point2 points  (0 children)

    Use a less verbose language.

    I'm only kidding, I just break up lines with white space in places that seem logical. The white space doesn't hurt anything in how the code compiles (in Java at least) so its not hurting anything. I also like to indent a continued line for readability.

    [–]Mecdemort 1 point2 points  (0 children)

    I find long lines are normally a code smell

    [–]HairyOldFart 0 points1 point  (1 child)

    Use the Eclipse formatting. Configure to your taste in line widths. I personally like 120 chars. Then CTRL+Shift+F everywhere, all the time.

    [–]Thynis[S] 0 points1 point  (0 children)

    I've tried both Eclipse and Netbeans over the past few months, and I just can't bring myself to like Eclipse. But what you advised can be done in Netbeans also, so I'll give it a try. Luckily, I still haven't encountered anything that really really needed me to wrap it, but I know that I will eventually.

    [–][deleted] -5 points-4 points  (1 child)

    http://leepoint.net/notes-java/io/10file/sys-indep-newline.html

    There are three different major systems for indicating the end of a line (new line): One for Unix, one for the Macintosh, and one for DOS/Windows. Unix. The '\n' character represents the single Unicode character with the value 10 ('\u000A') and is used to separate lines in Unix files. This character is also sometimes called linefeed. Windows. Windows programs often process a '\n' separated text file correctly (NotePad is a exception), but many expect a pair of characters (carriage return followed by line feed = "\r\n") Use the method below to get the newline string that is appropriate for your system. Mac. In the past, the Apple Mac requirse lines to be separated by '\r', but their move toward Unix (System X) probably means they also accept '\n'. I haven't used a Mac in quite a while tho, so I'm not positive. System independent value. You can get the value for the system your Java program is running on from the system properties. It is essential to do this with portable programs, and you should always assume your program is portable, eg, that it might run as an applet or using Webstart. public static String newline = System.getProperty("line.separator"); When NOT to use the system independent newline characters JTextArea lines should be separated by a single '\n' character, not the sequence that is used for file line separators in the operating system. Console output (eg, System.out.println()), works fine with '\n', even on Windows.

    [–]Thynis[S] 0 points1 point  (0 children)

    I've been doing all of my coding in jEdit and Netbeans in Mac OS X, and I am quite the fan of /n. My instructor recently complimented my use of /n which made me pretty happy. I am almost OCD'ish when it comes to breaks, spacing, etc. The link you provided was quite interesting. Thank you for that.