Balance out your mindset!

Hello everyone!!!

Last week, we have talked about having a relapse. If you have not seen it, you may want to read it here.

We have some cases where we cannot be sure where our work stands due to other people trying it out. Then, as we get back to work, we find that things are pretty accurate, but at the same time, we can fix it fast since we just know how it works (because we own the project).

Therefore, we are worried for almost nothing!

If you find yourself in this situation, don’t stress out too much. Just relax and stay focused. At most times, bugs can be fixed just by a line of code!

Also, if you don’t have enough people on your team and you need to temporarily add someone else just to ship a product, leverage each other so that everything works out properly. Every person has to work hard, but it’s better to work smarter and reduce stress at the same time.

I was also given great advice which is to always stay happy no matter what happens. Mood plays an important role in job function; if it’s good then most likely you’ll produce, but if it’s bad then most likely you’ll fall apart.

Yes, weekdays are reserved mainly for work time. For weekends, work as you see fit. If you feel there is no need to work on a weekend and rather spend the time to relax, then do that; working too much can cause somebody to lose motivation and work less.

It’s like the scale effect. Let’s take two sides and call one of them work, and the other motivation. By working too much, then motivation gets lost out of touch. The effect is finding a lot more bad spots that can be better avoided, producing too much where things can be done a lot better, and losing touch with the outside world which helps to spark creativity.

Let’s put the weight on the other side of the scale in which there is too much motivation and very little work. Maybe this is good along with finding bugs and fixing them, and also building something small that works really nicely. This is really great of course in a job. Let’s say however that one day, you’re working on something big and needs quite a bit of effort put into it. How less of code can be put if there’s a lot that needs to be done? Not very less.

Therefore, balance is very important, and being in the middle of everything you have. Horizons grow as you have friends, family, and anything else that helps you live a fulfilling life.

Therefore, be sure to always be attentive to anything you do, just don’t overdo it!

With warm regards,

Brian.

A little relapse because of blah…

Hey there!

Ever had this feeling in your gut that things will go smoothly, especially after your vacation time?

Then during your vacation all of a sudden you have an important call to take. In most cases, this means work is on the way.

This can sound a bit scary to some people, but myself I think of it as a motivator. Why?

It allows to help me in doing the best work that I possibly can do! This means a couple of things:

1 – Get back on your schedule as you would normally, and know that your vacation is already done.
2 – Re-energize and do the work!

I first thought of the sense in which I’ll have a more relaxed time outside, then I’ll come back and work on everything else in a relaxed manner because the main parts are already done.

In turn, I’ve gotten more work! This is actually good because others try to help build out your reputation. Therefore, the opportunity of getting something done authored by somebody else will pay off ten-fold.

Everybody has their fair share of mistakes, and that’s normal!

Anyway, going back to the vacation time – why do we usually have a relapse?

This is mainly because of not being productive. By relaxing, our minds usually become very focused in the moment trying not to think about anything else. Then going back to work would be like a disaster!

To solve this problem, the best thing to do would be sticking to your daily habits like exercising in the morning. Sure, you’re on vacation, but relax sparingly.

Then finally when you’re back at work, just do the work.

Until next time,

Brian.

Finish the project earlier than your deadline?

Hey everyone!

Ever heard the expression that it’s hard to meet deadlines several times because it usually ends up being very subjective? Ever heard that it can be very complex to do?

Why do some companies fail to meet deadlines anyway?

Sometimes it is because there are bugs within the system. Other times, it can be just because of another individual, or the protagonist himself or herself. If it’s the latter, then it’s usually the lack of motivation.

If it’s the protagonist, then the best way would be to change habits that are not so useful, and rather do something else that would be more productive to the person. Here’s a simple example – eating chips, candy and watching TV at the same time will lead to poorer mental and physical health. In conjunction, sitting in the office and doing work also isn’t very good either. Maybe the pleasurable things as mentioned above relieves stress, but it will not be reduced and instead its level will increase over time. Instead if the person chooses to exercise, then the bad stuff clears out much easier, and the person will also be able to focus. The stress levels will be reduced and also work could be thought of since exercise is just a routine workout. In turn, instead of taking 2 days to solve a problem, it can be solved probably within 4 hours.

