All Questions
Tagged with java programming-practices
127
questions
1
vote
2answers
73 views
Is using KDoc/Javadoc comments inside of a function considered bad practice?
I often find it helpful to use KDoc/Javadoc comments inside of a class or function instead of normal comments. IntelliJ colorizes them more obviously by default and, more importantly, allows ...
3
votes
2answers
106 views
Checking the user in almost all use cases
I have a web application that has Users that belong to Companies. A User can only belong to 1 Company at a time and they can manage their own company information. I'm using java spring and I'm ...
4
votes
3answers
227 views
Passing object or using the field
I would like to know what is a more appropriate way to code in Java. Is it generally better to pass entire objects in the method's parameters or just using the fields from the class?
Using the field:
...
-3
votes
1answer
123 views
Is it good practice to use try/catch like Python in Java?
I mainly use Python and just started learning Java. For now, I've tried using try/catch for basic file read/write as follows
public String[] readFile(String fileName){
try{
// read file
} ...
0
votes
3answers
107 views
When using a DSL to structure my application, should I favor coupling the code with the DSL, or trying to have majority of my code independent of it?
We're currently using the Apache Camel Java DSL to structure our application, but I guess this question can mostly apply to any DSL in general.
Now, amongst our developers, we are divided on two polar ...
1
vote
4answers
663 views
Separating data and behavior in Java
I am in the process of refining my code to adhere more to my current understanding of the Single Responsibility Principle (SRP). Originally, I had a class called Animal that had a set of methods and ...
2
votes
4answers
341 views
When is it appropriate to reuse a method for another method?
I am writing a program that describes different properties on a single Management Company's plot of land. For this program there are 3 overloaded addProperty method's. My question is I can reuse the ...
1
vote
2answers
1k views
Is using Objects.requireNonNull() in production bad practice?
Objects.requireNonNull is a helpful method to spot NullPointerExceptions effectively while developing. But in fact it's throwing a RuntimeException which could crash the application (the worst thing ...
1
vote
1answer
300 views
Where and how to connect to external API in my service class?
In my service class I would like to connect to external API. Where and how should I do it?
1) Inject in constructor ExternalClass and assign to private property. Next in other property in constructor ...
4
votes
3answers
3k views
Are there any drawbacks to using a nested class instead of declaring a new one?
I'm doing code review on a change my co-worker made to our Java application, and I've found something I'm not very familiar with - a nested class.
From reviewing the code, it seems like the nested ...
1
vote
2answers
123 views
My implementation accepts only URLClassLoader, may I be less restrictive in my method signature?
I wrote a Java API that accepts ClassLoaders as parameter, even though it won't accept any ClassLoader that isn't a URLClassLoader (because I'm only interested in the URLClassLoader::getURLs() method):...
0
votes
1answer
1k views
Passing an inputStream vs String [closed]
I have a file on the disk. I want a clear distinction of responsibilities between my reader and parser. The reader reads the file and extracts the content as a string.
This string is input to the ...
6
votes
5answers
3k views
What is more important? SOLID or KISS?
What is more important? SOLID or KISS?
To be consistent with SOLID I would have to inject Decorator and Footer classes and create interfaces for them.
public class HelloService {
String name;
...
7
votes
5answers
6k views
What to do with long variable names?
First example:
countryRepository.getCountriesFromAsiaWhereAreTheMostPlanesAndBoats();
countryRepository.getCountriesFromAfricaWhereAreTheMostCars();
Are these names too big? How else do we call ...
2
votes
5answers
1k views
What should a constructor contain?
What should a constructor contain?
In both cases, all three arguments are needed for the class to work.
Which approach is better and why?
1)
class Language {
LanguageRepository ...
4
votes
2answers
1k views
Separating Read / Write Responsibilities of a DB
We're working on a reporting system which has a clear write and a read path. For example writes will only happen after consuming events from a queue and reads will only happen when serving requests ...
0
votes
1answer
325 views
Is using Java reflection to map frontend actions to the actual db methods by name bad practice?
I have used java reflection with a static map to map actions(removeElements, getLatest ...) from the front end to the corresponding database methods by name in multiple web applications now.
I was ...
0
votes
1answer
254 views
Which is better in terms of performance (bool01==bool02) vs (bool01 && bool02) [duplicate]
Which of these two is the better way of doing the same thing -
public void someMethod(String s, boolean bool02){
boolean bool01 = s!=null;
if(bool01==bool02){
doSomething();
}
}
OR
public ...
5
votes
2answers
6k views
Design pattern for processing a huge CSV file — Java
I am learning design patterns in Java and also working on a problem where I need to handle huge number of requests streaming into my program from a huge CSV file on the disk. Each CSV line is one ...
6
votes
4answers
26k views
A DTO class having an ArrayList of its own type - is this considered a good design?
I came across a DTO class like the below one.
class PersonDTO {
private String firstName;
private String middleName;
private String lastName;
private String dob;
// some 50 fields ...
295
votes
16answers
28k views
Grokking Java culture - why are things so heavy? What does it optimize for? [closed]
I used to code in Python a lot. Now, for work reasons, I code in Java. The projects I do are rather small, and possibly Python would work better, but there are valid non-engineering reasons to use ...
0
votes
2answers
154 views
Which End should start first? (MVC)
I am developing a small application and trying my best to make it as professional as possible in regards to design pattern etc.
It is a JavaFX app, and my app works fine but I am uneasy at the fact ...
1
vote
7answers
2k views
Repeated Instantiation of the Same Class in a Method
This is sort of a follow up question on Multiple Same Object Instantiation.
And I think, is not really language specific, so this is applicable to Java and C#?
Version A
public MyClass {
public ...
6
votes
1answer
2k views
Is it a good idea to use a Spring MVC as an Frontend of a Microservice Architecture?
My microservice prototype currently has a Spring boot MVC application as its front-end. The application renders the View completely in the backend. It makes rest calls to other microservices like ...
3
votes
1answer
474 views
Contract interface/class with inner classes/interfaces
Brief description of my project structure.
I have some base classes like BaseView, BasePresenter ... .
Also my project consists of modules, module represents one complete part of the application.
...
10
votes
4answers
3k views
What are the responsibilties of the main in object oriented programming?
I'm new to object oriented programming and I don't understand what's the purpose of the main.
Yes, I read that it's the "entry point" of the program but what I don't understand is what should be in ...
-1
votes
3answers
367 views
Mental model for working with linked list [closed]
I have been doing some practice problems on LinkedList but more than half of my time is spent on managing null pointer issue in my code, provided I have to keep track of current, previous and runners ...
2
votes
2answers
166 views
Is it good practice to use a free website as a package
Is it good practice to use a free website as package identifier if I do not have a real website? EG com.weebly.vikarjramun.myapp.myactivity Just wondering...
2
votes
3answers
199 views
Data duplication, can it be an unavoidable practice in this example?
Say I have different employees of type Employee stored in a list inside a class SubCase.
public class SubCase{
protected ArrayList<Employee> employees;
...
}
SubCase represents a part ...
1
vote
0answers
89 views
Updating class property inside the method vs. returning a value from the method
In a large code base, what is a good practice between: Updating class properties inside the method vs. returning a value from the method and updating the property in the place where the call to the ...
4
votes
2answers
190 views
The order of values in ? : expression
Which construction is more readable:
someVariable == null ? SOME_VALUE : someVariable; // avoids negation
someVariable != null ? someVariable : SOME_VALUE; // normal situation first
I have to use ...
2
votes
1answer
245 views
Choice of variable names and types in graph algorithm
I am learning algorithms and data structures from this awesome resource Algorithms. Rather than doing dry reading I am trying to re-write all the code myself so that I can learn coding as well as ...
3
votes
3answers
12k views
How to store many global variables?
I have around 30 not-changing "objects" (the amount of them is final, no more can be added or removed). Each object has an id as well as some booleans describing what the object is and what it isn't. ...
38
votes
5answers
8k views
When should I extend a Java Swing class?
My current understanding of Inheritance implementation is that one should only extend a class if an IS-A relation is present. If the parent class can further have more specific child types with ...
1
vote
1answer
1k views
Improving a try/catch
I'm a python programmer trying to get to grips with Java's inflexibility; I'm trying to parse a date from a string into a Calendar object
private Calendar parsedDate ( String dateString ) throws ...
0
votes
2answers
112 views
Accessing C methods from class A
class A {
B bObject;
}
class B {
private List<SomeType> list;
public List getList() {return list;}
public void foo(int i) {
list.get(i).someTypeMethod(); // 2 dots
}
}...
1
vote
1answer
174 views
Better way to organize query methods in Android?
In my Android app I have:
A SQLiteHelper class that extends SQLIteOpenHelper, and takes care of things like table-creation and upgrades.
A SQLiteDatasource class that performs CRUD operations on the ...
3
votes
2answers
2k views
Using MVC style, where is the best place to put SQL functionality?
I am wondering about best practices here.
MVC (Model - View - Controller) patterns involve separating components of your program that model the data, manipulate those models, and display those ...
2
votes
2answers
217 views
Can I alter the code while adding logging?
When I add logging (e.g. log4j2 in Java) to existing code, is it ok (good practice), to alter the code?
Consider the following example:
//if process returned 0 return true
return (returnCode == ...
1
vote
1answer
419 views
Service/component based application in Java
I have a Java project whose architecture is quite component-oriented, and I am wondering
if this is a common way to organize code
which rules/patterns are used,
if there is a name for this coding ...
2
votes
3answers
185 views
When a class represents a property that might be invalid, how should the validation be done?
I have a Product class which has among others an attribute Ean13 that encapsulates an EAN13 code.
Here is a prototype of the Product class:
@Entity
@Table(name = "tb_produtos")
public class Product ...
46
votes
6answers
13k views
Is it better to check `c >= '0'` or `c >= 48`?
After a discussion with some my colleagues, I've a 'philosophical' question about how treat the char data type in Java, following the best practices.
Suppose a simple scenario (obviously this is only ...
-3
votes
1answer
102 views
Manage lock of a software feature [closed]
I've wrote a standalone java SE software which executes, for example, the features A, B and C.
Now, i want to that, if a lock is enabled (and this lock is a command sent remotely by my backend), the ...
31
votes
6answers
11k views
Is throwing new RuntimeExceptions in unreachable code a bad style?
I was assigned to maintain an application written some time ago by more skilled developers. I came across this piece of code:
public Configuration retrieveUserMailConfiguration(Long id) throws ...
6
votes
4answers
3k views
Creating two-way object references and keeping data integrity
I have two different classes; a Player and a Group. I need to be able to query a Player which Groups they are registered to (player.getGroups()), and which Players are registered to a Group (group....
1
vote
2answers
5k views
guideline on void methods that do not throw exceptions
In our codebase I saw a method that is similar to following:
public void doSomething() {
try {
...
} catch (Exception e) {
...
//log something
...
}
}
The ...
1
vote
3answers
2k views
When is a event listener invoked during program execution?
I am using the Obervable-Observer Pattern.
MyClass extends Observable{
invokeListeners(){
doSomething;
setChanged();
notifyListeners();//This invokes onUpdate() in "implements ...
3
votes
3answers
19k views
Using System.err.println() for debugging in Java
System.err
The "standard" error output stream. This stream is already open and
ready to accept output data. Typically this stream corresponds to
display output or another output destination ...
2
votes
1answer
219 views
Is it OK to deprecate methods that need to be public due to the packaging model but are not to be used outside the codebase in Java?
I am currently working on a semi-large project that has several packages. There are 3 main packages, a "client" package, a "server" package and a "common" package. There are two jars, one for the ...
2
votes
3answers
232 views
Documenting intent Vs knowledge
It is said you should document the intent of your function or class which I agree with. However recently this line became a little bit blurred when I was wanting someone to document why this ...