Reactive extensions with Xamarin – the answer

Hello again!

We’ll dive mainly into coding today.

This is part 2 of Reactive Extensions… in Xamarin Android???. Check that out if you’re interested in a little bit of history and use cases, or feel free to skip if you just want to dive into the bits and pieces of the code.

In reactive extensions, there is a timeline of events of what’s happening with something. We can take a real life example in which a person takes his/her wallet and dirty clothes, goes to the laundromat, washes the clothes, dries the clothes, puts them in the car, goes to the supermarket, buys food, and finally goes home with everything completed in the check list.

We’ll put this in the observer/observable pattern. The observable is the person in question. The observer is the follower of the story.

We’ll keep it simple by taking the example above with a shorter list of things to do: person exits his/her home, person goes to the supermarket, then person goes back home.

using System.IO;
using System;
using System.Reactive;
using System.Reactive.Subjects;

class Person
{
    Subject whenFinished = new Subject();
    IObservable WhenFinished => whenFinished;
    
    int step;
    
    void ExitHome()
    {
        // person exits home
        step = 1;
        whenFinished.OnNext(false);
    }
    
    void EnterSupermarket()
    {
        // person enters supermarket
        step = 2;
        whenFinished.OnNext(false);
    }
    
    void GoHome() {
        // done with tasks
        whenFinished.OnNext(true);
    }
    
    static void Main()
    {
        WhenFinished
        .Subscribe(finished => {
            string message;
            if (finished)
            {
                message = "finished";
            }
            else
            {
                switch (step)
                {
                    case 1:
                        message = "entering supermarket";
                        EnterSupermarket();
                        break;
                    case 2:
                        message = "going home";
                        GoHome();
                        break;
                    default:
                        message = "doing something else";
                        break;
                }
            }
            
            Console.WriteLine(message);
        });

        ExitHome();
    }
}

Let’s take a look at what’s happening here. We’ve made a subject that is a tracker to find the state of the person which is, in this case, the observer. Our observable lets us know if any updates are coming into the object, or rather, returning the subject. In our main method, we first initially subscribe to any events being pushed. No activity is being made at this time, that is until we call our ExitHome(); method.

Now as this method is being executed, it means we are updating our step to 1. Our observer then calls the OnNext(bool); method, triggering our code to run within the WhenFinished.Subscribe(...) method. The parameter finished, in this case, would be false because we passed in false as a parameter in our call to OnNext(...);. Now, since the step is 1, we call EnterSupermarket();, which updates our step to 2. Similar happens with step 1, except now it jumps to step 2 calling GoHome(); in the process. Now because GoHome(); passes in true within the OnNext(...); call, our subscription is done, at least for now…

So basically, anytime there is an observer and observable, with the observer calling OnNext(...);, the observable will always subscribe to it.

This can be used in any Xamarin codebase as long as the libraries are referenced. Please let me know in the comments below if you’d like more examples of such.

Until next time!

Brian.

Reactive Extensions… in Xamarin Android???

Hello all!!!

You’ve probably heard of RxAndroid and have already played with it, I’d imagine. Yes?

You’ve probably never thought of using reactive extensions in Xamarin because… it is just something different entirely…

Well, if you didn’t know, here’s the not-so secret surprise. Reactive extensions have already long existed within the .NET framework since version 4.0, so the good estimate was in 2011. That’s long before RxAndroid started to exist which was mid 2013 observing its commit log!

Therefore, it is really nothing new for functional-oriented programmers. It’s however a very new concept to object-oriented programmers since its style is especially looking for events in some interesting thing.

Depending on the purposes of what your code wants to achieve, sometimes object-oriented principles will play in better than reactive extensions, and the same is true vice-versa.

Usually with object-orientation, if we are passing an object as an input value, then an output value will be given by using this object possibly also giving the ability to change it. In reactive extensions, we have a timeline of events that look for what gets in, and then we get an output.

We’ll go in deeper into the subject in a different blog post.

Merry Christmas, and until next time!

Brian.

A second pair of eyes help as well!

Hello again!

Today, I’d like to talk about how another set of eyes will also help.

If you’ve been writing code mainly by yourself, you may think that it’s good because it works!

Some day, you or another person will have to go back to the code that you’ve written before, and if that pile of beauty as you’d call it is good because you’re the only person who has written it, then you’ll probably end up in more serious troubles in the future.

You’ll be lucky if there’s another nice and serious developer who has had a lot of miles ahead of him and will help you to look at what you could’ve done better. This is also a part of their job in order to help you grow.

This is not to say that they don’t write code that is hard to understand. Everybody is human and nobody is perfect. Therefore, we can think of it this way in which a person writes an essay. The first draft is not perfect. The second draft is a little bit better. The third draft gets better than the second draft. The final draft is the best.

In code, drafts are like release versions of an application. The first release will almost always have something that sucks. This is why there are so many iterations of releases in all the software that we see, in which, there’s never a final draft. The problem is, with big software applications, there’s a high chance in which some code in there would be considered as spaghetti – this should just be a rough draft where refactors will be needed later on.

We’ve always been taught in college that when writing essays, they should be clear and concise. This is the same with any profession. Dancers should know their techniques well which will give them the correct posture to perform. Singers should know how their voices work so that their larynx will not hurt. Programmers must always try to make their code readable and understandable. All of the clarity and conciseness takes time and practice.

I do still find myself making things more complex than it needs to be along with others in the team that I work with. Having others catch them and then showing how they’ve simplified it definitely helps along the way.

And the last thing I’ll add is as long as you’ll be working well with others, you’ll find no problems at all.

Until next time!

Brian.

Working with code that should be deprecated

Hi all!

Remember this one time where you had first jumped on the code base when you started a job?

Then remember also how much spaghetti code there was and that it could be a lot simpler?

Chances are that the 5000 lines of code to slim down becoming 500 lines will take quite a bit of time and effort to achieve.

However, that’s okay, especially when there will be more time down the future of a project. Nobody should be doomed working with legacy code.

Therefore, a good way would be to introduce a new concept somewhere in the middle of the project.

Here’s an example: I was pretty new to Xamarin, but I’m not that new to Android development. The concept of how things were working gave me a pretty good idea of how the code base kept working together.

I saw some things that definitely were able to use improvements. We had a colleague learning Android development at the time using fragments in order to reinvent the navigation drawer… there was a header fragment to reproduce the hamburger, back button, menu options, etc…, and body fragment for everything which was pretty much overkill. It was quite messy along with the extra classes and variables that were not really needed.

As I became much more familiar with Xamarin’s capabilities, I decided that it was a good time to introduce the CoordinatorLayout along with the real Toolbar. I reused the body fragments that had all the functionality within them. It didn’t take long to do 80% of the port at the time.

Then there came a design change in which the time to make all of those changes for myself weren’t very feasible to do alone. It’s quite fun to see that there were quick-and-dirty solutions in order to achieve all of the possible use-cases that were needed. Therefore for me, it was better to work with what I had in this case.

Time went on and we started adding a couple more people on the project I was solely working on. As resources grew, I started telling someone else also familiar with the concept about what I was trying to achieve. He started to jump on it straight away, while I’m now fixing bugs towards the day of releasing the app. As the app is still in progress, stay tuned for updates!

Anyway, here are some of the takeaways from this experience that I’ve gained:
– You learn other possibilities to try and make something using an already existing concept out there.
– You become very excited about how another person with more experience actually works on what you’ve started and starts to work along with you.
– You just keep getting better each time.

Therefore, don’t cringe and cry on that new project! Just work with it, and you’ll find an amazing experience along the way.

Until next time!

Brian.

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.