If it’s the antagonist, then the best way I’ve found to be helpful so far is to communicate earlier on. Addressing things early can help a person to think of the right way to do something, and then getting back to the protagonist latest on the next day. This also includes the first point – any systematic bugs either in the architecture or design. Code has many possibilities, but it can also lead to serious problems if something is not done as intended. Therefore, it’s not just the smarts, but it’s also the wisdom that will definitely help to save time.

This goes back to simplicity. Don’t reinvent the wheel if there’s already a solution out there. Otherwise, reading the documentation on how something is used will also give an idea of how to approach a problem.

The longevity of a project also depends on how good something has been done; in other words, will it be easy to add in features? Is the code maintainable? Is it readable? I’ll admit that sometimes it’s hard to do especially if somebody is rushing to get things done very fast. However, if it has to be and when the time comes, making sure to refactor the code for ease will do wonders to your system and also your team. If something is poorly written and/or structured, then it means the other person who is working or will work on the project will probably not want to touch it and instead do a rewrite. Therefore, considering thoughts will also help out your programming career.

So the verdict is to stay healthy, always communicate (there’s no such thing as over-communicating), and make everything simple. This will help save time, and then more possibilities will come along the way. Hopefully these principles help you as they are serving me well so far.

Until next time!

Brian.

Work during weekends?

Hey everyone,

Ever seen those people who are mainly just looking to mostly enjoy their weekends when they’re having a couple of days off?

At the same time, they are just pretty much regular, normal people who are either studying or working-class citizens. Then as Monday comes in, they go back in and do work. There are good days and bad days, but everybody tries to make them good. In a team effort, it’s pretty good in order to get things done in a timely manner.

How about weekends? What are the circumstances, advantages, and/or special cases, and why should or shouldn’t you work during weekends?

The advantages of working during weekends is that they can be compensated for your paid time off if there are any deductions and if the company you work for provides them! Also, working individually means less distractions for a single person. For a family person it is a little bit different, but nonetheless they can make it work. Anyhow, where am I getting at here?

Simple! The work gets done faster while a person does not need to deduct his or her PTO hours. This especially can go well if the individual in question wants to compensate for certain days off because of some event.

The disadvantages are that a person can do only so much in which some things need a team effort where it is almost impossible for an individual to do the work. Unless it is the person’s own individual project, then saying something is impossible will almost always be an excuse. The person might also grow tired of the project because there’s more stress to the person’s own body. Therefore, doing this all the time is not very healthy. Plus if the person is coding, then the project might become worse off as it grows since it won’t be cared for or maintained too greatly.

So how should we come with a compromise?

As any programmer will work with logic, it should also be applied to life principles. In this case, it means to always plan ahead and think about what you want as an individual. A perfect example: why I chose to work this weekend is so that I will be able to compensate for 2 days of vacation time during the Thanksgiving weekend, which means that I am doing what I understand could be done in a timely manner work-related, and also what cannot be done. At the same time, these events can be recorded as I get back in the office to discuss them along with a team member, and that as anything comes up then I know that it will be resolved. Also because the project is due by the beginning of December, the weekends helps to get the project out sooner than usual.

Having said that, what is the verdict?

1 – Work where you think it makes the most sense.
2 – Know your limits.
3 – Stay healthy and well to be at the best productive level.
4 – Communicate beforehand with your boss if you decide to work during the weekend just like any other work day.

Until next time,

Brian.

Replacing ViewPagers with RecyclerViews?

Hello again!

Ever used ViewPagers where users are able to swipe through your app kind of like how users swipe through Tinder profiles? Yes, those kinds that fill up the full screen.

But okay, with Tinder, the data already on screen can be thrown away (if it’s something cheap enough like text) then cached somewhere in its own database system since the data will not be available at the same instance the user is using the application.

However, what about caching all of those profile pictures? Also, what if one of those profile pictures change?

Sure, a developer can just do return PositionNone; as he or she overrides GetItemOffset(int position) and then do a NotifyDataSetChanged(); against the ViewPagerAdapter. Let’s say for our purposes, we want to do something similar for our application. At the same time, let’s multiply the limited amount of profile pictures by 1000. That phone’s or tablet’s RAM cannot handle that all at once for sure!

And then to handle that kind of situation requires quite some complex logic by keeping track of the view’s position possibly by using a DataSetObserver that has to be registered. The next developer who comes on the project wouldn’t understand it right away.

There’s a better way!

