Results 1 to 11 of 11
Like Tree4Likes
  • 1 Post By SurfMan
  • 1 Post By JosAH
  • 1 Post By Russd772
  • 1 Post By pbrockway2

Thread: Good places to find highly commented sample code for retyping

  1. #1
    chaucer345 is offline Member
    Join Date
    Dec 2012
    Posts
    44
    Rep Power
    0

    Default Good places to find highly commented sample code for retyping

    Hello all.

    So, I've been trying to learn java for a while now and I've found that I learn my best when I retype highly commented sample code. What I mean by this is code such as:

    Java Code:
    //Create a regular text field.
            JTextField textField = new JTextField(10);
            textField.setActionCommand(textFieldString);
            textField.addActionListener(this);
     
            //Create a password field.
            JPasswordField passwordField = new JPasswordField(10);
            passwordField.setActionCommand(passwordFieldString);
            passwordField.addActionListener(this);
     
            //Create a formatted text field.
            JFormattedTextField ftf = new JFormattedTextField(
                    java.util.Calendar.getInstance().getTime());
            ftf.setActionCommand(textFieldString);
            ftf.addActionListener(this);
    Where the comments give you an idea of what the methods you're looking at actually do and what the code is trying to accomplish. For me, ideal code for retyping and compiling would have as dense and detailed comments as possible, but as a relatively new programmer I really have no idea where to find such code. Can anyone suggest good locations to find code such as this aside from the Java tutorials (http://docs.oracle.com/javase/tutorial/)? I would be really grateful for any suggestions.

    EDIT:

    Note, I realize that most of the time comments are used in code to explain errors that need to be fixed later or describe some unusual programming technique being employed, but that's not the type of commented code I'm looking for. Figuring out the logic of how an application progresses is very important, but to understand what's going on you need a basic idea of what the words mean. So for example code like this:

    Java Code:
    // this declares a field which is an integer called "numberOne"
    int numberOne;
    teaches you more about what the words mean than code like this:

    Java Code:
    int numberOne;
    retyping stuff like the first int numberOne example helps me remember that that is how one declares an integer. It's sort of like writing out a word in a foreign language over so you can remember it's definition. Writing:

    amarillo
    amarillo
    amarillo...

    over and over will not help you memorize that amarillo is Spanish for yellow, but typing:

    amarillo = yellow
    amarillo = yellow
    amarillo = yellow....

    Will help you remember. And I know there are lots of important things other than the definitions of words you need to figure out the logic of either a sentence in Spanish, or a line of code, but you're still up a creek if you don't know the words.

    So that's why I'm looking for highly commented code. I hope that clarifies things.
    Last edited by chaucer345; 01-03-2013 at 05:00 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default Re: Good places to find highly commented sample code for retyping

    Sorry for disappointing you but there are typing tutors for that purpose; programming should have as little to do with typing as possible (although one could think otherwise given all those geeks glued to their keyboard). Programming is about proper design so that you can type as little as possible afterwards. Start designing your own (small) classes and play with them a little bit (in your head or on paper); if they do what you want and they do it elegantly only then start that stupid activity: type them in and (optionally) comment them; that should be a boring task because you allready did all the thinking ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    927
    Rep Power
    2

    Default Re: Good places to find highly commented sample code for retyping

    I agree with Jos above. Besides, I will immediately delete comments from code like the ones below. Every single comment in the example below is bullsh1t:
    Java Code:
    //create a string with my name
    String myname = "SurfMan";
    //set the age
    int age = 40;
    //increment the age by one
    age++;
    //Create a JFrame so that we can see the application on screen on view something
    JFrame frame = new JFrame();
    This makes an interesting read: Coding Horror: Code Tells You How, Comments Tell You Why
    A brilliant comment:

    Sometimes when I'm writing code, I think "I should comment this".
    Right. The first and proper response to that feeling is to *roll up your sleeves and refactor the code*. After you've refactored, if you still feel a comment is necessary, then by all means add one. Never comment before you've attempted to refactor away the comment first.
    Semi-offtopic: for a good laugh, read these: polls - What is the best comment in source code you have ever encountered? - Stack Overflow
    Last edited by SurfMan; 01-03-2013 at 12:11 PM.
    Fubarable likes this.

  4. #4
    chaucer345 is offline Member
    Join Date
    Dec 2012
    Posts
    44
    Rep Power
    0

    Default Re: Good places to find highly commented sample code for retyping

    Quote Originally Posted by JosAH View Post
    Sorry for disappointing you but there are typing tutors for that purpose; programming should have as little to do with typing as possible (although one could think otherwise given all those geeks glued to their keyboard). Programming is about proper design so that you can type as little as possible afterwards. Start designing your own (small) classes and play with them a little bit (in your head or on paper); if they do what you want and they do it elegantly only then start that stupid activity: type them in and (optionally) comment them; that should be a boring task because you allready did all the thinking ...

    kind regards,

    Jos
    Perhaps I was unclear about what I was hoping to accomplish by retyping source code. I've edited the top post for clarity.

  5. #5
    Russd772 is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: Good places to find highly commented sample code for retyping

    have you looked at the java tutorials?
    The Java™ Tutorials

    if i remeber correctly they have example code which is commented specifically for beginners

    hope this help
    Russ

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

    Default Re: Good places to find highly commented sample code for retyping

    Quote Originally Posted by chaucer345 View Post
    Java Code:
    // this declares a field which is an integer called "numberOne"
    int numberOne;
    teaches you more about what the words mean than code like this: [ ... ]
    [/code]
    Whoop dee doo; anyone who doesn't understand a bare 'int numberOne' can't program in Java, nor C, nor C++, nor C#; other people don't need comments like that.

    kind regards.

    Jos
    Sierra likes this.
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Russd772 is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: Good places to find highly commented sample code for retyping

    im not sure why everyone is being negative to this person about this post, they not breaking any of the forum rules or being rude or condecending to anyone so if you dont have the answer why be rude to him? they just stated how they learns best, and that is to comment what their code does, not implying that they would code like this for production purposes. this looks more like trolling to me than a helpfull forum.

    and he never stated he did not know what an int was, this was an example of what he is looking for!

    Russ
    foulkelore likes this.

  8. #8
    chaucer345 is offline Member
    Join Date
    Dec 2012
    Posts
    44
    Rep Power
    0

    Default Re: Good places to find highly commented sample code for retyping

    Quote Originally Posted by Russd772 View Post
    im not sure why everyone is being negative to this person about this post, they not breaking any of the forum rules or being rude or condecending to anyone so if you dont have the answer why be rude to him? they just stated how they learns best, and that is to comment what their code does, not implying that they would code like this for production purposes. this looks more like trolling to me than a helpfull forum.

    and he never stated he did not know what an int was, this was an example of what he is looking for!

    Russ
    Thank you.

  9. #9
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Good places to find highly commented sample code for retyping

    Well many of the more experienced ones here have seen over-commented code I guess. To know how to comment correctly is another thing beside debugging and using design patterns and all the other important aspects of coding ...
    I like likes!

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Good places to find highly commented sample code for retyping

    Babies and infants learn natural language through an amazing process whereby they are able to detect "significant" elements among the barrage of sound they experience. (This has been tested by subjecting the hapless experimental subjects to computer generated sounds and observing that they later show enhanced brain activity when hearing sounds which occur at a slightly higher frequency - the difference in frequency of occurrence is not detectable by an adult listener.) Meaning develops as they associate the "significant" sounds with significant interactions with the external world. The infants brain actually rewires itself in response to all this, not becoming stable until language has been acquired.

    Contrast this with adults who learn languages (computer and natural) using "comments" (including parses of linguistic constructs according to grammatical rules) as cribs. There is no doubt the infant's approach is more effective but, since the brain doesn't remain plastic, it isn't an option once the "mother" language(s) is acquired. As you learn a computer language you will resort to comments for all sorts of reasons: describing, explicitly, the steps of an algorithm, noting the significance of method call arguments, cross referencing with notes or assignments, noting things you are gong to do later, and things you simply don't understand but which wrok, linking to urls describing best practice or usage examples, etc.

    It's your code, and your comments: do what you like. But don't expect experienced practitioners to have the same concerns and, therefore, don't expect them to comment in the same way or appreciate your comments if they are presented in the public arena. (For a natural language analogue you might puzzle over "asociara" and eventually determine it's an imperfect subjunctive. However it's quite likely a native speaker has no clue what an imperfect subjunctive is.)

    ---

    One way in which computer languages attempt to cope with the extreme difficulty people have in language acquisition is to make the grammar really simple. Whenever you see "SomeType someName= new SomeType()" then a new some type called some name is being created. The simplicity means that it not really comment worthy. Another trick of good code is to choose names so that the very name is a comment of sorts.

    Java Code:
    //Create a formatted text field.
    JFormattedTextField ftf = new JFormattedTextField(
            java.util.Calendar.getInstance().getTime());
    Well it's a new JFormattedTextField so, of course a new formatted text field is being created. But that ftf? That's crying out for a comment. Instead of //... consider that a descriptive variable name is, itself a useful comment.

    Java Code:
    JFormattedTextField startTimeFTF = new JFormattedTextField(
            java.util.Calendar.getInstance().getTime());
    To choose another example - that of describing the significance of arguments in a method call - that's fine if you intend referring to the code later to prepare for an exam or whatever. But with so many methods, and so many arguments, that isn't going to "scale". Better to have the API docs available and refer to them whenever you are unsure.
    foulkelore likes this.

  11. #11
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    927
    Rep Power
    2

    Default Re: Good places to find highly commented sample code for retyping

    Quote Originally Posted by Russd772 View Post
    im not sure why everyone is being negative to this person about this post, they not breaking any of the forum rules or being rude or condecending to anyone so if you dont have the answer why be rude to him? they just stated how they learns best, and that is to comment what their code does, not implying that they would code like this for production purposes. this looks more like trolling to me than a helpfull forum.

    and he never stated he did not know what an int was, this was an example of what he is looking for!

    Russ
    Nobody has said a negative word or has been rude. These are professional coders that are speaking, and from their experience, these kinds of comments are unneccessary and only add noise. What he has been told is that commenting //this is an int that holds my age and is set to 30 then having a statement int myAge = 30 is completely useless. Having said that, this type of comment only makes sense if you write code like this:
    Java Code:
    //this is an int that holds my age and is set to 30
    int sajhdgkuasdasdbsaytsf = Math.floor((Math.pi() * 9.54) + 0.5);
    But then you have to ask yourself, should I refactor the variable name or should I add a comment explaining this mess?

Similar Threads

  1. (Help) Art and Science of Java Sample Code
    By TechTech in forum Eclipse
    Replies: 7
    Last Post: 07-23-2012, 06:37 PM
  2. Trying to find good resources for learning Agile.
    By StephenHawking9 in forum Forum Lobby
    Replies: 1
    Last Post: 06-22-2012, 04:09 AM
  3. sample crud operation code
    By java1 in forum Advanced Java
    Replies: 3
    Last Post: 11-18-2011, 12:15 PM
  4. Replies: 1
    Last Post: 03-04-2010, 11:19 AM
  5. Sample Code to Scan an Image
    By LankanSniper in forum Advanced Java
    Replies: 0
    Last Post: 03-24-2009, 02:54 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
  •