/Java_Args_Clean_Code

Apurvi Mansinghka (2019201093)

Primary LanguageJava

Name : Apurvi Mansinghka

RollNo. : 2019201093

This is the java version of the Args program described in: http://butunclebob.com/ArticleS.UncleBob.CleanCodeArgs

Code Explanation

User provides the input as command line argument as per the defined Schema. The Program extracts, sets the value of each datatype and displays all the values as output.

Schema:
 - char    - Boolean arg.
 - char*   - String arg.
 - char#   - Integer arg.
 - char##  - double arg.
 - char[*] - one element of a string ar ray.
 - char&   - give key:value elements of Map

Example schema: (f,s*,n#,a##,p[*],m&)
Coresponding command line: "-f -s Bob -n 1 -a 3.2 -p e1 -p e2 -p e3 -m aa:1,bb:2,cc:3

Features

  • Parse input and set values for different datatypes
    • Map data structure - [-char key:value] parses the key-value pair provided and stores the value into a hasmap.
    • String Array - [-char stringValue] add provided stringValue to string array .
    • Integer - [-char intValue] Set the default or user provided value to Integer variable.
    • Double - [-char doubleValue] Set the default or user provided value to double variable.
    • String - [-char stringValue] Set the default or user provided value to string variable.
    • Boolean - [-char ] sets the boolean variable to true.

Implemented following OOPs concepts:

  • Polymorphism - constructor overloading in all the classes to intialize the data members.
  • Abstraction - used an Interface class ArgsMarshaller to implement common functions in varios classes.
  • Modularity - Seperate classes to represent different entities and each method implements single functionality.
  • Encapsulation - Related data structures and functions in single unit.

Requirements and execution of code

Install/Update Java

  • sudo add-apt-repository ppa:openjdk-r/ppa
  • sudo apt-get update -q
  • sudo apt install -y openjdk-11-jdk

For The Main File

  • Clone this repo
  • install ant by running 'sudo apt-get install ant'
  • then go to the folder where you have cloned this repo
  • run 'ant compile'
  • run 'ant jar'
  • run 'java -cp build/jar/args.jar com.cleancoder.args.ArgsMain'

For the tests

  • Run the command given below from the root folder of this repo
  • 'java -cp "lib/junit-4.13.jar:lib/hamcrest-core-1.3.jar:build/jar/args.jar" ./test/com/cleancoder/args/ArgsTest.java testCreateWithNoSchemaOrArguments'

Characterstics of Clean Code included in code

  • Meaningful Naming- Methods named as verb+noun, class and data members named as noun or noun-phrase
  • Followed naming Convention for class, method, data members.
  • Followed proper indention of function, loops and classes.
  • Each Method serves only one functionality.
  • Functions are not more than 8-9 lines.
  • No function has more than 3 arguments. Most of the functions have 1 or 2 arguments, used 3 arguments only when it is necessary.
  • Each function validates its arguments by calling a validation function.
  • Each class uses constructors for data member initialization.
  • Each class has a default constructor if parameterized exists even if it is not used.
  • Vertical Alignment of function arguments.
  • Readable and self explanatory code with no useless comments.
  • Use of Optional class to handle null checking and handle nullpointer exception.
  • No duplication of methods and data members.
  • Encapsulation and abstraction feature of classes by declaring relevant private and public class members.
  • Objects hide their data behind abstractions and expose functions that operate on that data.
  • Exceptional handling using Try and catch
    • If a function has try keyword then is first keyword
    • there is nothing after the catch blocks.
    • try and catch blocks are made one liners by using functions
  • If else block are one liners mainly a function call.
  • No use of flag arguments.
  • Unit Test rules-
    • Fast.
    • Independent.
    • Repeatable.
    • Self validating
    • Timely

Bug Finds And Fixes

  • Invalid User Input : Original code allows two valid schema characters to be given as input.
    Example : [-np 5]
    Here 'n' is defined to take boolean input in schema and p is defined to take integer value, original code will set the integer value to 5 although it is an invalid input.
    Fix - Implemented a method to check the length of argumentType and return Invalid User Input Exception for wrong input.
  • Empty Key in Map : Original code allow user to give empty key value for Hashmap .
    Example : [-m :val1,key2:val2]
    Value 1 does not have key but original code consider empty string as key1.
    Fix - Provided a check of empty string before putting the value into map.
  • Input not starting with '-' : It is an invalid user input as user need to specify the datatype as -char. Original code was not providing a relevant check to invalidate such input.
    Example : [p 5]
    Fix - Implemented necessary checks while parsing the input and provided relevant error messages for Invalid User Input.