Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Is it a bad practice to open resources such as database connection etc in static field of a class.
class ClassName {
static {
//open database connection
}
}
Note: I am not using the same resource in multiple classes. It's used in atmost one class only.
I am doing this, basically to ensure that the program does not fail mid away through, due to error in resource allocation.
Edit : I am new to application development(trainee). I have read most of the previous answers and could not find anything related to the question i have asked (good practices).
This is definitely bad practice.
Keep in mind that static is an abnormality to good OO design (in the end, static translates to procedural programming in disguise).
In other words: you don't want something so essential as preparing your database setup happening in some static initialisier. Meaning: most likely you should add certain abstractions to your classes do not depend directly upon such code.
Beyond that: what happens if you figure at one point that you need certain levels of control? That will be almost impossible if those "resources" are statically owned by your classes.
And of course: if your classes rely on static code ... you render them "hard" (well, almost impossible) to unit-test by default. Not using static is like lesson number one in "how to create testable code" school.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I think javadocs are beautiful. Clear descriptions for each method and class. Our tester can easily write unit-tests without repetitive explanations from programmers.
This is how we practice TDD at our start-up. We first sit and plan the application structure, and we start creating all the methods and classes (without programming), we add descriptions to the classes, constructors and methods with javadoc. We then ship this to our tester who write unit tests. When he is done, we start programming.
No one is actually complaining. Our tester is in love with javadoc and although he is a terrible programmer (that is why he became a tester), he can easily understand the javadoc and write junit-tests.
The thing is, most of us are newbies in a start-up. And I don't know if you are supposed to document all the classes and methods before we even start programming? My question to you more experienced programmers is: is this a good TDD approach?
TDD is useful, because it makes sure you do not miss any requirements. It ends up beings used as unit test cases, and while the programmer starts their work they need to make sure all the test cases are covered.
Testers write system & integration test cases.
coming to javadoc, ideally it should be a must. It removes code duplication/reusablity and also adds some value to system document.
Javadoc should describe the return type, input params and process being carried out within the method.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I recently started a project in Java, that contains a class called System. This class (Luckily) contains methods for output management, so in the rare cases where I need to use the System. methods (Or the System object in general) I just reference it as java.lang.System.. I believe that this could be looked down upon, as System could be looked at as a reserved name. I currently am in the beginning stages of this program, and could change it accordingly quickly, as there are little calls to the class itself.
While it's not illegal, you don't want to do this. If I were the next person working on your code, the first thing I would do is try to remove "java.lang" from "java.lang.System" and then get miffed when it wouldn't compile.
The idea is to go toward brevity and only write what you need to write, while making sense of it all for the next person. It's more an art than a science.
You could always name it something like ProjectnamehereSystem or OutputManager or something to that effect.
I would not create something so similarly named as an important class. While everything is easy to edit, you may be able to keep up with all the changes you are making.
But when the project evolves things will get messy and complex. I would suggest naming it something else that can be easily distinguished.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
This is more of a conceptual question than technical pertaining to Java. I've noticed lately programmers tend to avoid creating classes to pass data around and simply move it using JSONObject if available or a basic HashMap. A lot of new interviewees choose to use what I call a JavaScript method of coding. So instead of creating class called Address, they would store it all in JSONObject/HashMap and pass it around as Object later casting to appropriate type. Also I notice a lot of
try { ... }
finally { return str; }
structured methods.
Is this something normal and accepted these days? To me, it just goes against everything I was thought in school. I mean, no generalization, no encapsulation, etc... And when asked, they claim JSONObject is encapsulation. Well, not disagreeing, but just not something expected.
The task is always being done in the end, but the way its done is disturbing to me. This kind of practice produces a lot of loose structures with a lot of potential errors if programmers are not careful. Wasn't java generics created to avoid exactly that?
Feel free to move this question to other exchange, but I'd really like to hear some opinions from seasoned folks.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
My project classes are quickly approaching large numbers of lines into the thousands. Is it good programming practice to divide the classes Into smaller classes even if they do the same thing? After all I'd hate to create communication caller functions for the same object.
It is a good programming practice to split up your code so you (and others) don't get lost.
Split it into methods/functions/procedures, classes and packages by meaning, not by size alone.
If several classes do the same thing, have you thought about using inheritance? Don't duplicate code, it makes maintenance harder (and is a waste).
For Java, interfaces and abstract classes can also improve legibility and structure of your code; use with moderation. Many Java IDEs come with handy "refactoring" functionalities which allow you to restructure your code easier and cleaner than copy/paste would be.
( Possibly related topic: "how do you organize your programming work" how do you organize your programming work )
As a rule, each class should have one responsibility that you can clearly state. If you can't state a single purpose for a class, or the narrowest purpose you can define is nebulous and vague, it's time to refactor.
Of course there are exceptions to every rule, and some classes with a lot of utility methods (like String) will be very large. But I generally take a hard look at the purpose of a class when it grows past about 300 lines. (For the second time - I do it the first time before the class grows past 0 lines.)
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I've heard so many different opinions, and haven't really been able to find a solid answer to this question. When/why are static variables bad?
If I wanted to store names of players in a game in a List, would it be better to make it non-static than static? If so, why?
The list would be updated everytime a player leaves/joins. It would be used to show the players who else is playing and to track their play time.
Static variables aren't necessarily bad, but a major principle of software design is that you should contain information to the smallest context that needs to know it. If it really makes sense for a variable to be shared among all instances of a class--logger objects are a common example--then making it static is just fine. If it would ever make sense to have more than one version or copy of it, as it would in the case of the players of a game (think multiple games running simultaneously), then it's best to place the list in the smallest possible (non-static) context.
Static variables make it more difficult to see the data as the input/output of functions, because any function has access to a static. It becomes an issue when you get thousands or tens-of-thousands lines of code. Not so much in your first small app.
It's also easier to "leak memory" because the life of a static is the same as the program, meaning it never gets garbage collected till the pogram closes, or it's nulled by code.