Tag: Development

Wrap Class – Refactor Legacy Pattern

Wrap Class

When using a wrap method, we want to add a new behavior that can be added around the method, such as before or after the existing code.
However, there are times when we need to add behavior in more than one method, like logging before or after some, or all public methods.

Description and how to apply

There are at least 2 ways of doing it:

1. Using the Decorator Pattern

When using the **Decorator Pattern**, we want to make sure both classes implement the same interface or inherit from the same base class, because we want the subclass to have the same “public” interface. This is because a Decorator class can be used interchangeably between the other Decorators. For instance, having a logging decorator or a performance decorator should be as easy as just pass one instance or the other in a class constructor, and its use should be transparent.

2. Aggregating the current class

If you think you want to be more specific about the class being refactored,
then a Wrap Class just wrapping the existing one should be enough. In this case, you don’t need to *extract interface* since the class is passed in the constructor parameters and used in the method. In this approach, you can do a “method rename”, for instance, if the original method was called *process*, the new exposed method could be *processWithLogging*. Another advantage is that in the constructor, you can pass other information useful for the new behavior, something that in the Decorator Pattern can “violate” the Decorator Pattern principle.

Which one?

From the examples below you can observe the Aggregating solution is more specific than the Decorator. Both will work, but if you need help choosing, choose the Decorator if you think that other changes will happen in the future that can be added creating more Decorators, and/or the updates are just adding new behavior, and probably you expect changes in multiple methods, like logging every public method. Use the Aggregating solution if the change is very specific, just in a few places and you don’t expect great changes in the future for these methods.

Examples

Imagine the following business existing code:

Using the Decorator Pattern

You just have to replace the calls to CustomerPaymentProcessor with the Decorator, sice both implement the same interface.

Probably when refactoring you will need to *Extract Interface* or create an *Abstract Class* to implement this pattern.

Using a Wrapper Class

Here, the solution is specific to the code refactoring being done, it cannot be reused.

Android Adventures – Development Tips – Part 5

How to add the back button on the Activity Action Bar

If the character space stops working with EditBox when using Listeners
Probably it is the google keyboard trying to impose words instead of characters
The one solution that worked for me was to disable the text suggestions

Android Adventures-Development Tips-Part 4

How to remove the divider in a ListView?
What if my text doesn’t fit in a TextView?
How to return values from to the caller Activity?
Main Activity:
Highlight an item in a ListView

Android Adventures–Development Tips-Parte 3

How to shrink activity layout when virtual keyboard is displayed and hides some of the views?
There are several different solutions, one of them is:

– In the AndroidManifest.xml, for the target activity

android:windowSoftInputMode=”adjustResize”

If you use Database Room framework for SQLite management and Kotlin:

If you are using Kotlin don’t create a new method, you can have compilation errors on the generated class

java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
Solution 1: add allowMainThreadQueries

MyApp.database =  Room.databaseBuilder(this, AppDatabase::class.java, “MyDatabase”).allowMainThreadQueries().build()

Solution 2: Use another thread

Solution 3: If you use Kotlin 1.3+ you can use coroutines

How to explore a database in Android Emulator?
– You can copy the db files from the device to your computer and use a sqlite browser

– I use: Android Debug Database: https://github.com/amitshekhariitbhu/Android-Debug-Database

Android Adventures – Development Tips – Parte 2

How to add an icon to a button?
android:drawableLeft=”@android:drawable/ic_menu_search”
How to push a button in a LinearLayout to the right?
The trick in is the empty view

 

How to have an ImageButton transparent?
android:background=”@null”
How to clear all the Spannable styles in an EditText?
val currentPositionsInputText = contentTextEditBox.selectionStart
contentTextEditBox.setText(contentTextEditBox.text.toString())

Give Me Numbers 1.1

icon_512x512_efeitoBuraco

I released an update to my first Android application developed Give Me Numbers.

User Interface Improved, more colorful
You can choose a separator for the numbers generated

 

Screenshot_1545439156

Go Language, First Steps

Go, first experience.

What

We are living in a very rich IT ecosystem, where developers aren’t afraid to explore new languages, tools and to use out of the box thinking. It’s a great time to living in this IT era! Following this trend, and due to some need I had to start to learn
a new language, GO.

Go, also called golang, was developed at Google. The motivation was to have a language less complex than C++.

