50 Replies - 23155 Views - Last Post: 17 October 2011 - 04:20 PM
#16
Re: What NOT to do when Java Coding
Posted 04 February 2011 - 09:33 AM
#17
Re: What NOT to do when Java Coding
Posted 04 February 2011 - 09:36 AM
cfoley, on 04 February 2011 - 06:23 AM, said:
Anyway, the gist of what I said was that that it's a bad idea to write code that makes thing easier in some way for the computer (i.e. uses less memory or runs quicker). Most of the time you can end up with bloated, buggy, unmaintainable code for negligible gain. Focus on writing clean, clear code and save optimisation for when you have a real performance problem.
I missed this earlier, but it's a good point and to quote Knuth:
Quote
At least I've seen it attributed to Knuth.
cfoley, on 04 February 2011 - 11:33 AM, said:
They have the getters and the setters. There is nothing dangerous or poor about how you interact with x and y by calling location.x and location.y.
#18
Re: What NOT to do when Java Coding
Posted 04 February 2011 - 09:42 AM
#19
Re: What NOT to do when Java Coding
Posted 04 February 2011 - 09:43 AM
Note: I am aware that the line I made up isn't a semantically correct line used to rearrange the list.
This post has been edited by xclite: 04 February 2011 - 09:44 AM
#20
Re: What NOT to do when Java Coding
Posted 04 February 2011 - 09:46 AM
Furthermore, if I decide I need a thread safe point class, I cannot simply extend the one you linked to. It's public members become part of my public interface. I can override the methods to synchronize them but I cannot do that with the variables. I'd have to make a wrapper class.
I think I know why they might have done it. They have:
public double getX()
If you add
public int getX()
it won't compile. What they should have done is omething like this:
public int getXasInt()
Quote
You can make it package access and still access it from the enclosing class. Damn, make it private and access it from synthetic accessor methods. In this case, the Node class is an implementation detail of the data structure. Making it public here is probably not the sin it is in top level classes but it's still confusing to the programmer who thinks it will be accessible from anywhere. Leave it default access and make the intent clear.
#21
Re: What NOT to do when Java Coding
Posted 04 February 2011 - 10:02 AM
#22
Re: What NOT to do when Java Coding
Posted 04 February 2011 - 10:08 AM
#23
Re: What NOT to do when Java Coding
Posted 04 February 2011 - 10:13 AM
#24
Re: What NOT to do when Java Coding
Posted 04 February 2011 - 10:15 AM
cfoley, on 04 February 2011 - 10:43 AM, said:
A creator pattern is nice. Though it could just as easily support public Double(String s). Hardly required or a compelling reason for static.
xclite, on 04 February 2011 - 11:43 AM, said:
Excellent example. I agree. Some classes are objects with behavioral patterns than need to be encapsulated. However, some are just complex data types, holding information and little more. Such things like private classes needn't worry about data wrapping.
OO design techniques address scope of use. I want to limit the impact of changes and the rest of the program from undefined behavior. A class whose only job is to be a data container can be completely open, depending on the scope of the class and structure of the program.
#25
Re: What NOT to do when Java Coding
Posted 04 February 2011 - 10:23 AM
baavgai, on 04 February 2011 - 12:15 PM, said:
cfoley, on 04 February 2011 - 10:43 AM, said:
A creator pattern is nice. Though it could just as easily support public Double(String s). Hardly required or a compelling reason for static.
I think you accidentally quoted cfoley =p
I could live with that, although I still stand in support of utility methods such as the Math library. I've also written a custom library that added ways to manipulate strings - certainly not methods that relate to any kind of instantiated object (and you can't change/extend the String class in Java), so they made sense to be static.
This post has been edited by xclite: 04 February 2011 - 10:23 AM
#26
Re: What NOT to do when Java Coding
Posted 04 February 2011 - 11:09 AM
set\get methods also allow you to check bad values passed to your variables.
Perhaps you have a variable that should hold only even integer values (I know it's unlikely, but that's just an example)
if that value is declared as public, you can actually assign it any value. However, if you declare it as private, and create a set method, you can add code to check the value passed to that variable and throw exception, or maybe change the value if an odd value is passed.
And if tommorow your manager says that he decided that the variable should hold only numbers divisable by 3, all you have to do is a small change to your set method.
However, If you used a public variable, you'll have to make much more changes to your code.
set\get methods offer a lot of modularity to the code.
#27
Re: What NOT to do when Java Coding
Posted 04 February 2011 - 11:14 AM
#29
Re: What NOT to do when Java Coding
Posted 04 February 2011 - 11:44 AM
SpeedisaVirus, on 04 February 2011 - 01:14 PM, said:
More times than my code has been complained about =p
You all are making great points for using getters and setters - and I agree that they are a good idea. I feel like my opposition to the words always and never is being taken as "getters/setters are useless."
#30
Re: What NOT to do when Java Coding
Posted 04 February 2011 - 01:44 PM
Also, never leave methods with more than 50 or so lines be. If you have 50 lines in a method, you are likely approaching it the wrong way. Take a step back and ask yourself how you can make it more OO or functional.
|
|

New Topic/Question
Reply




MultiQuote










|