Because the Android framework gets updated after each API update, there’s now the SnapHelper class along with its subclasses PagerSnapHelper and LinearSnapHelper. These classes have been around a little over a year ago, so they are sure to be quite stable. The most recent Google Play app even utilizes SnapHelper. Here, we will be focusing on the PagerSnapHelper class.

To use it, we’ll first have to instantiate our RecyclerView just as usual with our RecyclerView.Adapter and our LinearLayoutManager set horizontally. Next, it will be good to subclass the PagerSnapHelper class so that we will be able to keep track of the position as we will want. Then, we will just need to override the FindTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY) so that as the RecyclerView scrolls to the viewable position, the essential data to be displayed at that time will be bound there instead of OnBindViewHolder(RecyclerView.ViewHolder holder, int position) by loading our variable there. Why we’re doing this is because as we snap along to the other child views, the RecyclerView will still try to bind the next 2 views ahead or behind the current position as it predicts those views are upon interaction.

With that said, the code will look like this:

class MySnapHelper : PagerSnapHelper
{
    // C# action that will take an integer as a parameter
    readonly Action positionTracker;
    public MySnapHelper(Action tracker)
    {
        positionTracker = tracker;
    }

    public override int FindTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY)
    {
        var snapTracker = base.FindTargetSnapPosition(layoutManager, velocityX, velocityY);
        positionTracker.Invoke(snapTracker);
        return snapTracker;
    }
}

Looking good! Now in our OnCreate(Bundle savedInstanceState) method we can do this:

...
List urlList = ... ; // Data that exists from a source

var rv = new RecyclerView(this); // arbitrary RecyclerView
var lm = new LinearLayoutManager(this, LinearLayoutManager.Horizontal, false);
// adapter code omitted ...
rv.SetLayoutManager(lm);

// Where the data binding will happen
var sh = new MySnapHelper(viewingPosition => {
    // Our data at position
    var url = urlList[viewingPosition];

    // Here's our view:
    var viewAtPosition = lm.FindViewForPosition(viewingPosition);

    // Or if you prefer to get the ViewHolder
    var viewHolderAtPosition = rv.FindViewHolderForAdapterPosition(viewingPosition); // don't forget to make the cast to your custom ViewHolder! :)
    // Do awesome stuff with url along with the View or ViewHolder bound here!!!
});
...

And that’s all there is to it! This is much cleaner and a lot easier for other developers to understand! 🙂

Until next time,

Brian.

Working efficiently (Part 2: by your own)

Hey everyone,

Ever come across a problem that you can’t solve over the top of your head?

Remember that one time where you’d think it would take about 2 weeks to get something done, where in reality it only took you one week? Can you make it even less?

Communication was part of the goals to help you solve the problem. That can be found here if you’d like to catch up.

This week, we will be talking about our sole tasks in our own proficiency. An example is possibly a developer probably hasn’t touched something in the past. What would the junior-most person do? They would constantly ask the more senior developers about something. This is fine in the beginning, but fundamentals should stick after a couple of months!

However, let’s talk about what the people with more experience will do. The most possible first step is that they’ll either go through the documentation, or pop-up a learn by example procedure.

They will then build up a prototype to see if it’ll fit the needs of the project. Once the code is produced and just works as is, and depending on the complexity, they will either leave it as is, or they will try and simplify the code as much as possible.

Simplification is due to having the next person understand what the code does in a general sense, not getting through the nitty-gritty details of what something does.

Otherwise, how about if a new feature has to be implemented? First step again would be communication, in this case between the project manager and designer or architect. Once an agreement has been made, the developer will have to look at the whole picture of how the feature should work before implementation.

Once the developer has already mapped out the workings of the implementation, then he or she will find the simplest way to solve the problem. This means less code = less bugs, and a more fine-tuned product.

After implementation, the developer will make sanity tests to their code. There are some brilliant exceptions to this rule, but the good developers always will be their own adversaries and find out what’s broken. If something breaks, rinse and repeat.

Once the developer has finalized everything, he or she will now make a pull request to the lead. And most likely following these steps, the code will be merged in.

And that’s it! 🙂 Even if the developer is sick, then he or she will most likely still be able to produce that feature following these steps.

Until next time!

Brian.

Working efficiently (Part 1: communication)

Hello again!

Ever stumbled upon a problem that will probably take quite some time to solve?

Ever thought that a person can do something just by himself or herself?

The only exception to this rule if the project only solely belongs to one person and that this person truly understands everything about it. But this will also take some time.

How about if one were working within a company with several other people?

This means the person will have to understand how the infrastructure works, and the time factor to understand it takes some time depending on the size or domain of what the person needs to know.

And depending on the person, he or she might have to go through a series of weeks to figure something out.

There’s a better way. Ready for it? It is called: communication.

Now this person may call himself or herself an introvert, but good companies will always try and help each employee with their personal and professional growth.

Even when trying to debug a piece of code that can’t be solved solely yourself, always ask the person who committed the code to find out what exactly is going on. Pairing up for 10 minutes can just work wonders without headaches.

This is a reason why we call it team work. Of course if a person has to do something solely, then making a pull request to another member of the team will give an idea of what needs to be done. Once they review it, they’ll provide their feedback, or ask questions along the way.

Going out to lunch from time to time and communicating about what you can improve on will also give some clues. Seeing as a first-person is not the same perspective from a third-person.

Therefore, supporting one-another is important and will serve you well. Eventually, all the good habits will come out naturally given a problem, and then finding the best way to solve it.

So next time if it’s something related to your task that another person has done, communicate with that person to make the process easier.

As an added bonus, even senior-level people in their careers will always communicate when they have to. Therefore, there is absolutely no reason to be shy since others will find out about someone one way or another.

Until next time!
Brian.

Less is more!

Hey everyone,

Ever tried adding a library component that you’ve found through either GitHub or the Xamarin store? How many are there also to begin with?

Also, how portable will the component be? Will it work on just one platform or will it work on all?

Let’s take image library components for example. Fresco is quite a nice image library that immensely helps manage images in our memory buffer, so that there is no need to do a lot of processing on the client-side developer’s end. However, this is only for Android, and Xamarin does not yet have this component in their store 🙁

But no worries. SkiaSharp is here to the rescue! It is cross-platform and has been implemented by Google that Xamarin has ported over!

There is a nice introduction on how to get started. It is quite comprehensive in each of its functionality, but to get started is not as smooth as how Fresco’s documentation, which is to go from adding the gradle dependency down towards the implementation, would be. Its guide is okay-ish if one knows where to look, so that’s not too much of a problem. There’s an exception to one thing however.

What is it?!

The image management on where to load its buffers in memory! What if we had a large image and tried to put it on a canvas with all of the information? Our application will crash with an OutOfMemoryException! 🙁

How do we rectify this?

The easiest solution would be to call the .Resize(SKImageInfo, SKBitmapResizeMethod) method on a SKBitmap object. To do this, we’ll also need an SKImageInfo providing the desired width and height that we want to display on our device in the app.

Let’s say that if we want our image to be 1/2 the width of the screen, we can make our width to be context.Resources.DisplayMetrics.WidthPixels / 2. If we want our height to match the dimensions of how the image should look like, then it should be image.Width * width / image.Height, where image is our SKBitmap object.

Therefore, our code should look like this:

SKBitmap resizedSkBitmap;
using (var largeSkBitmap = SKBitmap.Create("ourLargeImage.png"))
{
    var width = context.Resources.DisplayMetrics.WidthPixels / 2;
    var height = largeSkBitmap.Width * width / largeSkBitmap.Height;
    var wantedImageInfo = new SKImageInfo(width, height);
    resizedSkBitmap = largeSkBitmap.Resize(wantedImageInfo, SKBitmapResizeMethod.Lanczos3);
}


Use the resize method that would suit your needs best 😉 I’ll provide the link here.

That’s pretty much it!

Also, don’t forget to call resizedSkBitmap.Dispose(); when finished with the image to avoid any memory leaks 😉

Until next time!

Brian.

Flexibility for your app

Hey everyone!

Have you ever worked on a very nicely knit portion of an app? That is, an element that is being used over and over so that you don’t have to reinvent the wheel every time?

Try this with a singleton class or type. This design pattern is really useful for symmetrical data that can be passed in multiple times from a JSON element. Therefore, singleton-models from here are very nice, no problem!

Now, try this with UI elements. Let’s take a ViewHolder from the Android SDK for example.

We have a base item element that our CustomViewHolder gets where it takes some fields like name, address, and profile picture. From there, it’s also possible to add in some nice helper methods to do some extra work like getting data from the server. However for simplicity, we’ll just focus on our CustomViewHolder. Therefore, we’ll have something like this:

public class CustomViewHolder : RecyclerView.ViewHolder
{
    public ImageView IvProfilePicture;
    public TextView TvName, TvAddress;

    public CustomViewHolder(View itemView)
    {
        IvProfilePicture = itemView.FindViewById<ImageView>(Resource.Id.ivProfilePicture);
        TvName = itemView.FindViewById<TextView>(Resource.Id.tvName);
        TvAddress = itemView.FindViewById<TextView>(Resource.Id.tvAddress);
    }
}

// ... we'll assume that the XML layout already has these fields

Cool! 🙂

Now, we’ll consolidate our CustomViewHolder into our CustomRecyclerViewAdapter, and then just populate along with the data we will need along with the constructor. Again, cool, it works as we will want it to! 🙂

Now let’s say that our design changed in one of the pages that uses this CustomRecyclerViewAdapter. There are still remaining view elements, with just one additional view element to the list item.

Therefore, depending on the screen, we’ll just toggle this view element’s visibility to Visibility.Gone or Visibility.Visible, right?

If it is a simple dumb view element, then that will work. However, if there is a control element involved, then it’s more complicated than that. 🙁

We can’t just use viewElement.Click += (objectSender, eventArg) => { // code for what should be done after user clicks }; Why? A RecyclerView will reuse elements as we move along, and therefore, we are incrementing click events instead of replacing them every single time.

Also, our constructor can’t support a ClickEventArgs type as a parameter since we will have different views all the time. Therefore, what is the solution?

Being quite familiar just with Android development, we can go back to our old View’s setOnClickListener(View.OnClickListener listener) method!

Well, almost 😉

In Xamarin.Android, we can’t just override the methods that the anonymous classes or anonymous interfaces give us in plain Android development.

Rather, we will need to make our own extension classes in C# that will support the same functionalities that is already provided in plain Android. So, let’s do that!

public CustomOnClick : Java.Lang.Object, View.IOnClickListener
{
    readonly Action action;

    public CustomOnClick(Action action)
    {
        this.action = action;
    }

    protected override void OnClick()
    {
        action.Invoke();
    }
}

Now in our CustomOnClick class, we have just made an Action that work like anonymous methods in Java. Therefore, we can now do this:

viewElement.SetOnClickListener(new CustomOnClick(() => { // code for what should be done after user clicks }));

Now every time we scroll our RecyclerView, we are sure that our clicks get reset each time with the specific functionality we want exclusive to the item! Now, we have a flexible RecyclerView.Adapter! 🙂

In this case, we have inherited from Java’s Object class to avoid IntPtr handles and Dispose since Android should handle all of this on its own regardless. In the Android SDK, this is mainly with interfaces described here, but there should be no need to do this for classes.

By the way, this works well with other portions where by default Xamarin.Android doesn’t provide their own interfaces for any other Android anonymous classes. 😉

Until next time!

Brian.

Introduction

Welcome to Improvised Code!

Our goal is to keep doing our best in our software development skills! We may know how to write out our functions/methods, structs, or classes, but some of those times what we write may not be the best.

Even some frameworks or libraries we use out there can be really great, however, there can also sometimes be bugs as well! That’s why there’s not really any best framework. They can be the best for a period of time, but maintaining that best figure is quite the challenge.

Anyhow, almost any software developer knows that as long as a person knows how to code, then the languages, frameworks or libraries that he or she is using will not matter much since there are similarities. For example, a person that knows Java will not have too much of a problem with C# since they both share very similar syntax.

Therefore, learning the technical stuff isn’t too much of a problem. However, learning the best practices of code takes a bit of time (especially if a person is just starting out). You might even be asking yourself “Wow, this looks really rough! Can I make this even better?!”

Of course! We just need to figure out what we want our software to do by first mapping it out with chicken scratch, and then code it in the simplest way to achieve what we have mapped out. When that’s done, then anybody who reviews our code will be much happier along the way!

Although the bad news is that learning the bits and pieces to make really nice and interesting code can take a bit of time, the good news is it can be fun and rewarding once a person him/herself experiences this. So with that being said, let’s motivate ourselves to make better software!

Improvised Code’s starting choice of platform will be Android, on top of using the Xamarin.Android framework. But don’t fret too much since there may be similarities with just Android under Google! Also, unless an object or type is platform specific, just because there is one focus of platform doesn’t mean that the same algorithm cannot be used in another.

And, my final words in this post are: code happy, live well.

Yours truly,

Brian.