Go has great advantages, such as:

  • Concurrency system, far superior than Python, for instance.
  • Compiled language, which makes it faster
  • The executable is static linked, which allows to create an executable for the destination platform. It means, it doesn’t need an interpreter.
  • strongly handling exceptions
  • Low memory footprint
  • Very easy to learn
  • Docker is built in Go

Starting

Installation

Just visit the Golang web site
The binaries are here.

Baby steps

First things first: IDEs

Learning

I started with the “official” tutorial A Tour of Go

The big points:

  • Every Go program is made up of packages
  • Programs start running in package main
  • By convention, the package name is the same as the last element of the import path
  • In Go, a name is exported if it begins with a capital letter
  • In functions, the type comes after the variable name
  • When two or more consecutive named function parameters share a type, you can omit the type from all but the last
  • A function can return any number of results
  • Go’s return values may be named. Also called naked return
  • The var statement declares a list of variables (package or function level)
  • Inside a function, the := short assignment statement can be used in place of a var declaration with implicit type
  • The expression T(v) converts the value v to the type T (casting)
  • Constants are declared with the const keyword
  • loop: for – like c, java, c++ but without the parentesis
  • The while> in Go its the for. for {} = infine loop
  • if doesnt need the parentesis
  • the if> allows a statement before the condition: if v := math.Pow(x, n); condition {
  • switch doesnt have the “break”, it is implicit
  • switch without a condition is the same as switch true. Good for long sequence of “if”
  • A defer statement defers the execution of a function until the surrounding function returns. defer add(2,3) PS: call’s arguments are evaluated immediately
  • Go has pointers!
  • The type *T is a pointer to a T value. Its zero value is nil.
  • The &var operator generates a pointer to its operand.
  • Unlike C, Go has no pointer arithmetic.
  • A struct is a collection of fields “type Complex struct”
  • arrays: var a [10]int
  • slices: var s []int = primes[1:4] – A slice does not store any data, it just describes a section of an underlying array
  • A slice has both a length (number of elements it contains) and a capacity (the number of elements in the underlying array)
  • Slices of slices
  • A map maps keys to values: var m map[string]int
  • Go functions may be closures. return func(x int) int { return x }
  • Go does not have classes. However, you can define methods on types. func (v Vertex) Abs() float64 {}
  • An interface type is defined as a set of method signatures. type Abser interface {Abs() float64}
  • Interfaces are implemented implicitly
  • The empty interface – may hold values of any type
  • A type assertion provides access to an interface value’s underlying concrete value. t := i.(T)
  • ype switches switch v := i.(type) { case T:…
  • Go programs express error state with error values. type error interface {…
  • The io package specifies the io.Reader interface.
  • A goroutine is a lightweight thread managed by the Go runtime. go f(x, y, z)
  • Channels are a typed conduit through which you can send and receive values with the channel operator, <-.
  • Like maps and slices, channels must be created before use: ch :=make(chan int)
  • Channels can be buffered.
  • The select statement lets a goroutine wait on multiple communication operations.

Conclusion

In a first impression I would say its a very good language for scripting. No complications around object oriented concepts. Just got the best of native languages and modern languages. While reading some articles everyone has the same option, Go its very
suited for concurrent applications, since its very easy to use channels, select and goroutines.
For sure, a very good challenger to my usual preference on python

Managers, it is always about people!

 

types-of-exerciseAfter years of developing, team leader and project management experience I realized that all good decisions, even the technical ones must include all the people. Well, “everybody” knows this, every project manager, team leader, manager,etc. Really?

Lets focus on the team developers.

 

Imagine you have a project to develop, a software product. You have a team leader and some developers that you will assign to the project. You want to use the new technologies like HTML5, JavaScript and lets add Java, the software product will be an Android app.

The typical decision will be something like this:

1) I have developers and a senior developer team leader

2) They all know Java

3) They all know about HTML5 and JavaScript, because “everybody” knows about HTML and JavaScript. Right?

4) They all know about java and that is the language of the android, and know what is an Android phone, it will be a challenge!

 

Lets remake the decision looking from outside:

 

1) You have developers and at least one of them it’s a Senior.

– Great!

 

2) They all know Java.

– The problem starts here. What they know about Java? The language and some algorithms (even with some years of experience), or they understand how to apply Java in the context of mobile Applications? If this is the first time they will develop mobile applications, they don’t understand the architecture of these applications, how to develop the components, the events, actions, how the hardware responds and send events to the application. The thing here its not the details of the mobile android development, it’s the lack of “mind set” the team will need before start to code, and the result will be most certain a bad quality product, who will need a lot of fixes and rebuilding. (been there)

 

