Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Working code, but has it been correctly written?  RSS feed

 
Glenn van Cauter
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So as the title says .. I wrote a piece of very basic Java code which simply asks the user a question and then gives feedback based on the input.

This is the code:



So I know the code works and stuff, but what I would like to know is if I written the code in a efficient way? If not, what can I use to nest and/or efficient code the above?

Regards,
Glenn
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, good job writing code that works!  That's always an accomplishment.

Your formatting is not standard.  There isn't one absolute way to format code, but formatting is a more standard way is necessary -- not just nice.  Here is a good start to standard formatting.

There is also a very strong convention that variables should start with a lowercase letter.  Only types (Classes, Emuns, Interfaces) should start with an uppercase letter.

You have a lot of code in the main() method.  With a short program like this is probably doesn't matter, but writing most of your code outside of main() is a good habit to start.  Even just creating a method called run and putting your code there is a step in the right direction.

Good instincts on putting the text into strings.  The next step is to get the string variables out of  main() and make them "constants".  "Constants" is in quotes because Java doesn't have a const keyword, but the convention is to make a variable constant by making it static and final.
 
Glenn van Cauter
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:First, good job writing code that works!  That's always an accomplishment.

Your formatting is not standard.  There isn't one absolute way to format code, but formatting is a more standard way is necessary -- not just nice.  Here is a good start to standard formatting.

There is also a very strong convention that variables should start with a lowercase letter.  Only types (Classes, Emuns, Interfaces) should start with an uppercase letter.

You have a lot of code in the main() method.  With a short program like this is probably doesn't matter, but writing most of your code outside of main() is a good habit to start.  Even just creating a method called run and putting your code there is a step in the right direction.

Good instincts on putting the text into strings.  The next step is to get the string variables out of  main() and make them "constants".  "Constants" is in quotes because Java doesn't have a const keyword, but the convention is to make a variable constant by making it static and final.


Thank you so much for your reply, Knute Snortum!

A few questions ..

1. Strange, I'm following this guide (SoloLearn) and they tell me to always put code within the method instead of outside of a method. Why is it better to have for example, strings as constants outside a method?
2. What is a Constant?

Sorry if I didn't understand you properly.

Regards,
Glenn
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16027
87
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glenn van Cauter wrote:1. Strange, I'm following this guide (SoloLearn) and they tell me to always put code within the method instead of outside of a method. Why is it better to have for example, strings as constants outside a method?

Knute did not mean that you should literally put the code outside of any method. He means that you should not put everything in one method (for example, the main method) - you should instead group it into logical parts, and put each of those parts in its own method.
 
Glenn van Cauter
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
Glenn van Cauter wrote:1. Strange, I'm following this guide (SoloLearn) and they tell me to always put code within the method instead of outside of a method. Why is it better to have for example, strings as constants outside a method?

Knute did not mean that you should literally put the code outside of any method. He means that you should not put everything in one method (for example, the main method) - you should instead group it into logical parts, and put each of those parts in its own method.


Thank you very much Jesper!
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Strange, I'm following this guide (SoloLearn) and they tell me to always put code within the method instead of outside of a method. Why is it better to have for example, strings as constants outside a method?

SoloLearn is essentially correct, but it is allowed, and sometimes necessary to declare a variable outside of any method.  This changes its scope so that it can be accessed in any method.

2. What is a Constant? 

A constant is a variable whose content doesn't change.  You make the variable final to do this.  The convention is also to make the variable static, so there is only one copy of it.  Also, the naming convention for a constant is different:
 
Dave Tolls
Rancher
Posts: 2911
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
Knute did not mean that you should literally put the code outside of any method.


Well, he is (sort of) since he's talking about turning the String constants into actual constants in the class, rather than in the main() method.
Of course depends on your definition of "code".
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glenn van Cauter wrote:
1. Strange, I'm following this guide (SoloLearn) and they tell me to always put code within the method instead of outside of a method. Why is it better to have for example, strings as constants outside a method?
2. What is a Constant?


Well, to an application, a constant is simply a value that does not change. To the compiler, with one exception to be discussed, this means nothing... ... it is simply another value that needs to be stored.  If the value is stored with a static variable, then the variable is initialized just once (when the class is loaded). If the value is stored with an instance variable, then the variable is initialized every time an instance is created. And finally, if the value is stored with a local variable, then it is initialized every time the method is called.

Ideally, if the value truly never changes, then storing it as a static variable guarantees that it will initialize only once. In this case, it is a local variable of the main method, which is only run once, and all the code is in the main method, so probably, it doesn't matter.


The one exception case is the compile time constant. These are constant values, that are setup in a very particular way, that the compiler can detect/confirm that it is a constant, and can do some optimization with it. One optimization is that the value is never actually saved; when the compiler sees the constant variable used, the generated code actually uses the value directly.

Henry
 
Glenn van Cauter
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much all .. I think I need to rest, my brain is overloading!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!