Clean Coder Chapters 4 and 5

The Clean Coder Chapters 4 and 5 are all about coding and TDD (Test Driven Development), respectively. Reading these chapters gave me some insight on how to properly write clean code, and there were several parts of each chapter that intrigued me.

Chapter 4 discusses proper Coding techniques, and adds onto what I learned last year reading Clean Code by the same author. Although the chapter allowed me to evaluate my coding technique, I also found the 3 AM coding story quite hilarious. It doesn’t relate to any stories I would have, but at least it helps me understand better about the importance of a healthy, awake, and alert mind.

Chapter 5 introduces some tips for TDD. TDD has been around for ten years and is still a common practice to this day. The only thing I found interesting, though, was that TDD isn’t exactly useful in every situation. One can still write horrible code even if the tests are written first, especially if the tests are bad themselves.

These reads keep getting interesting as I continue, and I hope the next few chapters make some sense for when I step out into the real world. I hope I can also use these techniques while working on the OpenMRS code.

From the blog cs-wsu – jdongamer by jd22292 and used with permission of the author. All other rights reserved by the author.

CS Review: Strings

Tokenizers:

  1. StringTokenizer tokenizer = new StringTokenizer(“Example String Here”, delimiter (blank if space));
  2. while(tokenizer.hasMoreElements()){
  3.         Integer i = Integer.parseInt(tokenizer.nextElement().toString());
  4. }

Substring:

  1. String full = “Full Text”;
  2. String sub = full.substring(5,9);
  3. System.out.println(“Will print the word ‘Text’:  ” + sub);

Split:

  1. String full = “This is the full text”;
  2. String[] = full.split(” “); //or regular expression
  3. string.trim();

Character At:

  1. char c = string.charAt(7);

Replace:

  1. String s = “Hello”;
  2. s.replace(‘H’, ‘J’);//turns Hello to Jello
  3. s = “Hello”;
  4. s.replace(“Hello”, “Jello”);//also turns to Jello

Character Array:

  1. char[] cha =  new char[str.length()];
  2. for(int i=0;i<str.length();i++){
  3.         cha[i] = str.charAt(i);
  4. }
  5. //toString for printout

 

 

From the blog shatos by shatos and used with permission of the author. All other rights reserved by the author.

CS Review: Strings

Tokenizers:

  1. StringTokenizer tokenizer = new StringTokenizer(“Example String Here”, delimiter (blank if space));
  2. while(tokenizer.hasMoreElements()){
  3.         Integer i = Integer.parseInt(tokenizer.nextElement().toString());
  4. }

Substring:

  1. String full = “Full Text”;
  2. String sub = full.substring(5,9);
  3. System.out.println(“Will print the word ‘Text’:  ” + sub);

Split:

  1. String full = “This is the full text”;
  2. String[] = full.split(” “); //or regular expression
  3. string.trim();

Character At:

  1. char c = string.charAt(7);

Replace:

  1. String s = “Hello”;
  2. s.replace(‘H’, ‘J’);//turns Hello to Jello
  3. s = “Hello”;
  4. s.replace(“Hello”, “Jello”);//also turns to Jello

Character Array:

  1. char[] cha =  new char[str.length()];
  2. for(int i=0;i<str.length();i++){
  3.         cha[i] = str.charAt(i);
  4. }
  5. //toString for printout

 

 

From the blog shatos by shatos and used with permission of the author. All other rights reserved by the author.

CS Review: Input/Output

There are a few basic types of input/output sources that are usually used.

Buffered File In:

    1. String file = “file.txt”;
    2. FileReader fReader = new FileReader(file);
    3. BufferedReader bReader= new BufferedReader(fReader);
    4. String s = bReader.readLine();
    5. bReader.close();

Buffered File Out:

    1. String fileOut = “fileOut.txt”);
    2. FileWriter fWrite = new FileWriter(fileOut);
    3. BufferedWriter bWrite = new BufferedWriter(fWrite);
    4. bWrite.write(“Anything”, offset, length);
    5. bWrite.close();

Standard In (Usually a Keyboard):

    1.  InputStreamReader cin = new InputStreamReader(Sytem.in);
    2.  char c = (char) cin.read();

Standard Out (Console):

    1. System.out.print(“System.out is already setup”);

Scanners:

  1. Scanner in = new Scanner(System.in);
  2. int i = in.nextInt();

 

 

From the blog shatos by shatos and used with permission of the author. All other rights reserved by the author.

CS Review: Input/Output

There are a few basic types of input/output sources that are usually used.

Buffered File In:

    1. String file = “file.txt”;
    2. FileReader fReader = new FileReader(file);
    3. BufferedReader bReader= new BufferedReader(fReader);
    4. String s = bReader.readLine();
    5. bReader.close();

Buffered File Out:

    1. String fileOut = “fileOut.txt”);
    2. FileWriter fWrite = new FileWriter(fileOut);
    3. BufferedWriter bWrite = new BufferedWriter(fWrite);
    4. bWrite.write(“Anything”, offset, length);
    5. bWrite.close();

Standard In (Usually a Keyboard):

    1.  InputStreamReader cin = new InputStreamReader(Sytem.in);
    2.  char c = (char) cin.read();

Standard Out (Console):

    1. System.out.print(“System.out is already setup”);

 

 

 

From the blog shatos by shatos and used with permission of the author. All other rights reserved by the author.

OpenMRS Radiology Progress

I finally got the OpenMRS Radiology Module working with vagrant. On my first attempt with vagrant up, it seemed that I must have gotten some errors that prevented the OpenMRS module from correctly building, because although I could access tomcat, the OpenMRS module was not and could not be started. However, I simply deleted the directory, cloned again from git, and ran vagrant up again. I then ran vagrant ssh and changed the mysql password by entering mysql, then running

