Nikola Brežnjak blog - Tackling software development with a dose of humor
  • Home
  • Daily Thoughts
  • Ionic
  • Stack Overflow
  • Books
  • About me
Home
Daily Thoughts
Ionic
Stack Overflow
Books
About me
  • Home
  • Daily Thoughts
  • Ionic
  • Stack Overflow
  • Books
  • About me
Nikola Brežnjak blog - Tackling software development with a dose of humor
Android

Calling Android Devs Who Hate Spammers

TL;DR

We are looking to hire a new senior Android mobile app developer that would help our team in getting revenge on the spammers and telemarketers.

If you want to help us fight the good fight, please apply here.

!TL;DR

I’m happy to announce that we’re expanding our mobile team here at Teltech and this time we’re searching for a senior Android mobile app developer that would help our team in getting revenge on the spammers and telemarketers.

This is your chance to join a dynamic environment where you get to hone your software development skills alongside other experienced and passionate developers. At TelTech, we make software that is used by oodles of people around the globe, and every team member is crucial to our success in making our customers happy. We’ve been doing this since 2008 when we published one of the first 100 iPhone apps EVER!

We pride ourselves in our relaxed, flexible culture, where you are treated like a member of the family. We have an inspiring building, perks like free food and games, and, most importantly, good people who are always looking to have fun. We strive to provide a place where you will happily balance your work with the rest of your life for years to come.

If you’re the kind of person who thinks outside the box to solve hard technical challenges, loves learning new things, loves sharing what you know, and is passionate about making great software, this could be the place for you!

Using cutting-edge technologies, you will:

  • Improve our existing and launch new mobile apps for Android.
  • Contribute to our Web APIs for products and services.
  • Quickly prototype new ideas for market and user evaluation.
  • Mentor others to improve their mobile and general software development skills.

We look for those who don’t let themselves be pigeonholed into a specific list of functions and are willing to help out wherever there is a need.

Here’s what we expect you have:

  • 4+ years of Android experience developing consumer-facing apps
  • Strong Java skills with extensive knowledge of the latest versions of Android
  • Experience in taking a design file (e.g. Sketch, Zeplin, Photoshop) and bringing it to life on Android
  • Experience using Web RESTful APIs
  • Demonstrated success in defining and launching mobile apps
  • A positive, ‘get stuff done’ attitude and passion for learning and making great apps

If on top of the above traits, you also have one of these, it’s a plus for you:

  • Experience with telecommunications technologies such as SIP, VoIP, IVR, etc.
  • Experience with using Kotlin
  • Microservices and/or socket communications experience
  • Experience with using Protocol buffers
  • UI test automation experience, including TDD
  • UI/UX design sense

What about iOS!?

If you’re intrigued by the problems, we’re solving and would like to help out, but you’re ‘only’ focused on iOS fret not. Even though we’re not actively looking, we’re always open to establishing a connection.

So, feel free to reach out to me in either case.

Where do I sign up?

You can send your resume to my email, or via this link.

Books

Leading Snowflakes by Oren Ellenbogen

These are my notes from an awesome and more importantly practical book called Leading Snowflakes by Oren Ellenbogen. This is a very good book if you just moved from a developer role to Lead or Manager.

Switch between Manager and Maker models

  • NSC! – Never stop coding!
  • Schedule Maker and Manager times on your calendar

No one is being promoted to a managerial position to increase their productivity. We already proved our capabilities there. Our job as managers is to amplify our teammates. Our job is to ask the right questions, to encourage people to think, to challenge, to inspire.

The only way we can keep our edge is by continuously practicing our Maker skills, even if it’s on a smaller scale. Make it easy to pick small tasks you can take. If you’ve got 3 hours to be productive, it would feel counter-productive to use this time to think of tasks instead of making them happen.

Code Review Your management decisions

  • WHEN/WHO/THE DILEMMA/THE DECISION MADE/RETROSPECTION/DID I SHARE THIS?
  • discuss these dilemmas with your boss and see how he would have handled it
  • discuss dilemmas with another engineering manager and review each other decisions
  • do a retrospection every day for ten mins
  • do a retrospection once a month for 1 hour

People quit their boss, not their job

Receiving constant feedback is the fastest way to learn

Confront and challenge your teammates

  • email summaries of your 1on1’s
  • care deeply about your team but don’t care about what they think of you
  • The Asshole Checklist: a) Did I show empathy (not sympathy) b) Did I clarify my expectations c) Did I practice what I just preached
  • share harsh feedback if needed no matter what
  • don’t go helping others with their tasks
  • share your failures with the team

If you want to achieve anything in this world, you have to get used to the idea that not everyone will like you.

I used to send a summary of my conversations with my teammates including my concrete examples and recommended steps. Then, I added how I understood the other side and the next action items we agreed on. I asked them to reply back to this email with their feedback if I got it wrong or they have anything else to add. That email was archived (I had a label per teammate), and used as a way to congratulate them on their progress or to figure out together why a particular pattern keeps emerging.

Not making a decision is equally important and explicit as making one

We also have to keep in mind that bored people quit. Engineers want to improve and get better over time. They want to learn new techniques. They want to tackle harder problems. They want to gain the respect of their peers and the organization they’re a part of. They enjoy new challenges; they appreciate different solutions. It’s our responsibility to help them grow. If we’re not giving our teammates feedback and challenging them, they will become unhappy (and bored) and will eventually leave.

Teach how to get things done

  • show how something is done
  • your job is to help someone else succeed
  • small increments (1-3hrs per task)
  • write docs after the feature is finished
  • see if you can have someone teach others

A great way to amplify teaching is by showing someone else how to get things done, rather than telling them what should be done

Managers should exist for only one reason. To help those who work underneath them be successful and succeed.

Delegate tasks without losing quality or visibility

  • make a list of things you’re doing today and see which ones you can start delegating
  • use the spreadsheet from the book
  • use the one-pager template from the book

Ask yourself two questions for each task:

  • Does it utilize my unique strength and responsibilities as a manager?
  • Does it serve the leader I want to become in the long-run (does it push me out of my comfort zone)?

The idea is to define only what we expect to see happening and not how we expect them to get there

Build trust with other teams in the organization

  • in the priority meetings list three priorities that are most important for you in the next week, but acknowledge that you know what is expected of you
  • TEAM is a group of people that TRUST each other
  • There is no I in TEAM
  • ‘Thank you’ email (or public praise) for a feature well done
  • Internal tech talks
  • Cross-team exchange program
  • Tracking our team’s output in a simple spreadsheet
  • Pizzability

Sitting down with other teams and watching how our product is being used could create a better understanding of the value we produce

Premature optimization is the root of all evil – it is the same logic we should apply when we are optimizing our process.

Don’t fall in love with the solution you’ve built, but with the problem, you’re trying to solve

Automated Testing can decrease the time to deliver each feature and so increase efficiency

Optimize for business learning

Startup Metrics for Pirates: AARRR!:

  • Acquisition
  • Activation
  • Retention
  • Referals
  • Revenue

Spend 80% on tweaking features and 20% on developing new features.

Technical Debt is less scary than getting out of business

Companies fail when they stop asking what they would do if they were started today, and instead just iterate on what they’ve already done.

Use inbound recruiting to attract better talent

  • Answer questions on StackOverflow for 1hr/week
  • Guest blog posts or repost your posts on Medium or dev.to
  • Have hackathons
  • Birthday pics to Instagram, Facebook, Blog. Also do a video maybe
  • Give talks
  • Have side projects

It’s so hard to find great employees these days – everyone, all the time.

Instagram talked about their unique infrastructure and lessons learned a year before they were acquired by Facebook for 1 billion dollars.

Make something people want includes making a company that people want to work for. — Sahil Lavingia

The goal of a successful Inbound Recruiting tactic is to cause a wow factor for our future candidate. The best case scenario would be that this person will sit at home, read our content or play with our project and say oh wow, that’s amazing! I wish I could work there.

Build a scalable team

  • Vision
  • Core values
  • Self-balanced
  • Sense of accomplishment
  • Who is putting down fires? – get him to distribute knowledge and mentor others to get the job done
  • Who is an expertise bottleneck – same as above
  • Who is not building trust?
  • On your 1on1’s ask “What is the worst thing about working here?”

Own it. Never let someone else fix our mess: we understand that sometimes it’s not our fault, but we don’t believe in making excuses and pointing fingers. We believe in getting things done and take full ownership of our deliveries

If someone is kicking ass, it’s our job to recognize and celebrate it together. Most chances are, we’ll see each other on a daily basis more than we see our families. Let’s make sure we enjoy each other’s companionship.

Passion to learn & get better every single day is an agile state of mind: constant feedback, constant improvement, baby steps for large change.

Books

Coaching for performance – Sir John Whitmore

Here are my notes from the book Coaching for performance by Sir John Whitmore.

Coaching focuses on future possibilities, not past mistakes.

Coaching is unlocking peoples potential to maximize their own performance. It is helping them to learn rather than teaching them.

If blame and criticism are a prevalent communication style and this doesn’t change, relationship failure can be predicted with over 90% accuracy.

Of the spoken word: 7% in words, 38% in the way the words were said, 55% facial expression.

I think this person:
– Is a problem
– Has a problem
– Is on a learning journey and is capable, resourceful and full of potential

Intentionality – set a clear intention for a meeting. 2 min before a meeting: “if the meeting would wildly exceed your expectations, what would happen?” No limits!

Questions for building a great team:
– What would the dream or success look for us working together?
– What would the worst case look like
– What’s the best way for us to achieve success or dream
– What do we need to be mindful of to avoid the worst case
– What permissions each of us wants from each other
– What will we do when things get hard?

Open questions:
– What do you want to achieve
– What’s happening at the moment
– How would you like it to be
– What’s stopping you
– What’s helping you
– What problems might there be
– What can you do
– Who can help you
– Where can you find out more
– What will you do

Don’t ask WHY question => ask “what were the reasons” instead

Don’t ask HOW question => ask “what are the steps” instead

Top 10 questions in coaching:
– If I wasn’t here what would you do
– If you knew the answer, what would it be
– What if there were no limits
– What advice would you give to your friend in the same situation
– Imagine having a dialog with the wisest person you know; what would they tell you to do
– What else?
– What would you like to explore next
– I don’t know where to go next, where would you like to go
– What is the real issue
– What is your commitment on a scale from 1 to 10 to doing it. What can you do in doing it

1on1s:
– Goal setting for the sessions (both long and short-term)
– What do you want?
– Reality check to explore the current situation
– Where are you now?
– Options and alternative strategies
– What could you do?
– Will – What is to be done, when, by whom, and a will to do it
– What will you do?

SMART, PURE and CLEAR goals

Having a big dream brings as much work as a small dream.

To set up accountability:
– What will you do
– When
– How will I know

70 learning through experience on the job
20 learning from other people
10 formal learning

Feedback framework:
– What happened
– What have you learned
– How to use this knowledge in the future

Discussion may be facilitated by the team leader but what happens should be decided by team members.

Bonding opportunity by doing something together.

Our true values reside within us and at the deepest level, those values are universal.

Vue.js

Getting started with Vue.js 2 by building a Giphy search application

TL;DR

This tutorial follows the same pattern as this Angular 2 post. You’ll learn how to use Vue CLI to build a Vue.js 2 application for searching Giphy’s gifs by using their API.

The main reason for making this post was to evaluate a bit more lightweight solution than Angular or React.

I’m using Vue.js 2, and I’ll be referring to it as just Vue.js in the rest of this post.

Introduction

Let’s be honest, even those of us that don’t actually hate the JavaScript ecosystem can understand the frustration with the fact that new JS frameworks are popping up all the time.

I stuck to the Angular bandwagon ever since version 1.0. Even though I would recommend starting a new SPA project with it, I must say that my recent research led me to believe that due to Vue’s progressive and flexible nature, it’s a perfect fit for the teams that have to rewrite old codebases one step at a time.

If you want to check out some detailed comparisons between popular frontend frameworks, here are two great posts:

  • Angular vs. React vs. Vue: A 2017 comparison
  • Vue.js vs. React, Angular, AngularJS, Ember, Knockout, Polymer, Riot

General ‘framework war’ kind of questions I tend to answer in the following way:

Please just stop with the analysis paralysis already.

Do your research, pick a framework (any framework for that matter) that the community is using, use it, and see how far it gets you.

All these talks about X being slow or Y being better just make no sense until you try it yourself for your use case and preference. Besides, nowadays the speed will not be a deciding factor among the top JS frameworks.

With all this said, fasten your seatbelts, take a venti (or trenta) sized cup of coffee, and let’s go!

Demo app

As said, we’ll build an application for searching (and showing) gifs from the Giphy website by using their API.

You can fork the complete source code on Github.

Prerequisites

Make sure that you have the following tools installed:

  • Node.js – step by step guide for both Windows and Mac
  • Git – a fun getting started tutorial

Vue CLI

As their README says:

Vue CLI aims to be the standard tooling baseline for the Vue ecosystem. It ensures the various build tools work smoothly together with sensible defaults so you can focus on writing your app instead of spending days wrangling with configurations. At the same time, it still offers the flexibility to tweak the config of each tool without the need for ejecting.

To installing vue-cli, run:

npm install -g @vue/cli

You can confirm that the installation went well if you run:

vue --help

⚠️ Just for reference (in case you follow this tutorial at a later stage and something is not the same as I output it here), my version (vue --version) as of this writing is 2.9.3.

Starting a new app with Vue CLI

We’ll call our app, originally, giphy-search. So, let’s start a new app using vue-cli:

vue init webpack giphy-search

You should get an output similar to this:

# nikola in ~/DEV/Vue [13:37:99]
? Project name giphy-search
? Project description Giphy search app with Vue.js
? Author Nikola Brežnjak <[email protected]> // I welcome spam ;)
? Vue build standalone
? Install vue-router? No
? Use ESLint to lint your code? Yes
? Pick an ESLint preset Standard
? Set up unit tests No
? Setup e2e tests with Nightwatch? No
? Should we run `npm install` for you after the project has been created? (recommended) npm

   vue-cli · Generated "giphy-search".

# Installing project dependencies ...

npm notice created a lockfile as package-lock.json. You should commit this file.
added 1349 packages in 28.159s

Running eslint --fix to comply with chosen preset rules...
# ========================

> [email protected] lint /Users/nikola/DEV/TeltechRepos/FrontendEvaluation/Vue/giphy-search
> eslint --ext .js,.vue src "--fix"

# Project initialization finished!
# ========================

To get started:

  cd giphy-search
  npm run dev

Documentation can be found at https://vuejs-templates.github.io/webpack

After this command finishes, let’s cd into the project and run it:

cd giphy-search
npm run dev

You should get:

DONE  Compiled successfully in 3745ms

Your application is running here: http://localhost:8080

You should see the following page in your browser if you visit this link: http://localhost:8080.

Folder structure

Now, let’s open this project in the editor of your choice (I’m using Visual Studio Code), and you should see something like this:

As I said, this is an introduction tutorial to get you running fast so I won’t be going into any specific details this time (this is up for some other posts), so here we’ll only focus on the src folder. The contents of that folder should be something like this:

Adding content

Ok, so, let’s add something to our app.

But, where to start?

Well, one of the first things I do when I come to a project for the first time is look at the generated output. Then I try to find the strings corresponding to that output within the source code.

So, if you search for the string Welcome to Your Vue.js App, you’ll see the string is within the HelloWorld.vue file. This file contains the following (... is used for brevity in the listing below):

<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <h2>Essential Links</h2>
    <ul>
      ...
    </ul>
    <h2>Ecosystem</h2>
    <ul>
      ...
    </ul>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App'
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
  font-weight: normal;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>

Without knowing anything about Vue.js, we can see where we would change the Welcome to Your Vue.js App text. So, let’s change that to Welcome to GiphySearch. While you do that, also remove the contents of the style tag.

Adding input and a button

Our basic application should have one input field and one button.

To do this, add the following code to the HelloWorld.vue file:

<input name="search">

<button>Search</button>

Actions

Having a simple search input field and a button doesn’t help much. We want to click the button, and we want to output something to the console just to verify it’s working correctly.

So, this is how we define a function that will handle button click in Vue:

<button @click="performSearch">Search</button>

But, now if open up your DevTools, you’ll see an error like:

webpack-internal:///./node_modules/vue/dist/vue.esm.js:592 [Vue warn]: Property or method "performSearch" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option or for class-based components, by initializing the property. See https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

found in

---> <HelloWorld> at src/components/HelloWorld.vue
       <App> at src/App.vue
         <Root>

That’s because we haven’t defined the performSearch function anywhere.

Let’s do that now. In the HelloWorld.vue file, add the following function definition inside the script tag, methods object property:

<script>
export default {
  name: "HelloWorld",
  data() {
    return {
      msg: "Welcome to GiphySearch"
    };
  },
  methods: {
    performSearch: function() {
      console.log("clicked");
    }
  }
};
</script>

With this, we defined the performSearch function, which doesn’t accept any parameters and it does not return anything.

Taking input

What if we would like to print to the console the string that someone typed in the input field?

Well, first we need to add a new attribute to the input field:

<input name="title" v-model="searchTerm">

The v-model is a directive that instructs Vue to bind the input to the new searchTerm variable.

Finally, change the performSearch function to this:

performSearch: function() {
    console.log(this.searchTerm);
}

Giphy search API

Finally, we come to the cool part, and that is to fetch some data from the service and to show it in our app (in our case, we’ll show images).

So, how do we get this API? Well, if you do a simple Google search for giphy api and open the first link you’ll get the documentation for their API.

We need the search API. If you scroll a bit, you’ll find the following link:

http://api.giphy.com/v1/gifs/search?q=funny+cat&api_key=dc6zaTOxFJmzC

Great, now we see what kind of a request we need to create to search Giphy’s gif database for a certain term.

If you open this link in the browser, you’ll see what the service returns. Something like:

In the next section, we’ll cover retrieving this data from within our app.

Vue.js HTTP requests

We have several options for doing HTTP requests in Vue.js, and some may see this flexibility as a pro and some may see it as a con. Either way, for more options, check out this post.

I chose to go with Axios, as it’s a very popular JavaScript library for making HTTP requests. It’s an HTTP client that makes use of the modern Promises API by default (instead of the ~~ugly~~ not so nice JavaScript callbacks) and runs on both the client and the server (i.e. Node.js).

In your Terminal/Command prompt enter the following command to install axios via npm:

npm install axios --save

Import axios in the HelloWorld.vue file just after the script tag:

import axios from "axios";

The performSearch function should now look like this:

var link = "http://api.giphy.com/v1/gifs/search?api_key=dc6zaTOxFJmzC&q=";
var apiLink = link + this.searchTerm;

axios
.get(apiLink)
.then(response => {
    console.log(response);
})
.catch(error => {
    console.log(error);
});

Just for reference, to put it all in one listing, the contents of the HelloWorld.vue file should be:

<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <input name="title" v-model="searchTerm">

    <button @click="performSearch()">Search</button>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "HelloWorld",
  data() {
    return {
      msg: "Welcome to GiphySearch"
    };
  },
  methods: {
    performSearch: function() {
      console.log(this.searchTerm);

      var link = "http://api.giphy.com/v1/gifs/search?api_key=dc6zaTOxFJmzC&q=";
      var apiLink = link + this.searchTerm;

      axios
        .get(apiLink)
        .then(response => {
          console.log(response.data);
        })
        .catch(error => {
          console.log(error);
        });
    }
  }
};
</script>

Now, if you run the app, enter something in the search box, and click the search button, you’ll see something like this in your console log:

You can see that we’re getting back the response object and that in its data property there are 25 objects, which hold information about the images that we want to show in our app.

And, well, this is all great now, but we don’t want to be logging out our objects to the console, we want to show them in our app.

To show the image, we need to position ourselves on the object images, then original, and finally on the url property.

Also, we don’t want to just show one image but all of the images. We’ll use the v-for directive for that:

<img v-for="g in giphies" :key="g.id" src="{{g.images.original.url}}">

For reference, here’s the full listing of HelloWorld.vue file:

<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <input name="title" v-model="searchTerm">

    <button @click="performSearch()">Search</button>

    <div>
        <img v-for="g in giphies" :key="g.id" :src="g.images.original.url">
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "HelloWorld",
  data() {
    return {
      msg: "Welcome to GiphySearch",
      searchTerm: "cat",
      giphies: []
    };
  },
  methods: {
    performSearch: function() {
      console.log(this.searchTerm);

      var link = "http://api.giphy.com/v1/gifs/search?api_key=dc6zaTOxFJmzC&q=";
      var apiLink = link + this.searchTerm;

      axios
        .get(apiLink)
        .then(response => {
          this.giphies = response.data.data;
        })
        .catch(error => {
          console.log(error);
        });
    }
  }
};
</script>

At this point, if we take a look at the app and search for example for ‘cat coding’ we’ll get this:

Although the result doesn’t look sleek, our code does exactly what it’s supposed to do. If you want it to look nicer, feel free to add more CSS.

Bulma is a cool framework that I used recently in the JWT authentication in an Angular application with a Go backend post.

Conclusion

In this tutorial, you learned how to get started with using Vue.js by building an application for searching Giphy’s gifs by using their API.

Please leave any comments and feedback in the discussion section below!

Thank you for reading!

Getting started with #Vue.js 2 by building a #Giphy search application https://t.co/AV1kjK6C1m

— Nikola Brežnjak (@HitmanHR) March 26, 2018

Programming

Code Complete 2 – Steve McConnell – Table-Driven Methods

I just love Steve McConnell’s classic book Code Complete 2, and I recommend it to everyone in the Software ‘world’ who’s willing to progress and sharpen his skills.

Other blog posts in this series:

  • Part 1 (Chapters 1 – 4): Laying the Foundation
  • Chapter 5: Design in Construction
  • Chapter 6: Working Classes
  • Chapter 7: High-Quality Routines
  • Chapter 8: Defensive programming
  • Chapter 9: Pseudocode Programming Process
  • Chapter 10: General Issues in Using Variables
  • Chapter 11: General Issues in Using Variables
  • Chapter 12: Fundemental Data Types
  • Chapter 13: Unusual Data Types
  • Chapter 15: Using Conditionals
  • Chapter 16: Controlling Loops
  • Chapter 17: Unusual Control Structures

Table-driven code can be simpler than complicated logic using ifs. Suppose you wanted to classify characters into letters, punctuation marks, and digits; you might use a logic like this one (Java):

if ( ( ( 'a' <= inputChar ) && (inputChar <= 'z' ) )  || ( ( 'A' <= inputChar ) && (inputChar <= 'Z' ) ) ) {
    charType = CharacterType.Letter;
}
else if ( ( inputChar == ' ' ) || ( inputChar == ',' ) ||( inputChar == '.' ) || ( inputChar == '!' ) || 
    ( inputChar == '(' ) || ( inputChar == ')' ) || 
    ( inputChar == ':' ) || ( inputChar == ';' ) ||
    ( inputChar == '?' ) || ( inputChar == '-' ) ) {

    charType = CharacterType.Punctuation;
} else if ( ( '0' <= inputChar ) && ( inputChar <= '9' ) ) {
    charType = CharacterType.Digit;
}

Two Issues in Using Table-Driven Methods:

  • how to look up entries in the table
  • what to store in the table
    • the result of table lookup can be data, but also an action. In such case, you can store a code that describes the action, or in some languages, you can store a reference to the routine that implements the action. In either of these cases, tables become more complicated.

How to look up entries in the table

You can use some data to access a table directly. If you need to classify the data by month, for example, you can use an array with indexes 1 through 12.

Other data is too awkward to be used to look up a table entry directly. If you need to classify data by Social Security Number, for example, you can’t use the Social Security Number to key into the table directly unless you can afford to store 999-99-9999 entries in your table. You’re forced to use a more complicated approach. Here’s a list of ways to look up an entry in a table:

  • Direct access
  • Indexed access
  • Stair-step access

Direct Access Tables

Suppose you need to determine the number of days per month (forgetting about leap year, for the sake of argument). A clumsy way to do it, of course, is to write a large if statement:

if ( month == 1 ) {
    days = 31;
} else if ( month == 2 ) {
    days = 28;
}
// ...for all 12 months, you get the point
else if ( month == 12 ) {
    days = 31;
}

An easier and more modifiable way to perform the same function is to put the data in a table:

int daysPerMonth [12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

Now, instead of using the long if statement, you can just use simple array access to find out the number of days in a month:

days = daysPerMonth[ month - 1 ];

Indexed access Tables

Sometimes a simple mathematical transformation isn’t powerful enough to make the jump from data like Age to a table key.

Suppose you run a warehouse and have an inventory of about 100 items. Suppose further that each item has a four-digit part number that ranges from 0000 through 9999.

First, if each of the entries in the main lookup table is large, it takes a lot less space to create an index array with a lot of wasted space than it does to create a main lookup table with a lot of wasted space.

For example, suppose that the main table takes 100 bytes per entry and that the index array takes 2 bytes per entry. Suppose that the main table has 100 entries and that the data used to access it has 10,000 possible values.

In such a case, the choice is between having an index with 10,000 entries or the main data member with 10,000 entries. If you use an index, your total memory use is 30,000 bytes. If you forgo the index structure and waste space in the main table, your total memory use is 1,000,000 bytes.

That means you waste 97% less memory with indexed access tables.

Stair-Step Access Tables

Another kind of table access is the stair-step method. This access method isn’t as direct as an index structure, but it doesn’t waste as much data space.

The general idea of stair-step structures is that entries in a table are valid for ranges of data rather than for distinct data points.

For example, if you’re writing a grading program, here’s a range of grades you might have to program:

> 90.0% A
< 90.0% B
< 75.0% C
< 65.0% D
< 50.0% F

This is an ugly range for a table lookup because you can’t use a simple data-transformation function to key into the letters A through F. An index scheme would be awkward because the numbers are floating points. You might consider converting the floating-point numbers to integers, and in this case, that would be a valid design option, but for the sake of illustration, this example will stick with floating point.

Here’s the code in Visual Basic that assigns grades to a group of students based on this example.

' set up data for grading table
Dim rangeLimit() As Double = { 50.0, 65.0, 75.0, 90.0, 100.0 }
Dim grade() As String = { "F", "D", "C", "B", "A"}
maxGradeLevel = grade.Length - 1
...
' assign a grade to a student based on the student's score
gradeLevel = 0
studentGrade = "A"
while ( ( studentGrade = "A" ) and ( gradeLevel < maxGradeLevel) ) 
    If ( studentScore < rangeLimit( gradeLevel ) ) Then
        studentGrade = grade( gradeLevel )
    End If
    gradeLevel = gradelevel + 1
wend

Although this is a simple example, you can easily generalize it to handle multiple students, multiple grading schemes, and changes in the grading scheme.

The advantage of this approach over other table-driven methods is that it works well with irregular data. The grading example is simple in that although grades are assigned at irregular intervals, the numbers are “round,” ending with 5s and 0s. The stair-step is equally well suited to data that doesn’t end neatly with 5s and 0s. You can use the stair-step approach in statistics work for probability distributions with numbers like this:

Probability Insurance Claim Amount
0.456935 $0.00
0.546654 $254.32
0.627782 $514.77
0.771234 $717.82

Ugly numbers like these defy any attempt to come up with a function to neatly transform them into table keys. The stair-step approach is the answer.

⚠️ Tables can provide an alternative to complicated logic, so ask yourself whether you could simplify by using a lookup table.

Programming

Code Complete 2 – Steve McConnell – Unusual Control Structures

I just love Steve McConnell’s classic book Code Complete 2, and I recommend it to everyone in the Software ‘world’ who’s willing to progress and sharpen his skills.

Other blog posts in this series:

  • Part 1 (Chapters 1 – 4): Laying the Foundation
  • Chapter 5: Design in Construction
  • Chapter 6: Working Classes
  • Chapter 7: High-Quality Routines
  • Chapter 8: Defensive programming
  • Chapter 9: Pseudocode Programming Process
  • Chapter 10: General Issues in Using Variables
  • Chapter 11: General Issues in Using Variables
  • Chapter 12: Fundemental Data Types
  • Chapter 13: Unusual Data Types
  • Chapter 15: Using Conditionals
  • Chapter 16: Controlling Loops

Recursion

For a small group of problems, recursion can produce simple, elegant solutions. For a slightly larger group of problems, it can produce simple, elegant, hard-to-understand solutions. ?

Java Example:

void QuickSort( int firstIndex, int lastIndex, String [] names ) {
    if ( lastIndex > firstIndex ) {
        int midPoint =  Partition( firstIndex, lastIndex, names );
        QuickSort( firstIndex, midPoint-1, names );
        QuickSort( midPoint+1, lastIndex, names );            
    }
}

In this case, the sorting algorithm chops an array in two and then calls itself to sort each half of the array. When it calls itself with a subarray that’s too small to sort-such as ( lastIndex <= firstIndex ) – it stops calling itself.

Tips for Using Recursion

  • Make sure the recursion stops – Check the routine to make sure that it includes a nonrecursive path.
  • Limit recursion to one routine – Cyclic recursion (A calls B calls C calls A) is dangerous because it’s hard to detect.
  • Keep an eye on the stack – With recursion, you have no guarantees about how much stack space your program uses. To prevent stack overflow, you can use safety counter and set the limit low enough or allocate objects on the heap using new.
  • Don’t use recursion for factorials or Fibonacci numbers – You should consider alternatives to recursion before using it. You can do anything with stacks and iteration that you can do with recursion. Sometimes one approach works better, sometimes the other does. Consider both before you choose either one.

goto

You might think the debate related to gotos is extinct, but a quick trip through modern source-code repositories like SourceForge.net shows that the goto is still alive and well and living deep in your company’s server.

The Argument Against gotos

The general argument against gotos is that code without them is higher-quality code
+ Dijkstra observed that the quality of the code was inversely proportional to the number of gotos the programmer used
+ Code containing gotos is hard to format
+ Use of gotos defeats compiler optimizations
+ The use of gotos leads to the violation of the principle that code should flow strictly from top to bottom

The Arguments for gotos

The argument for the goto is characterized by advocacy of its careful use in specific circumstances rather than its indiscriminate use.

A well-placed goto can eliminate the need for duplicate code. Duplicate code leads to problems if the two sets of code are modified differently. Duplicate code increases the size of the source and executable files. The bad effects of the goto are outweighed in such a case by the risks of duplicate code.

Good programming doesn’t mean eliminating gotos. Methodical decomposition, refinement, and selection of control structures automatically lead to goto-free programs in most cases. Achieving goto-less code is not the aim but the outcome, and putting the focus on avoiding gotos isn’t helpful.

Summary of Guidelines for Using gotos

Use of gotos is a matter of religion. My dogma is that in modern languages, you can easily replace nine out of ten gotos with equivalent sequential constructs.

  • Use gotos to emulate structured control constructs in languages that don’t support them directly. When you do, emulate them exactly. Don’t abuse the extra flexibility the goto gives you.
  • If goto improve efficiency, document the efficiency improvement so that goto-less evangelists won’t remove it.
  • Limit yourself to one goto label per routine
  • Limit yourself to gotos that go forward, not backward
  • Make sure all gotos labels are used.
  • Make sure a goto doesn’t create unreachable code.

https://t.co/AGH65l2L9F

— Nikola Brežnjak (@HitmanHR) March 12, 2018

Programming

Code Complete 2 – Steve McConnell – Controlling Loops

I just love Steve McConnell’s classic book Code Complete 2, and I recommend it to everyone in the Software ‘world’ who’s willing to progress and sharpen his skills.

Other blog posts in this series:

  • Part 1 (Chapters 1 – 4): Laying the Foundation
  • Chapter 5: Design in Construction
  • Chapter 6: Working Classes
  • Chapter 7: High-Quality Routines
  • Chapter 8: Defensive programming
  • Chapter 9: Pseudocode Programming Process
  • Chapter 10: General Issues in Using Variables
  • Chapter 11: General Issues in Using Variables
  • Chapter 12: Fundemental Data Types
  • Chapter 13: Unusual Data Types
  • Chapter 15: Using Conditionals

“Loop” is a structure that causes a program to repeatedly execute a block of code. Common loop types are for, while, and do-while.

When to use the while loop

If you don’t know ahead of time exactly how many times you’ll want the loop to iterate, use the while loop. Contrary to what some novices think, the test for the loop exit is performed only once each time through the loop, and the main issue concerning while loop is deciding whether to test at the beginning or the end of the loop.

  • while – loop tests condition at the beginning
  • do-while – tests condition at the end, which means that the body of the loop gets executed at least once.
  • loop-with-exit – structure more closely models human thinking. This is the loop in which the exit condition appears in the middle of the loop rather than at the beginning or the end.

When to use the for loop

A for loop is a good choice when you need a loop that executes a specified number of times. If you have a condition under which execution has to jump out of a loop, use a while loop instead.

When to use the foreach loop

It is useful for performing an operation on each member of an array or some other container. It has an advantage of eliminating loop-housekeeping arithmetic and therefore eliminating any chance of errors in the loop-housekeeping arithmetic.

Controlling the loop

You can use two practices. First, minimize the number of factors that affect the loop. Simplify! Simplify! Simplify! Second, treat the inside of the loop as if it were a routine-keep as much control as possible outside the loop.

Entering the loop

  • Enter the loop from one location only
  • Put initialization code directly before the loop – Principle of Proximity advocates putting related statements together.
  • Use while ( true ) for infinite loops – that’s considered a standard way of writing an infinite loop in C++, Java, Visual Basic, and other languages. for(;;) is an accepted alternative.

Processing the middle of the loop

  • Use { and } to enclose the statements in a loop – Use brackets every time. They don’t cost anything in speed or space at runtime, they help readability, and they help prevent errors as the code is modified.
  • Avoid empty loops – In C++ and Java, it’s possible to create an empty loop, one in which the work the loop is doing is coded on the same line as the test that checks whether the work is finished. Here’s an example:
    while( (inputChar = dataFile.GetChar() ) != CharType_Eof ) {
        ;
    }
    

    The loop would be much clearer if it were recoded so that the work it does is evident to the reader:

    do {
        inputChar = dataFile.GetChar();
    } while ( inputChar != CharType_Eof );
    

The new code takes up three full lines rather than that of one line, which is appropriate since it does the work of three lines rather than that of one line.
+ Keep loop-housekeeping chores at either the beginning or the end of the loop – As a general rule, the variables you initialize before the loop are the variables you’ll manipulate in the housekeeping part of the loop.

Exiting the loop

  • Assure that the loop ends
  • Don’t monkey with the loop index of a for loop to make the loop terminate
  • Avoid code that depends on the loop index’s final value

Exiting loops early

The break statement causes a loop to terminate through the normal exit channel; the program resumes execution at the first statement following the loop.

continue causes the program to skip the loop body and continue executing at the beginning of the next iteration of the loop.

Inefficient programmers tend to experiment randomly until they find a combination that seems to work. If a loop isn’t working the way it’s supposed to; the inefficient programmer changes the < sign to a <= sign. If that fails, the inefficient programmer changes the loop index by adding or subtracting 1. Eventually, the programmer using this approach might stumble onto the right combination or simply replace the original error with a more subtle one. Even if this random process results in a correct program, it doesn’t result in the programmer’s knowing why the program is correct.

How long should a loop be?

Make your loops short enough to view all at once. Experts have suggested a loop-length limit of one page (about 50 lines of code). When you begin to appreciate the principle of writing simple code, however, you’ll rarely write loops longer than 15 or 20 lines.

Creating loops easily – from the inside out

If you sometimes have trouble coding a complex loop, which most programmers do, you can use a simple technique to get it right the first time.

Here’s the general process:
+ Start with one case.
+ Code that case with literals.
+ Then indent it, put a loop around it, and replace the literals with loop indexes or computed expressions.
+ Put another loop around that, if necessary, and replace more literals.
+ Continue the process as long as you have to
+ When you finish, add all the necessary initializations.

Since you start at the simple case and work outward to generalize it, you might think of this as coding from the inside out.

Android

How to make a native Android app that can block phone calls

TL;DR

In this post, I’ll show you step by step how to make a native Android app that can block certain numbers from calling you.

The source code is on Github.

I hope that my step by step guide that I’m going to show you here will help you and save you from doing additional research.

Of course, since I’m not a native Android developer in my day to day job, I’m doing it also for the fact that it will serve me as a good reminder for when I need to deal with a similar situation again. Shout out to the rest of you #jackOfAllTrades out there ?

Also, given the statement above; I would appreciate any feedback regarding this code. ?

!TL;DR

I’ve spent a lot of time going through StackOverflow and blog posts in search of this solution. Of all of those, these were helpful:

  • How to detect incoming calls on an Android device?
  • Can’t answer incoming call in android marshmallow 6.0
  • Android permission doesn’t work even if I have declared it
  • End incoming call programmatically
  • Is the phone ringing

But sadly, none of them was straightforward, beginner kind of tutorial. So, after a lot of additional research, I made it work, and here’s my best attempt at explaining how.

As a sidenote: while testing this, the discovery of how to simulate an incoming call or SMS to an emulator in Android Studio was also very helpful.

Starting a new project

In Android Studio go to File->New->New Project, give it a name and a location and click Next:

Leave the default option for minimum API level:

Select an Empty Activity template:

Leave the name of the activity as is:

AndroidManifest.xml

Set the permissions (two uses-permission tags) and the receiver tags in AndroidManifest.xml file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.nikola.callblockingtestdemo">

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.CALL_PHONE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver  android:name=".IncomingCallReceiver" android:enabled="true" android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

With the READ_PHONE_STATE permission we get this (as defined in official docs):

Allows read-only access to phone state, including the phone number of the device, current cellular network information, the status of any ongoing calls, and a list of any PhoneAccounts registered on the device.

With the CALL_PHONE permission we get this (as defined in official docs):

Allows an application to initiate a phone call without going through the Dialer user interface for the user to confirm the call.

⚠️ I found that even though not stated here, I need this permission so that I can end the call programmatically.

The receiver tag is used to define a class that will handle the broadcast action of android.intent.action.PHONE_STATE. Android OS will broadcast this action when, as the name implies, the state of the phone call changes (we get a call, decline a call, are on the call, etc.).

IncomingCallReceiver.java

Create a new class (File->New->Java Class), call it IncomingCallReceiver and paste this code in (note: your package name will be different than mine!):

package com.example.nikola.callblockingtestdemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.widget.Toast;
import java.lang.reflect.Method;
import com.android.internal.telephony.ITelephony;

public class IncomingCallReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        ITelephony telephonyService;
        try {
            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
            String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);

            if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)){
                TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                try {
                    Method m = tm.getClass().getDeclaredMethod("getITelephony");

                    m.setAccessible(true);
                    telephonyService = (ITelephony) m.invoke(tm);

                    if ((number != null)) {
                        telephonyService.endCall();
                        Toast.makeText(context, "Ending the call from: " + number, Toast.LENGTH_SHORT).show();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

                Toast.makeText(context, "Ring " + number, Toast.LENGTH_SHORT).show();

            }
            if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_OFFHOOK)){
                Toast.makeText(context, "Answered " + number, Toast.LENGTH_SHORT).show();
            }
            if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE)){
                Toast.makeText(context, "Idle "+ number, Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

In Android, if we want to ‘get’ the data from the BroadcastReceiver, we need to inherit the BroadcastReceiver class, and we need to override the onReceive method. In this method, we’re using the TelephonyManager to get the state of the call, and we’re using the ITelephony interface to end the call.

To be honest, this is where it gets a bit ‘weird’, as to get this ITelephony interface, you need to create the ITelephony interface.

ITelephony.java

To do that, create a new class (File->New->Java Class), call it ITelephony and paste this code in (note: overwrite everything with the below content; yes, even the weird package name):

package com.android.internal.telephony;

public interface ITelephony {
    boolean endCall();
    void answerRingingCall();
    void silenceRinger();
}

Android Studio will complain about package com.android.internal.telephony; (red squiggly dots under this package name), but that’s how it has to be set for this to work. I didn’t find the exact explanation why this has to be included, so if you know, please share it in the comments.

Requesting permissions at runtime

This was one thing that was hindering my success in getting this to work!

Namely, after Android 6.0+, even if you have permissions set in the AndroidManifest.xml file, you still have to explicitly ask the user for them if they fall under the category of dangerous permissions. This is the list of such permissions:

  • ACCESS_COARSE_LOCATION
  • ACCESS_FINE_LOCATION
  • ADD_VOICEMAIL
  • BODY_SENSORS
  • CALL_PHONE
  • CAMERA
  • GET_ACCOUNTS
  • PROCESS_OUTGOING_CALLS
  • READ_CALENDAR
  • READ_CALL_LOG
  • READ_CELL_BROADCASTS
  • READ_CONTACTS
  • READ_EXTERNAL_STORAGE
  • READ_PHONE_STATE
  • READ_SMS
  • RECEIVE_MMS
  • RECEIVE_SMS
  • RECEIVE_WAP_PUSH
  • RECORD_AUDIO
  • SEND_SMS
  • USE_SIP
  • WRITE_CALENDAR
  • WRITE_CALL_LOG
  • WRITE_CONTACTS
  • WRITE_EXTERNAL_STORAGE

To ask for such permissions here’s the code you can use (I used it in MainActivity.java in the onCreate method):

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
    if (checkSelfPermission(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_DENIED || checkSelfPermission(Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_DENIED) {
        String[] permissions = {Manifest.permission.READ_PHONE_STATE, Manifest.permission.CALL_PHONE};
        requestPermissions(permissions, PERMISSION_REQUEST_READ_PHONE_STATE);
    }
}

The PERMISSION_REQUEST_READ_PHONE_STATE variable is used to determine which permission was asked for in the onRequestPermissionsResult method. Of course, if you don’t need to execute any logic depending on whether or not the user approved the permission, you can leave out this method:

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_READ_PHONE_STATE: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "Permission granted: " + PERMISSION_REQUEST_READ_PHONE_STATE, Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "Permission NOT granted: " + PERMISSION_REQUEST_READ_PHONE_STATE, Toast.LENGTH_SHORT).show();
            }

            return;
        }
    }
}

App in action

This is how the app looks like in action, tested on the emulator and call triggered by using Android Device Monitor in Android Studio:

Conclusion

In this post, I showed you how to make a native Android app that can block certain numbers from calling you. I pointed out the blocker that I was facing, and I’m still searching a solution to hide a native incoming call popup that still sometimes shows up for a brief second before the call gets rejected.

So, if you have any ideas, I’m open to suggestions ?

How to make a #native #Android app that can #block phone #calls https://t.co/NMYvOFlPO8

— Nikola Brežnjak (@HitmanHR) February 22, 2018

Android

How to simulate an incoming call or SMS to an emulator in Android Studio

In this quick tip, I’ll show you how easy it is to simulate an incoming call or SMS to an emulator in Android Studio.

TL;DR

In Android Studio, go to Tools->Android->Android Device Monitor

Select the Emulator on the left (you have to run it first – do that through Tools->Android->AVD Manager), then the Emulator Control tab, insert the number, select Voice or SMS and click the Call or Send button:

See both of these functions in action:

Conclusion

In this short tip, I showed you how easy it is to simulate an incoming call or SMS to an emulator in Android Studio.

Android Device Monitor allows you to set other stuff as well, like for example the current location, Network preferences (to test your app in not so ideal connectivity situations), etc.

Hope this helps ?

How to simulate an incoming call or SMS to an emulator in #Android Studio https://t.co/Ou9vRRV7RW

— Nikola Brežnjak (@HitmanHR) February 21, 2018

Projects

DevThink podcast

I’m super happy to announce that my friend (and coworker) Shawn Milochik and I started our very own podcast called DevThink.

In the DevThink podcast we discuss ideas and practices that worked for us in our software development journey so far. We’re also discussing the topics that we’re currently exploring and learning about.

Currently, we published two shows:

  • Is It Worth To Build A Profile On StackOverflow?
  • Dvorak #1 – Impressions After Using It for 2 Weeks

But, we’ve already recorded 8 additional shows that we’ll be publishing one per week.

Of course, feedback is greatly appreciated (be it good or bad)… We’re also open to suggestions for what you’d like to hear us discuss ?

Super happy to announce the DevThink podcast, brought to you by @ShawnMilo and yours truly https://t.co/VwgtRUdXkj

— Nikola Brežnjak (@HitmanHR) February 19, 2018

Page 14 of 52« First...10«13141516»203040...Last »

Recent posts

  • SendGrid Phishing Scam Attempts
  • Retrospective Questions
  • When espanso Breaks on Long Replacement Strings (and How to Fix It)
  • 2024 Top Author on dev.to
  • Hara hachi bun me

Categories

  • Android (3)
  • Books (114)
    • Programming (22)
  • CodeProject (36)
  • Daily Thoughts (78)
  • Go (3)
  • iOS (5)
  • JavaScript (128)
    • Angular (4)
    • Angular 2 (3)
    • Ionic (61)
    • Ionic2 (2)
    • Ionic3 (8)
    • MEAN (3)
    • NodeJS (27)
    • Phaser (1)
    • React (1)
    • Three.js (1)
    • Vue.js (3)
  • Leadership (1)
  • Meetups (8)
  • Miscellaneou$ (80)
    • Breaking News (8)
    • CodeSchool (2)
    • Hacker Games (3)
    • Pluralsight (7)
    • Projects (2)
    • Sublime Text (2)
  • PHP (6)
  • Quick tips (41)
  • Servers (8)
    • Heroku (1)
    • Linux (3)
  • Stack Overflow (81)
  • Unity3D (9)
  • Windows (8)
    • C# (2)
    • WPF (3)
  • Wordpress (2)

"There's no short-term solution for a long-term result." ~ Greg Plitt

"Everything around you that you call life was made up by people that were no smarter than you." ~ S. Jobs

"Hard work beats talent when talent doesn't work hard." ~ Tim Notke

© since 2016 - Nikola Brežnjak