JD1's Personal Development Blog
Hello everyone,
Firstly, I'd like to apologise if this thread's in the wrong forum. There wasn't anything that was going to really suit the topic of this thread. As posted in my introduction, I'm going to do my best to learn as much as possible about the Java programming language over the next year (and afterwards, of course).
To help me cement some of the new found knowledge I'm receiving via tutorials and guides, I've decided to keep this thread as my personal location for posting what I'm learning about, any questions I might have, and code snippets I'm working with. That is, of course, if that's alright with the staff of JPF.
Unless you have anything to contribute; perhaps you can answer a question of mine or make a comment on what I'm doing, I'd ask that you let me be. Don't get me wrong, I'm all for assistance - that's what I need most. I just want to keep this thread as a collection of my personal work, so I'd like all discussion here directly relevant to what I'm posting, if that's alright.
Thanks,
OA-OD-JD1
Re: JD1's Personal Development Blog
Just a few words of advice, take them or leave them:
Even if an if or a loop only consists of a single statement, you should still surround it with {brackets}. This is not only a standard Java convention, but I promise it'll save you a headache in the future.
And perhaps I can clear up some of your thoughts on the switch statement.
You use an if statement to test conditions, the operands of which you might not know at compile time, because they can change depending on program flow. But a subsection of those cases exists where you do know the value of one of the operands at compile time, and you're simply testing whether the other operand is equal to any of those known values. That's when you use a switch statement. It's not just more efficient, but once you're used to it, the syntax is also easier to read (easier to read than a bunch of if statements), which makes it easier to debug and harder to screw up.
Re: JD1's Personal Development Blog
Quote:
Originally Posted by
KevinWorkman
Just a few words of advice, take them or leave them:
Even if an if or a loop only consists of a single statement, you should still surround it with {brackets}. This is not only a standard Java convention, but I promise it'll save you a headache in the future.
And perhaps I can clear up some of your thoughts on the switch statement.
You use an if statement to test conditions, the operands of which you might not know at compile time, because they can change depending on program flow. But a subsection of those cases exists where you do know the value of one of the operands at compile time, and you're simply testing whether the other operand is equal to any of those known values. That's when you use a switch statement. It's not just more efficient, but once you're used to it, the syntax is also easier to read (easier to read than a bunch of if statements), which makes it easier to debug and harder to screw up.
Hi Kevin,
Thanks for your input. Can you briefly explain why it is necessary to encase single statement If Statements in brackets? I get that it's a global programming convention to do this, just incase you need to go back and add something else, but is there a more latent reason?
I'm definitely going to be using more Switch Statements. I guess it would be more effective to use an If Statement if you're testing one condition, or even if you're testing two (use Else If). But as soon as there's three conditions or more to be tested, I'm thinking I'll go for the Switch Statement. Is this advisable?
Thanks,
OA-OD-JD1
Re: JD1's Personal Development Blog
Quote:
Originally Posted by
OA-OD-JD1
Can you briefly explain why it is necessary to encase single statement If Statements in brackets? I get that it's a global programming convention to do this, just incase you need to go back and add something else, but is there a more latent reason?
From the standard naming conventions: "Braces are used around all statements, even single statements, when they are part of a control structure, such as a if-else or for statement. This makes it easier to add statements without accidentally introducing bugs due to forgetting to add braces."
And from experience, not using brackets leads to more confusion than you'd think it would- for you and for people reading your code.
Quote:
Originally Posted by
OA-OD-JD1
I'm definitely going to be using more Switch Statements. I guess it would be more effective to use an If Statement if you're testing one condition, or even if you're testing two (use Else If). But as soon as there's three conditions or more to be tested, I'm thinking I'll go for the Switch Statement. Is this advisable?
It's not just the number of conditions, it's also the idea of knowing one of the operands of the condition ahead of time, at compile-time.
Re: JD1's Personal Development Blog
If you are serious about blogging your progress here, I will grant you Blog permissions. PM me.
Re: JD1's Personal Development Blog
Quote:
From the standard naming conventions: "Braces are used around all statements, even single statements, when they are part of a control structure, such as a if-else or for statement. This makes it easier to add statements without accidentally introducing bugs due to forgetting to add braces."
And from experience, not using brackets leads to more confusion than you'd think it would- for you and for people reading your code.
Thanks for sharing that. I'll definitely be using braces for all If Statements (and probably others too, where appropriate).
Quote:
It's not just the number of conditions, it's also the idea of knowing one of the operands of the condition ahead of time, at compile-time.
I'm not 100% sure I understand you just yet. Would you be able to share an example?
Quote:
Originally Posted by
JavaPF
If you are serious about blogging your progress here, I will grant you
Blog permissions. PM me.
I'd really like that. Hopefully by maintaining an official blog, I can help out others as they make a similar journey into the world of Java. I appreciate this.
Thanks,
OA-OD-JD1
Re: JD1's Personal Development Blog
Quote:
I'll definitely be using braces for all If Statements
Don't limit their use to if statements.
There is always a time you decide later and in a hurry to insert some statement at the front end of some code that is naked (without {}s) and voila a bug.
If you always add the { a blank line and the ending } you'll save yourself a few gotchas.
Re: JD1's Personal Development Blog
Quote:
Originally Posted by
Norm
Don't limit their use to if statements.
There is always a time you decide later and in a hurry to insert some statement at the front end of some code that is naked (without {}s) and voila a bug.
If you always add the { a blank line and the ending } you'll save yourself a few gotchas.
And in terms of effiency, the braces really don't make much of a difference, do they?
Re: JD1's Personal Development Blog
Quote:
Originally Posted by
OA-OD-JD1
And in terms of effiency, the braces really don't make much of a difference, do they?
No, braces don't really effect efficiency but the actual thing is if you are too beginner, don't just leave using braces whether it's a single statement as mentioned by others, this can in the end lead you to the huge trouble......
Re: JD1's Personal Development Blog
Quote:
Originally Posted by
Mr.777
No, braces don't really effect efficiency but the actual thing is if you are too beginner, don't just leave using braces whether it's a single statement as mentioned by others, this can in the end lead you to the huge trouble......
No worries. I'll make sure to implement braces in every statement from now on. Thanks.
Re: JD1's Personal Development Blog
@OA-OD-JD1: Though you are learning Java but you must need to know the naming conventions for variables, constants, methods, classes, interfaces etc....
So, follow the Sun naming conventions in order to have a good programming experience (Java)
Re: JD1's Personal Development Blog
Quote:
Originally Posted by
Mr.777
@OA-OD-JD1: Though you are learning Java but you must need to know the naming conventions for variables, constants, methods, classes, interfaces etc....
So, follow the Sun naming conventions in order to have a good programming experience (Java)
The reason I've used such odd class names and variable names is to help paint a picture in the reader's head (my head). These simple names aren't meant to uphold the various naming conventions, but are more aimed towards enabling me to fully understand what's going on and what's what.
Re: JD1's Personal Development Blog
Some more comments on your coding style;
Class names should start with uppercase letters:
class class1{
vs
class Class1{
Put some white space in the statement to make them easier to read:
String name="Tony";
vs
String name = "Tony";
Re: JD1's Personal Development Blog
Quote:
Originally Posted by
Norm
Some more comments on your coding style;
Class names should start with uppercase letters:
class class1{
vs
class Class1{
Put some white space in the statement to make them easier to read:
String name="Tony";
vs
String name = "Tony";
I'll keep that in mind when creating class names. I don't know what it is, but I actually enjoy using as little white space as possible. I like jamming everything together. I know it's not the best way to go about things, but it works for me so I'm going to stick to is. Thanks, Norm.
Re: JD1's Personal Development Blog
As long as you don't care if anyone else can look at your code and understand it, you are free to do it any way you want.
Then why are you posting your code here?
Re: JD1's Personal Development Blog
OA-OD-JD1, I'd listen to Norm and KevinWorkman, these guys are where you want to be.
Convention is massively important. Play by the rules now and you won't have to adjust your coding style in the future.
Re: JD1's Personal Development Blog
Quote:
Originally Posted by
OA-OD-JD1
I'm not 100% sure I understand you just yet. Would you be able to share an example?
Case expressions must be constant values. So if you don't know the values ahead of time (if it's based on some calculation that might change, for example), then you can't use a switch.
Re: JD1's Personal Development Blog
Quote:
Originally Posted by
Norm
As long as you don't care if anyone else can look at your code and understand it, you are free to do it any way you want.
Then why are you posting your code here?
You win. I'll space it out nicely. :rolleyes:
Quote:
Originally Posted by
KevinWorkman
Case expressions must be constant values. So if you don't know the values ahead of time (if it's based on some calculation that might change, for example), then you can't use a switch.
Understood.
I've Learned About A Few General Java/Programming Conventions
First of all, thanks to the following members for helping me out thus far. I very much appreciate your time and effort and I've decided to take everything on board.
First of all, KevinWorkman told me of the importance of implementing braces on all statements (If Statements, Switch Statements, etc) regardless of whether there's only one line's code after the condition. If you don't implement braces around these statements and you decide later that you want to add another line of code into a statement, there's a chance you're going to forget to insert your braces. Basically, we do this just to prevent bugs from appearing at compile time.
Kevin also addressed a question I had about Switch Statements. Instead of using an If/If Else Statement, we use a Switch Statement when we know exactly what one of the operands are ahead of compiling.
JavaPF then mentioned he would allow me blogging permissions so I could make this an official blog. Hopefully, I'll be moving this blog to an official blog in the near future.
Norm and Mr.777 then backed up Kevin to cement the importance of using braces always.
Norm then made two very useful observations about my programming style. First, he made aware to me the fact that classes should start with a capital letter. Instead of using class1, I should have been using Class1. I'll make sure I use proper capitalisation in the future. Norm, I presume that if the name of the class has two words, so to speak, that the first word isn't capitalised and the second is? By this I mean, startRendering. If we were to use that as a class name (for whatever reason), it would be as is instead of StartRendering? Maybe I'm getting this confused with the naming conventions for variables. I'd appreciate if you could clear that up.
Norm then pointed out that I clustered my code together too much. An example being, if(this==that). I should be spacing it out some more and implementing more white space: if(this == that). I had been under the impression that it wasn't really a big deal whether you made use of spacing or not, but I guess it only makes sense to space it out. If not for yourself, for others.
That's all for now. I'll be covering some more in-depth information on the use of multiple methods in my next post.
Thanks everyone,
JD1