TLDR; Coding Tips for beginners.Write your code as temporary comments, remove syntax errors as soon as you see them.
Here are a few tips I’ve been verbally passing on when teaching people on my Java For Testers face to face training.
- write the code you want to see as comments first
- remove syntax errors as soon as you see them
Write the code you want to see as comments first
Creating a blog comment e.g.
iterate over the list and print the name of each object in the list
and assert that when I call getAge for each object it is greater than 18
The reason for doing this is that learning Java is hard enough:
- what short cut keys in the IDE do I use
- what was that loop construct again?
- how do I get the age?
- what does the if statement look like?
You are trying to remember a whole bunch of stuff.
Writing down what you are trying to achieve means that you don’t have to keep that in your head at the same time.
Eventually you will stop doing this. And you will want to delete the comments when you are finished.
But, I’ve seen this help people because this helps stopping people get too lost.
Remove syntax errors as soon as you see them
When people don’t do this, they end up writing a bunch of code and then none of it works, and it can be hard to resolve.
As soon as you see a syntax error, fix it, to allow you to write code and harness code completion.
Sometimes that means I’ll write
"" because I just want the syntax error to go away and I haven’t decided on the data yet.
Sometimes that means I’ll pass in
null as the argument, because I don’t know what it should be yet, but I know it needs to be there.
Then when the line of code is syntactically correct, I make it semantically correct.
This also helps when you are using the IDE to write code with “Alt+Enter”, the more syntactically correct it is, the more the IDE will generate the code you want to see.
And using the IDE to write your code can help avoid syntax errors.
Those are the first two tips that come to mind.
I’ll try to keep a list next time I do training as simple things can avoid the cognitive load associated with learning to code.