• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

What are some things to keep in mind when coding as a beginner?

 
Ranch Hand
Posts: 38
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My Question is as stated in the subject, i often find myself repeating certain types of mistakes(like over complicating code trying to fit new things i learn) or coding in ways that are not the 'standard practice'. Id like to know what are the general things i should avoid doing and what should i keep in mind when starting a new program? Im 18 going to uni for compsci so im hoping to make a career out of programming and dont want to be hung up on these mistakes.
 
Saloon Keeper
Posts: 8243
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
NAMING
  • Naming things is incredibly important, especially for the reader, which will mostly be you.
  • Follow naming conventions.
  • In Java that means that variables and methods start with a lower case letter, constants are in all caps, and classes start with an upper case letter.
  • Don't use underscores (_) except in constants, use camel-case instead.
  • Be mindful of using plural vs singular names.
  • Field names are usually nouns and method names are verbs or verb phrases.
  • Method names should describe clearly what the method does and then the method shouldn't muddy the waters by doing lots of other things as well.
  • Be precise. Don't use "array" when it's a "List" and  vice versa. Note: An ArrayList is-a List.
  • Avoid abbreviations unless blatantly obvious to any reader.
  •  
    Marshal
    Posts: 26626
    81
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Don't write a hundred lines of code before testing it to see if it works. It won't, of course, and then you'll find it's hard to locate the problem. This is especially true for beginners but it's still true for experienced programmers.

    Instead, you should be able to run and test your code at almost any point. Sure, the tests you apply to a partially-written program aren't going to be the same tests you apply when the program is written, but there should still be tests.

    Here's an example. Suppose you have to read the lines from a text file and do some complicated processing on them to analyze the contents of those lines. So you should start by writing the code which reads the lines from the text files. Then write something to make sure you read all of the lines -- your code might not be able to read any of the lines, or it might miss the last line, or it might miss out empty lines when the presence of an empty line is significant, etc. You could just write the total number of lines to the console, or you could copy all of the lines to the console, or whatever it takes to make sure your text-file-reading code is working the way you want it to work. Only after that should you write the code to process the data from the text file.
     
    Marshal
    Posts: 73021
    330
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    As well as naming things, make sure your code is correctly and consistently formatted. You need to be able to see the locations of operators , e.g.  + − as well as where {...} and other bracket pairs match up. Indentation is an important part of formatting. We see all sorts of errors which would have been obvious if the code had been formatted correctly.
    Winston Gutkowski, one of our mods, wrote some useful guidelines. I find I quote the one about stopping coding most frequently. If you don't know what your algorithm will be on paper, you will have no chance of working it out in code. I disagree with Winsont's concept of equality.
     
    Campbell Ritchie
    Marshal
    Posts: 73021
    330
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Divide and rule.
    You want to break your code down into tiny parts. As Paul C said, you might want to test reading a file before you do anything with what you have read. I think you often want to have a method to read the file and a different method to do something with what you read. Otherwise you end up with methods which are so large they become unmanageable.
     
    lowercase baba
    Posts: 12989
    66
    Chrome Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    write methods like you're using Legos.  you don't want to write a single, massive method that does 12 things.  you want to write 13 methods - one method for each of the twelve, and then one method that calls the other twelve.  For example:

    write a password validator.  a password must have at least one upper, case, one lower case, one symbol, one digit, no spaces, must be at least 12 characters long, not start with an '@', and should not contain 3 consecutive same characters.

    I've seen folk try and write a single regex that does all this in one pass.  It's a nightmare to write, and even worse, a nightmare to fix if you specs change.  If instead, you write one method that checks each of the above conditions (and it's possible some of those methods may actually call multiple other methods), it's easy to adjust, turn off, or add a new condition.  you can focus on the one part, and not worry about the other eight (or whatever).

     
    Gurtej Grewal
    Ranch Hand
    Posts: 38
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    fred rosenberger wrote:you want to write 13 methods - one method for each of the twelve, and then one method that calls the other twelve.  



    coming back to this, if i have many small methods and i need to call them into eachother or have them all put together into another method, is it correct to call methodName();? This may sound simple but i remember being told that this was incorrect for some reason and it confused me for the longest time.
     
    Campbell Ritchie
    Marshal
    Posts: 73021
    330
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Gurtej Grewal wrote:. . . is it correct to call methodName();? . . .

    Yes, of course it is.

    i remember being told that this was incorrect for some reason and it confused me for the longest time.

    Somebody else was confused, too.
     
    Paul Clapham
    Marshal
    Posts: 26626
    81
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Gurtej Grewal wrote:coming back to this, if i have many small methods and i need to call them into eachother or have them all put together into another method, is it correct to call methodName();? This may sound simple but i remember being told that this was incorrect for some reason and it confused me for the longest time.



    I'm not surprised that you were confused by that. I'm confused by it too.

    First of all I don't understand what it means to call methods into each other. And I don't understand what your second question means either -- if you've got a method named clearTable() then it's correct to put "clearTable()" into your code. That has the result of calling the method. What you mean by "correct" and what alternatives you're considering as possibly correct, those are mysteries to me.

    But all of that is because you're a beginner and you don't know anything about methods yet, and so you're making up your own terminology to try and ask questions about them. It's as if you had heard about bicycles but were unfamiliar with their purpose; you might see that the wheels rotated so you might start asking questions about how to rotate a bicycle. So start by reading basic tutorials about Java on the web. There's lots of them. "Java method tutorial" is a suitable set of keywords to find them.
     
    You showed up just in time for the waffles! And this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic