Results 1 to 12 of 12
  1. #1
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default Writing easy to follow code...

    Hi!

    Are there any "best practices " way of writing Java code for it to be easy to manage?

    I tried to keep my code clean by having one class with only methods, and another class with just the form then another class with just the main() but ended up writing a lot of methods right in the form class

    once I started adding different threads from two other classes it got even more confusing... and quite honestly I would be ashamed to share my code with anyone because its such a mess.

    Any tips on writing easy to understand code? For example if I am going to be working in a team... whats the best practices so that we can all understand what the other person has done in a clean format?

    Thanks!


    (I know this is not a real "java help" topic, but this I think is a very important newbie question... and I am a total newb)

  2. #2
    Join Date
    Jan 2011
    Posts
    13
    Rep Power
    0

    Default

    what your looking for is Java conventions!

  3. #3
    codesmuggler is offline Member
    Join Date
    Feb 2011
    Posts
    30
    Rep Power
    0

    Default

    Show the example of your code and the community will try to help you.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I believe that writing clean, easy to understand code should come with experience. Read some books on design and good java/programming techniques, with time you will get more comfortable with what you want and how you want to do it and through this your code should start becoming much cleaner and more organized. A new programmer Id imagine will generally have pretty messy, hard to understand code.

  5. #5
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    what your looking for is Java conventions!
    Thanks, will Google ;)
    Show the example of your code and the community will try to help you.
    I usually do that but its still shameful when you know others are looking at your messy code :D
    It's strange, even though I am a big fan of open source and will willingly give away my code for free most of the time - code still feels quite private! (Especially dirty written code, like an old dirty secret!)

    Read some books on design and good java/programming techniques,
    Recommendations?
    with time you will get more comfortable with what you want and how you want to do it
    I'm moderately happy with my code because *I* can understand and follow it, but if I were to be in a team I can understand that others wont be able to follow it as easy as me.
    Coming from a non OOP background I am used to messy code, but I appreciate the neatness of OOP.
    A new programmer Id imagine will generally have pretty messy, hard to understand code.
    I'm not a new programmer, I have been coding for years - just not Java.
    I learned Java over a decade back, forgot it, and am learning it again. But have programmed for years in PHP (non OOP) and other scripting languages.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    I'd say the core of your question lies in the title: "easy to follow code". What follows what or who? I'd say the code follows an idea, a design. The design should be clear and easy to follow (by reading the code?) If the design is a messy, muddy pile of spaghetti the code that follows that design is also messy and muddy etc. In short: always design what you want your code to do and design how it should do it. As a corollary: never start banging your keyboard and spit out code when your design is still not clear (even to you). You might end up in endless debugging sessions and a lot of hacking; never be proud of that.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I dont have any books to suggest, but Id imagine some books on design patterns would be helpful, this way before you code you can have a pattern chosen and stick to it. I am about to get head first design patterns and head first software development, I am sure there are a lot of other good books, try googling for design patterns, software development, good habits in java, and other similar things.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    And here's a bit of fun to read.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    And here's a bit of fun to read.

    kind regards,

    Jos
    Haha! I felt totally nerdy for enjoying to as much as I did :D

    Quite a few good tips if you do exactly the _opposite_ of what he tells you, and some of those lines seem like good advise if you are starting out in programming but brings back some fond memories and laughs if you have been programming for a while.

    Thanks for that link!

    A little on topic:
    Debuggers, I am using Eclipse and so far have not touched it's debugger (only saw it in the menu) - any good online video tut you recommend to learn/understand it?

  10. #10
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    The eclipse debugger isn't difficult, I learned to use it by myself.

    First in the upper right there should be 2 buttons: one with "java" on it and the other one with "debug" on it. Click one the one with debug. This opens the debug perspective.

    Debugging works as follows: it gives you the oppertunity to halt the program at any point in your code. Then it gives you the oppertunity to look at the states of all the variabeles at that point. First you need to find a place you want your program to halt. Then double click just left of that line of code or right click and then click "toggle breakpoint". A blue dot should appear. Now run the code, not by clicking regular button, but the button with a bug on it just left. This runs your program as usual, but when it reaches the line of code with the blue dot, the program halts.

    Now there are 2 tabs that are important. One is a tab called "debug", it is probably somewhere near the console. This tab shows you which threads are running, gives you the option to resume the program or to step to the next line of code. Then there is the tab called variabeles which shows you which variabeles there are availebel at that point and what their state is.

  11. #11
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    @imorio,
    Thanks for the explanation, will try to follow along on what you said in my program.

  12. #12
    Jojomofoman is offline Member
    Join Date
    Dec 2010
    Posts
    21
    Rep Power
    0

    Default

    Design patterns definitely helped me quite a lot, such a pattern as MVC helps keep everything seperated making it easier to come back to.

    Another way to help keep your code easier to follow is consistent naming conventions.

Similar Threads

  1. help writing a java code
    By joshsmith3400 in forum Forum Lobby
    Replies: 1
    Last Post: 02-02-2011, 07:06 AM
  2. Writing code seamlessly...
    By JoshNerd557 in forum New To Java
    Replies: 2
    Last Post: 12-07-2010, 01:51 AM
  3. Is this the right way of writing html code in java??
    By raju.i in forum Advanced Java
    Replies: 1
    Last Post: 05-18-2010, 08:05 AM
  4. What's an easy way to test this code?
    By twiggy62 in forum New To Java
    Replies: 2
    Last Post: 02-10-2010, 09:41 AM
  5. Replies: 1
    Last Post: 11-07-2008, 05:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •