Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Is This Good Programming Practice?

Just wondering if the following is considered to be good programming practice or not? I like to keep my individual source files as concise and uncluttered as possible, but I'm wondering what more experienced coders would think of it. I especially like the idea of the Settings.java class to keep all of my "Magic Numbers" in the one place. Has anyone any suggestions as to how I might improve on things?

Happy coding :-)

class ApplicationLauncher 
{
    public static void main(String[] args) 
    {
        SwingApplication mySwingApplication = new SwingApplication();
    }
}

//////////////

import javax.swing.*;

public class SwingApplication extends JFrame
{
    public SwingApplication()
    {       
        JFrame myJFrame = new JFrame();
        myJFrame.setSize(Settings.frameWidth, Settings.frameHeight);
        myJFrame.setVisible(true);      
    }
}

//////////////

class Settings 
{
    static int frameWidth = 100;
    static int frameHeight = 200;
}
like image 980
The Thing Avatar asked May 17 '09 20:05

The Thing


People also ask

What is good coding practices in Java?

5 Best Practices When Coding in JavaUsing Naming Conventions. Ordering Class Members by Scopes. Class Members should be private. Using Underscores in Numeric Literals.

Is Java good for anything?

Java can be used to create complete applications that can run on a single computer or be distributed across servers and clients in a network. As a result, you can use it to easily build mobile applications or run on desktop applications that use different operating systems and servers, such as Linux or Windows.


3 Answers

There's nothing wrong with having a settings class; however, in your example the settings are rather ambigious in terms of what frame they apply to, neither are they actual settings, but rather default values that strictly belong in the SwingApplication class.

Another thing which we don't have much control over is how the constructor call in Swing cascades into the program's message pump loop.

To me it has never made sense with a constructor that never returns (unless the frame is closed) and does more than initialize an object.

like image 192
Cecil Has a Name Avatar answered Oct 20 '22 08:10

Cecil Has a Name


Having special classes with magic numbers as static members is a good Java practice.

As programs grow, multiple settings classes, each with descriptive names, can be used.

like image 36
Macker Avatar answered Oct 20 '22 10:10

Macker


Some like to group all of this stuff, the magic numbers etc... in a big and ugly XML file which will be read (and made sense of) at runtime. Your approach is obviously okay for a small project (e.g. general coursework) but think about the obvious advantage of getting these Settings from an XML file: you will not need to recompile your source code in order to reflect changes made to your settings :)

like image 4
Peter Perháč Avatar answered Oct 20 '22 09:10

Peter Perháč