View on GitHub

Kenneth V.

Personal Website for CSE 15L

back to main page

Lab Report 4 - Week 8

Clean Coding Practice

Alrighty, hey guys! This is the fourth lab report for CSE 15L. There aren’t any new concepts that are game-changing that will be discussed this time, but it’s all about refining and cleaning up your code.

What does cleaning up your code mean? In short, it means to make your code more easy to read and understand. In the efforts of making your code more better, it’s not all about adding more safety measures to make sure your code works, but rather to make the code more satisfying and appealing to people who do want to peruse your code.

Simply, clean code is code that clearly displays its intended purpose and that is is simple.

Below, I will test 3 snippets to another person’s implementation of MarkdownParse with my own implementation. These snippets deal with the markdown syntax when using backticks.

 

Snippet 1

`[a link`](url.com)

[another link](`google.com)`

[`cod[e`](google.com)

[`code]`](ucsd.edu)

The first snippet here shows a clear issue with using the backticks syntax within a link format. In this case, when running MarkdownParse.java, it should produce a list of:

["google.com"]

Everything else (besides the third line) in the snippet above clearly violates the markdown syntax for a link.

Testing the Snippet

    @Test
    public void snippet1() {
        List<String> expect = List.of("google.com");
        try {
            ArrayList<String> links = MarkdownParse.getLinks(readFile("snippet1.md"));
            assertEquals(links, expect);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

Within MarkdownParseTest.java, here is the code that I will be using to see whether or not my implementation and someone else’s implementation will work.

My Implementation

After running the file to my implementation, it seems like my MarkdownParse didn’t work as expected:

Image

Other Implementation

After running the file to someone else’s implementation, it also failed:

Image

Clean Code Change

Overall, if I had to improve on the code by adding a small code change (as per clean code guidelines), I think it may likely be possible to deal with the backtick syntax issues with markdown. For example:

Snippet 2

[a [nested link](a.com)](b.com)

[a nested parenthesized url](a.com(()))

[some escaped \[ brackets \]](example.com)

For the second snippet, it deals with nested formatting potential issues with markdown. In this case, when running MarkdownParse.java, it should produce a list of:

[]

If it doesn’t look like an obvious valid link, it’s not a valid link.

Testing the Snippet

    @Test
    public void snippet2() {
        List<String> expect = List.of();
        try {
            ArrayList<String> links = MarkdownParse.getLinks(readFile("snippet2.md"));
            assertEquals(links, expect);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

Within MarkdownParseTest.java, above is the next test for snippet 2.

My Implementation

After running the tester, it looks like it passed!

Image

Other Implementation

After running on someone else’s implementation, their tests for MarkdownParse.java seems to have failed!

Image

Clean Code Change

So, how would I clean up my code with a small change? To be honest, my implementation passed so it’s good enough. But, if I had to, one minor tweak would be to make the code more simple or to make it look not as cluttered. Besides that, any other changes would focus on the brackets and the parentheses when dealing with nested stuff.

Snippet 3

[this title text is really long and takes up more than 
one line

and has some line breaks](
    https://www.twitter.com
)

[this title text is really long and takes up more than 
one line](
https://sites.google.com/eng.ucsd.edu/cse-15l-spring-2022/schedule
)


[this link doesn't have a closing parenthesis](github.com

And there's still some more text after that.

[this link doesn't have a closing parenthesis for a while](https://cse.ucsd.edu/



)

And then there's more text

For the third and last snippet, this deals with a ton of spaces where the parentheses/brackets are in different line. When we run MarkdownParse.java, it should produce a list of:

["https://www.twitter.com", "https://sites.google.com/eng.ucsd.edu/cse-15l-spring-2022/schedule"]

The link format should have adjacent syntax that doesn’t have empty spacing.

Testing the Snippet

    @Test
    public void snippet3() {
        List<String> expect = List.of("https://www.twitter.com", "https://sites.google.com/eng.ucsd.edu/cse-15l-spring-2022/schedule");
        try {
            ArrayList<String> links = MarkdownParse.getLinks(readFile("snippet3.md"));
            assertEquals(links, expect);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

Above is the snippet 3 test that I’ll be using to check if the outputs match for MarkdownParse.java.

My Implementation

After running the tester, it looks like it passed too!

Image

Other Implementation

After running on someone else’s implementation, their tests for MarkdownParse.java seems to have failed by a huuuuge margin:

Image

Clean Code Change

In making a small code change in my implementation for the basis of clean code, I think it’s good enough. There doesn’t seem to be much to change, unless we want the empty spaces within a file to be considered also. My implementation focuses on links that are formatted next to each other. As long as they are within the next line of each other, it’s okay. But if there’s an empty white space in-between, that doesn’t count as a valid link. Besides that it may be possible to shorten or add some code to make it more better.

 

back to main page