SET PASSWORD FOR ‘root’@’localhost’ = PASSWORD(‘MyPassword’);

Upon doing this, OpenMRS then worked. I am now working on getting dcm4chee working, as the default user/password of admin/admin specified in the readme does not appear to be working.

From the blog cs-wsu – Spencer Leal by spencerleal and used with permission of the author. All other rights reserved by the author.

Radiology Module Setup

Setting up the radiology module for OpenMRS seemed very easy at first. Cloning one repo from github and installing vagrant that does the hard work for us? Should be easy! Nothing is ever as easy as it seems. After running the vagrant up command to setup the vm and download all needed software to run openmrs I thought I was all set to start working on the first issue. Wrong! There was an error in which port was being used but after asking the question on the slack channel I found that my local install of MySQL there was an issue with the ports that it used conflicting with the ports for the vm. I did not need MySQL installed locally as my focus went from local install of openmrs to the vm so I did not need it anymore. Thinking that this fixed the issue There was a timeout error not allowing the dcm4chee to not be a recognized command in the vm. So as of now I have got a solution through the slack channel on what to do to try to fix this error. I have learned that vagrant is a very useful tool in setting up vms and can have many good applications such as what we are doing; setting up the a test environment on many machines.

From the blog CS@worcester – Greg Tzikas by Greg Tzikas and used with permission of the author. All other rights reserved by the author.

Clean Coder Chapter 5: TDD

Test Driven Development is something that Martin thinks that all programmers should be using in the design of their code. Since this method is only something mentioned to me and done for a course and not implemented in my way of writing code it is something that I am skeptical of adapting and trying to use. After hearing what it does, how it works, and practicing it in a previous course it looks like the best way to approach coding. The main struggle I have with it is actually going about implementing it in my coding routine. I have learned to write code create an input and see if you get what you want. If that fails put some print statements in the portions of the code you think it is failing and trace back through it and try to fix it. After some mess it finally gets fixed but for the assignment to work and be run correctly. That is the problem when it is code that we are having to only do for an assignment we are not concerned with how it gets done, as long as it gets done. I think working for the last month of the semester with actual code in the Radiology module of OpenMRS will allow us to implement TDD as we need to create test cases for the code we create. This could allow for TDD to be used and possibly make writing of the code a lot easier.

From the blog CS@worcester – Greg Tzikas by Greg Tzikas and used with permission of the author. All other rights reserved by the author.

Clean Coder Chapter 5: TDD

Test Driven Development is something that Martin thinks that all programmers should be using in the design of their code. Since this method is only something mentioned to me and done for a course and not implemented in my way of writing code it is something that I am skeptical of adapting and trying to use. After hearing what it does, how it works, and practicing it in a previous course it looks like the best way to approach coding. The main struggle I have with it is actually going about implementing it in my coding routine. I have learned to write code create an input and see if you get what you want. If that fails put some print statements in the portions of the code you think it is failing and trace back through it and try to fix it. After some mess it finally gets fixed but for the assignment to work and be run correctly. That is the problem when it is code that we are having to only do for an assignment we are not concerned with how it gets done, as long as it gets done. I think working for the last month of the semester with actual code in the Radiology module of OpenMRS will allow us to implement TDD as we need to create test cases for the code we create. This could allow for TDD to be used and possibly make writing of the code a lot easier.

From the blog CS@worcester – Greg Tzikas by Greg Tzikas and used with permission of the author. All other rights reserved by the author.

Clean Coder Chapter 4:Coding

In chapter 4 Martin brought up many great ideas about the environment and attitude you have while coding. Early on in college I would often have music playing and have my phone next to me. I thought that it never distracted me until reading this chapter. I slowly have pulled away from listening to music or having distractions but after reading this chapter I realize that maybe it is not the best idea to have distractions on in the background that put you into a “zone” that allows you to just blow through code and problems. This leads to poor and sloppy work. While it is nice to breeze through something, more likely than not finishing a chunk of code or a problem is not as easy as taking one look and making a change. It takes time to create it and ensure that it is functioning and done in an efficient manner rather than spaghetti code. Often when things are running in the background or you are distracted because of an event that happened or is going to happen the bad code comes into play. This is because your mind isn’t giving full attention to what you are trying to get done. Doing so results in more problems down the road. The books explanation of 3am code and when to walk away reminds me of the casino. When you are there and winning(focused state) you are good you are having a good time and excited. Good code should do the same but once you start losing money and are down(unfocused/distracted) you start to play risky to try to win your money back. This is when you need to learn to step away, go outside and take a walk or just step away from everything and clear your mind. I think this is a huge thing that I myself should learn as I find the best work I do is after a few hours after I wake up. Myself working or thinking too much about something can stress me out and make me frustrated only hunting and thinking about the correct answer. Luckily being in college still with other classmates it is easy to practice and work together with others. Many times something I am just missing that is not obvious to me is obvious to someone else and we can help each other and vice versa. I think that programming with someone else and getting and receiving help is the best way to learn more. There is just so much to know about programming and given where we are in our careers it is very hard to know every little detail so some students know more about certain things and can help. I have heard that this is also true in the industry and I hope to continue having people around to program with to help and be helped by. I think in the end it comes down to practice. The more you practice the better you will become allowing times where you are stuck to recognize similar situations which when you were stuck before. It may not always be the same scenario but knowing similar problems can solve endless hours of stress and problem solving just by practicing.

From the blog CS@worcester – Greg Tzikas by Greg Tzikas and used with permission of the author. All other rights reserved by the author.