• Post Reply Bookmark Topic Watch Topic
  • New Topic

Good programming habits - evaluate this very short piece of code  RSS feed

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone

I am working my way through an informal, optional assignment on basic Java. Was hoping someone could help me along with it a wee bit -- if I get it completed asap then I get feedback before an impending assignment

Part one
There are four important aspects of CODE TWO that make it better than CODE ONE. For each of these explain why CODE TWO is better in terms of one or more of: code re-use, data hiding, readability and maintainability.

Part Two
The programmer of CODE TWO needs to incorporate a new requirement into her class design. The new requirement is that instance of no other class (except for the Student class) should be able to change the value of the courseCode instance variabale. Explain how you would implement this new requirement by making a slight change to CODE TWO's version of the class (without adding or deleting any methods).
So if someone can help me get a few more points, that would be great. I have a few thoughts of my own but not enough!

Many thanks

CODE ONE




CODE TWO






My answer:

There are several aspects to CODE TWO that make it superior to CODE ONE.

The use of accessor methods in the code aids in code re-use. Accessor methods help facilitate the altering of a class or creating a subclass for use in different situations.

Data-hiding, is where an object can hold encapsulated data (the instance variables) which can only be accessed through a limited set of public methods. Although only a basic project, should the project become larger, the use of data-hiding will allow for more effeficient division of labour. Also, this use of data-hiding means that any work to be performed on classes can be isolated, allowing different programmers to work on different classes, only requiring certain public methods.

The use of method comments helps support the re-use of code as it will assist anyone looking at the code to understand how to use the method and what it should do. Therefore, it can be suggested that method comments improve the maintainability of the code as anybody maintain the code is able to understand how the method was intended to be used.

By adding new methods to the class, it makes the code easier to maintain as it re-uses methods that have already been developed and tested for that class.

 
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
code reuse: i dont really see how this code can be used in other programs, but dont trust me someone else may have a better answer.
data hiding: your member variables are private, this hides them.
readability and maintainability look great to me.
part 2: just make courseCode a private variable.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote:part 2: just make courseCode a private variable.

It already is.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ali Lumsden wrote:I have a few thoughts of my own

Well why don't you share those thoughts. It will save people repeating things that you have already thought of.
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh my bad. just make you setCourseCode() method private
 
Ali Lumsden
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your input guys

I have edited the OP to contain the full question I am attempting to answer, as well as the solution I have produced so far. I welcome your feedback and any more suggestions -- especially with regard to the second part!

Thanks,
Ali
 
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Data hiding- The code 2 is more better because your instance variables cannot be accessed directly. And their access is via the getters/setters. So at any point you can change the behavior of the accessing or setting of the data without really impacting the consumers of your API. For example- you can put a check to see that the user doesn't pass an invalid courseCode in your setCourseCode method. But if you were using the code-1 then you dont have that flexibility, you would have to introduce some method and this would require the users of the class to change accordingly.

Readability- There isn't much code there to comment on this. But by the use of the accessors the code-2 is a bit better than code-1. But in both the places proper naming of the variables and the methods has added to the readability of the code. I dont really require any comments there to understand the code. Its not necessary that comments have to be added to improve the readability of the code. Often the naming conventions should help in improving the readability of the code. Cases where you want to give an user a basic overview of what the method does or what the class is used for, then comments often add value.

Maintainability- Code-2 is easy to maintain. The reason for this according to me would be the use of getters/setters (I have explained part of this reasoning in the "Data Hiding").
Your Part Two question shows that Code-2 is easy to maintain and modify.

Code Re-use- Again the given code is not much to comment on re-use of code. But as you are using the getters/setters in the Code-2, may be you can re-use them in the Constructor of Code-2. That way all the verifications you do in your setters would be applied while you are using the constructor as well. Re usability of code is something like- you avoid copy pasting the same code in different methods for adding the same functionality- and in it is the essence of DRY principle.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you notice there are several other mistakes in code 2? You can always find more things wrong with code than were noticed at first. And if they are in code 2, you will probably find them in code 1, too.
  • 1: The indentation is inconsistent. That might simply reflect how you have copied it.
  • 2: The naming of parameters is inconsistent. If you are using the this.x = xIn; format, you might as well expose the name of the variable. If you have gone to lots of trouble to think of the best name for the field, why hide it inside your method where nobody can see it?
  • 3: They have missed out all the documentation comments.
  • 4: Inconsistent whitespace: there are sometimes spaces before () and sometimes not. Again that might be how you copied the code.
  • I would suggest a set method would look like this:There are design peculiarities, which you have not been asked about. Why does a Student have a course code and not a name? Are you validating that non-null values are being passed to the methods or the constructors?
     
    Ranch Hand
    Posts: 2907
    1
    Java Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Few observation from my side (For code 2)
  • 1. Code doesn't compile, method name is wrong on line 30.
  • 2. Why need of set method for course code, if its already been initialized thru constructor. Though its no harm in this case, but creating immutable object is always good.
  • 3. displayStudent() is doing what toString() method ideally do
  •  
    Mohamed Sanaulla
    Bartender
    Posts: 3185
    34
    Google App Engine Java Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sagar Rohankar wrote:Few observation from my side (For code 2)
  • 2. Why need of set method for course code, if its already been initialized thru constructor. Though its no harm in this case, but creating mutable object is always good.


  • You mean NOT always good? or always Good? Because if its a mutable object then you have concurrency related concerns.
     
    Sagar Rohankar
    Ranch Hand
    Posts: 2907
    1
    Java Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mohamed Sanaulla wrote:
    Sagar Rohankar wrote:Few observation from my side (For code 2)
  • 2. Why need of set method for course code, if its already been initialized thru constructor. Though its no harm in this case, but creating mutable object is always good.


  • You mean NOT always good? or always Good? Because if its a mutable object then you have concurrency related concerns.

    Oops What I really want to say is creating "immutable" object are always good. Edited original post for same.
     
    Ranch Hand
    Posts: 1608
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Randall Twede wrote:oh my bad. just make you setCourseCode() method private


    Any suggestion why?
     
    Ranch Hand
    Posts: 808
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Akhilesh Trivedi wrote:
    Randall Twede wrote:oh my bad. just make you setCourseCode() method private


    Any suggestion why?


    Read Part Two of the OP again.
     
    Akhilesh Trivedi
    Ranch Hand
    Posts: 1608
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Dennis Deems wrote:
    Read Part Two of the OP again.


    I did not read the actual post completely, just glanced over. I must be careful thanks.
     
    Randall Twede
    Ranch Hand
    Posts: 4696
    8
    Java Scala
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ali, you shouldn't have totally changed your original post. in the future just reply.
    btw i also sometimes use "this" even when it is not needed, i think it makes the code more readable. i may change my mind about this.
    before anyone jumps on me for saying that, consider the following:

    it is obvious what i am doing. how about if i wrote it like this?

    it will still work but can you tell from reading it what is going on?
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!