Annyce Davis

ones and zeros

  • Home
  • Public Speaking
  • About Me
  • Courses
  • Life

Bash Scripting for Flutter Tests and Code Coverage Reports

March 19, 2019 by Annyce Davis Leave a Comment

Do you have a development task that is mostly a series of commands? Do you have to perform that task on a regular basis? If so, a script might be just what you need. Over the years I’ve wanted to learn bash scripting several times. Yet there’s always something else that takes priority and my desire to script takes a back seat. Not any more!

I finally decided to start learning how to write bash scripts and I want to share it with you in case you’d like to do the same. At the bottom of this post, I’ve listed several resources that I’m using to help me on this journey. If you’re an Android developer, you can use Gradle to handle many automated tasks. If you want to learn about Gradle on Android or how to create a Gradle plugin, check out my course here.

On to the script. It does the following:

  • Runs the unit and widget tests for a Flutter application
  • Generates a code coverage report
  • Removes the unwanted files from the coverage output
  • Converts the coverage data to HTML
  • Opens the HTML page in your browser

I’m going to break down what each section of the script does so that you can start having fun creating your own scripts! (This post assumes you are already familiar with programming and related concepts.)

Continue Reading

Google I/O 2016 Recap

May 20, 2016 by Annyce Davis Leave a Comment

This was my second time attending Google I/O and it was once again a great experience. I put together a few highlights of the event below:

Venue

So the event took place at Shoreline Amphitheatre in Mountain View, California. It was my first time in Mountain View and it’s such a beautiful city. On Day 1 of the conference it was a bit “toasty” outside so that proved to be challenging, but eventually the remaining days were pleasant and breezy. The majority of the talks took place inside tents and there tended to be a lot of interest on the part of attendees.

Surprisingly I am told no developers have passed out from the heat yet at #GoogleIO2016 #io16 pic.twitter.com/YaDTNxL3Mn

— Ina Fried (@inafried) May 18, 2016

Attendees

For me one of the best parts of attending I/O this year was getting to meet so many of the people that I have interacted with online. Most of them were really cool people and managed to keep their spirits up despite putting up with the heat and long lines.

Most of the Android ladies I know of in the world. Who’s missing?!? #AndroidDev #io16 pic.twitter.com/in8KjFnVHS

— Corey Latislaw 🐞 (@corey_latislaw) May 18, 2016

Takeaways

I was completely impressed with the updates to Android Studio. Some of the things that I felt would be nice to haves, like showing Gradle dependencies clearly or having the Espresso Test Recorder were released. Definitely one of the major highlights for me.
 

The latest @androidstudio is amazing!!! Merged Manifests, better support for updating dependencies, and more. #io16 pic.twitter.com/3Chc5zJq84

— Annyce Davis (@brwngrldev) May 19, 2016

 

Something that surprised me though was how Firebase has become the de facto standard of all things mobile for Google. In fact, several products were renamed “Firebase X, Y, Z”. The following tweet summarizes it best:


#io16 so far… pic.twitter.com/AEIauhNekM

— John Mahoney (@jjmiv) May 20, 2016

 

Also there were several talks on developing applications for emerging markets. I especially enjoyed the one titled, “Building for Billions”. The speakers talked about the best strategies for making sure your application is always interactive and responsive for users regardless of network and memory conditions.

All in all, I am very thankful I was able to attend; I know that everyone doesn’t get this opportunity. I learned a lot, I drank a lot of coffee, and I met some awesome folks.



Make sure you subscribe to my newsletter to keep up with my Android Adventures. Until next year…

Conquering Cyclomatic Complexity

July 7, 2015 by Annyce Davis Leave a Comment

Have you ever received a warning about Cyclomatic Complexity while working with a class in Android Studio? Would you like to know how to fix it?  If yes, keep reading…

What is cyclomatic complexity?


Here is a definition from Wikipedia:

“The cyclomatic complexity of a section of source code is the number of linearly independent paths within it. For instance, if the source code contained no control flow statements (conditionals or decision points), such as IF statements, the complexity would be 1, since there is only a single path through the code.” — Wikipedia

Ok, so that particular definition is a tad verbose, but it’s essentially saying that the more control structures (if, else, switch, etc.)  you have in your code the more you introduce complexity into your program.

Why is high cyclomatic complexity bad?


Short answer, it can make it more difficult to write unit tests that cover all possible branches of a given function as well as hinder debugging efforts. Others argue that it affects the readability of your code, making it more challenging for another developer to understand the intent of your functions and/or classes.

How do you fix it?


You start by reducing the number of paths through any given function.  Let’s take a look at a very contrived example.

