Quantcast
Viewing all articles
Browse latest Browse all 12749

Multiple methods or a single method (but long)

I'm doing a school project which consists of a client-server game.

Java code can be written like demonstrated in example 1 and example 2 :

In example 2 , I see the possibility to shorten the constructor by creating 3 private methods and calling them inside the constructor (Which IMHO looks a lot more neat).

Which example is best practice/recommended in Java?

Example 1: public ServerConnection() { private Socket serverSocket = null; private SSLSocket sslServerSocket = null; private DataOutputStream dos; private DataInputStream dis; private ObjectOutputStream oos; private ObjectInputStream ois; private DataOutputStream ssldos; private DataInputStream ssldis; private ObjectOutputStream ssloos; private ObjectInputStream sslois; try { serverSocket = new Socket("localhost", 9000); String password = "password"; KeyStore trustStore = KeyStore.getInstance("JKS"); trustStore.load(ClassLoader.getSystemResourceAsStream("SSL/mykey.jks"), password.toCharArray()); TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("SunX509"); trustManagerFactory.init(trustStore); SSLContext context = SSLContext.getInstance("TLS"); context.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom()); SSLSocketFactory sslSocketFactory = (SSLSocketFactory)context.getSocketFactory(); sslServerSocket = (SSLSocket) sslSocketFactory.createSocket("localhost", 9001); sslServerSocket.startHandshake(); dos = new DataOutputStream(serverSocket.getOutputStream()); dis = new DataInputStream(serverSocket.getInputStream()); oos = new ObjectOutputStream(serverSocket.getOutputStream()); ois = new ObjectInputStream(serverSocket.getInputStream()); ssldos = new DataOutputStream(sslServerSocket.getOutputStream()); ssldis = new DataInputStream(sslServerSocket.getInputStream()); ssloos = new ObjectOutputStream(sslServerSocket.getOutputStream()); sslois = new ObjectInputStream(sslServerSocket.getInputStream()); } catch (IOException | KeyStoreException | NoSuchAlgorithmException | CertificateException | KeyManagementException e) { System.out.println("ServerConnection 2 " + e.getMessage()); } } Example 2: public ServerConnection() { private Socket serverSocket = null; private SSLSocket sslServerSocket = null; private DataOutputStream dos; private DataInputStream dis; private ObjectOutputStream oos; private ObjectInputStream ois; private DataOutputStream ssldos; private DataInputStream ssldis; private ObjectOutputStream ssloos; private ObjectInputStream sslois; try { serverSocket = new Socket("localhost", 9000); keyMethod(); SSLContext context = SSLContext.getInstance("TLS"); context.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom()); SSLSocketFactory sslSocketFactory = (SSLSocketFactory)context.getSocketFactory(); sslServerSocket = (SSLSocket) sslSocketFactory.createSocket("localhost", 9001); sslServerSocket.startHandshake(); setStreams(); setSSLStreams(); } catch (IOException | KeyStoreException | NoSuchAlgorithmException | CertificateException | KeyManagementException e) { System.out.println("ServerConnection 2 " + e.getMessage()); } private void keyMethod() { String password = "password"; KeyStore trustStore = KeyStore.getInstance("JKS"); trustStore.load(ClassLoader.getSystemResourceAsStream("SSL/mykey.jks"), password.toCharArray()); TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("SunX509"); trustManagerFactory.init(trustStore); } private void setStreams() { dos = new DataOutputStream(serverSocket.getOutputStream()); dis = new DataInputStream(serverSocket.getInputStream()); oos = new ObjectOutputStream(serverSocket.getOutputStream()); ois = new ObjectInputStream(serverSocket.getInputStream()); } private void setSSLStreams() { ssldos = new DataOutputStream(sslServerSocket.getOutputStream()); ssldis = new DataInputStream(sslServerSocket.getInputStream()); ssloos = new ObjectOutputStream(sslServerSocket.getOutputStream()); sslois = new ObjectInputStream(sslServerSocket.getInputStream()); } }

Example 2 would be your best bet because it follows best practice guidelines, it is easier to read and in turn it makes your code cleaner. It is also ideal to turn code fragments into a method whose name explains the purpose of the method. There are a couple reasons why you would want short, well-named methods:

First, it increases the chances that other methods can use a method when the method is finely grained.

Second, it allows the higher-level methods to read more like a series of comments. Overriding also is easier when the methods are finely grained.

Take the example Martin Fowler gives on his blog:

Fragmented code: void printOwing() { printBanner(); //print details System.out.println ("name: " + _name); System.out.println ("amount " + getOutstanding()); } Refactored code: void printOwing() { printBanner(); printDetails(getOutstanding()); } void printDetails (double outstanding) { System.out.println ("name: " + _name); System.out.println ("amount " + outstanding); }

This also helps other people who might view your code to quickly understand what's going on.

You can check out more of Mr. Fowler's blog at Refactoring . Also see the SourceMaking blog on why you should extract your methods.


Viewing all articles
Browse latest Browse all 12749

Trending Articles