3) Yes, every developer who have developed before web applications knows about HTML and JavaScript, everybody knows WHAT IS, and the syntax and some tricks, but few, very few understands the JavaScript paradigm and good HTML. Be aware of this before anyone tells that he knows about this subjects. It was only in the least 2-3 years that the JavaScript boom has started seriously, and Senior JavaScript programmers are a very small team. Before this language was only used for events in the HTML, now we have frameworks developed in JavaScript, we even can use JavaScript to create servers like “Node.js”!

 

4) It will be a challenge!

– No, it will be a disaster for the company and/or for the customers! The lack of know how in the middle of the project will arise and show all the architecture problems not prevented in the analysis phase or by the Senior. And here you will have 2 options: Remake the whole product from the start, which sometimes its not a bad idea (only for project managers), because the team already acquired the know how and know how to develop in a better way and know what to develop. Or fix the architecture, and this for me it’s the real challenge and here I thing “It will be a challenge” makes all sense! Its like trying to fix an already built house into other blueprint, good luck! (you will see its better to destroy and build again)

 

What’s my point, here?

 

This was just a simple example, to show that the most common way of thinking can lead to big problems, which by the way are always happening.

Before a great decision to be made in the software industry, first take in consideration the people you will choose. They are people with a specific know how and mind set, and to change that it takes time and it is a process you must consider. You can say, you will put them all in training classes. Great, but don’t expect that some dozen hours will create a miracle and prepare them for big projects. Training just gives you an overview sometimes shallow, other times deep, but after the training the people must play for some time before entering the “competition”. You can see this in sports teams. They are professionals, they play for many years, but when something changes, like others players, tactics (projects), coacher (team leader) they need to practice before the real competition to be successful, or you will ear from supporters “they had no time to practice that’s why they are playing so bad”. Probably you say the same about your sports team. Remember those words in your company and your team.

Once someone said, that 80% of the quality of a product comes from management, not the workers, because they need conditions to develop a quality product or project. These conditions include human resources management, understanding human behavior and acting accordingly!

 

What to do?

 

I will not give you a magical solution, from years of experience as a developer, trainer, team leader, architect and project manager, I can help you to take a decision involving all these variables, but as a manager, project manger or whatever manager you must open space for your developers talk about the difficulties and frustrations they have, and not throwing a “it’s a challenge for you guys” like  a magic sentence that in the future will bring its fruits (demotivation and consequently quitting), and I will not talk here how much it costs to bring a new member to a team (think about a new player in a team, even expensive he need time paid by the sports team to adjust to the new environment).

If you are a developer, be more open to your managers, and ask them time to practice, to develop small “challenges” besides the training and before starting a big project, it will give you know how, confidence and experience for a bigger project. Your company its not wasting money its an investment. I think it will cost more to rebuild or fix a bad product than to spend time before doing some “virtual work” and then do the real work. A soccer team plays 1-2 times a week, and receive money from the public, but during the week they spend money training.

Great Technical Books I have read in the last months

transferirAs a software architect I must have a broad knowledge of several subjects so I can have several tools in my brain to create the better solutions with the right tools. I’ve read (and still reading!) a lot of books, so this post will show some of my last readings with some comment on each of the books.

In future posts I will show other interesting books, sometimes not directly related with software but they gave me another way of thinking about systems, patterns and algorithms that helped me to be a better professional, as a technician and leader whenever necessary.

Hope it can be helpful to your tech and soft improvement in the software industry.

If you know others books you have read who can enrich my knowledge, I will appreciate your comment!

How many methods you have in your classes with: Logging, transaction, validation, exception management? How about remove the code from those methods, write it in another class(es) and add just attributes (or java annotations) and surprise! Your code its so much cleaned! This book has improved the quality of my code, architecture and thinking about applications!
Dependency Injection its about building great software architectures. Its about building software like a house, its about decoupling and loosely couple. Its about building testable software, its about software with quality.
A great book about NoSql! Not too deep, but with the enough information to understand the NoSql world. Even with years in database systems I’ve learned new ways of thinking in this book.
The reference for programming windows, now for windows 8. You can’t go wrong with this book.
You want to know about SCRUM, agile and how it works, this is a very good book to start.
Best book for android development.
I know C, C# and C++. With this book from these guys, I added Objective C to the Cs languages list!
Great book about iOS development. Starts from the basis and then develops some applications and of course your knowledge.

Bad Behavior has blocked 105 access attempts in the last 7 days.

Hyper Smash