public void overlyComplexMethod(Video video) {
    if (video != null && video.getStreamUrl() != null) {
        switch (video.getCategory()) {
            case "CAT1" :
                playVideo(video);
                if (video.getLargeImageUrl() == null) {
                    video.setLargeImageUrl("http://www.largeImage.png");
                }
                updateMetadata(video);
                break;
            case "CAT2" :
                if (video.getLargeImageUrl() == null) {
                    video.setLargeImageUrl("http://www.smallImage.png");
                }
                updateMetadata(video);
                break;
            case "CAT3" :
                if (video.getLargeImageUrl() == null) {
                    video.setLargeImageUrl("http://www.mediumImage.png");
                }
                updateMetadata(video);
                break;
            default:
                break;
        }
    }
}


This function has several different paths through it, notice the use of a switch statement with several cases and within the cases more if statements. The cyclomatic complexity of this method is 9, ideally you would want to have most functions with a value of 8 or less.  So let’s clean it up!

First we’re going to move the switch statement into its own method.

public void overlyComplexMethod(Video video) {
    if (video != null && video.getStreamUrl() != null) {
        updateVideoBasedOnCategory(video);
    }
}

private void updateVideoBasedOnCategory(Video video) {
    switch (video.getCategory()) {
        case "CAT1" :
            playVideo(video);
            if (video.getLargeImageUrl() == null) {
                video.setLargeImageUrl("http://www.largeImage.png");
            }
            updateMetadata(video);
            break;
         case "CAT2" :
            if (video.getLargeImageUrl() == null) {
                video.setLargeImageUrl("http://www.smallImage.png");
            }
            updateMetadata(video);
            break;
         case "CAT3" :
            if (video.getLargeImageUrl() == null) {
                video.setLargeImageUrl("http://www.mediumImage.png");
            }
            updateMetadata(video);
            break;
          default:
            break;
    }
}


By making this simple change we’ve already reduced the complexity down to a value of 7. The next step would be to look for code duplication among the case statements and then create one method that they can all share.  Let’s see how that might look…

public void overlyComplexMethod(Video video) {
    if (video != null && video.getStreamUrl() != null) {
        updateVideoBasedOnCategory(video);
    }
}

private void updateVideoBasedOnCategory(Video video) {
    switch (video.getCategory()) {
        case "CAT1" :
            playVideo(video);
            updateVideoMetaDataAndUrl(video, "http://www.largeImage.png");
            break;
        case "CAT2" :
            updateVideoMetaDataAndUrl(video, "http://www.smallImage.png");
            break;
        case "CAT3" :
            updateVideoMetaDataAndUrl(video, "http://www.mediumImage.png");
            break;
        default:
            break;
    }
}

private void updateVideoMetaDataAndUrl(Video video, String url) {
    video.setLargeImageUrl(url);
    updateMetadata(video);
}

Now by extracting out this common method, updateVideoMetaDataAndUrl, we have reduced the cyclomatic complexity to 4. We could reduce this further by eliminating the need for a switch statement with polymorphism.  I leave that to you as an exercise.

Conclusion


As we can see it’s a simple refactoring effort to reduce the cyclomatic complexity of methods and by extension classes.  I would however like to mention that having unit tests in place before you begin the refactoring is ideal to ensure you don’t make any breaking changes.

If you want to learn more about writing clean code, I highly recommend the book Clean Code by Robert Martin. 

Android: IntelliJ IDEA/Android Studio Quick Tip

January 2, 2014 by Annyce Davis Leave a Comment

I’ve started using IntelliJ as my IDE of choice for developing backend APIs and Android applications.  One really nice feature, which is also available in Android Studio, is the option to create strings.xml values on the fly.  All you need to do is type the name of the desired string resource and then the “bulb” icon will appear asking you if you want to create that resource.  Super simple, but extremely convenient for those times when you are editing a lot of xml files in Android.

 

 

 

Follow Me

What engineering leaders need to know from this year’s Google I/O

I didn't intentionally aim to create a rhyming title, but there you have it. Each year, I look forward to Google I/O like a kid going back to school. This year … [Read More...]

Talk: The Real MVP

You have an idea for a new app. You've secured buy-in from the business. What's next? The MVP. But what does it take to ship a new app these days? What are the … [Read More...]

Categories

  • Android (55)
  • Career (2)
  • Communication (4)
  • Flutter (1)
  • Git (4)
  • Gradle (4)
  • Grails (23)
  • Java (8)
  • JavaScript (6)
  • Kotlin (17)
  • Life (4)
  • Public Speaking (23)
  • RxJava (1)
  • Software Development (7)
  • Twitter (3)
  • Uncategorized (11)
  • Video Course (5)

Copyright © 2023 · Beautiful Pro Theme on Genesis Framework · WordPress · Log in