Kiepski pierwszy dzień w pracy

Ktoś miał wczoraj bardzo zły dzień. Młodszy programista pierwszego dnia pracy w nowej firmie przez pomyłkę… usunął bazę produkcyjną tej firmy. Został natychmiast wyrzucony z pracy, a CTO poinformował go, że najprawdopodobniej w sprawę zaangażowany zostanie dział prawny, bo przywrócenie backupów okazało się problemem.

Ze strony programisty błąd nie był zbyt duży. Skrypt, który dostał, aby przygotować mu bazę testową, na koniec zwrócił dane bazy danych. Programista wziął do konfiguracji jednak dane z dokumentu, który otrzymał – jak się okazało, dane produkcyjne. Uruchomienie testów jednostkowych aplikacji wyczyściło bazę, którą podał w konfiguracji – a która okazała się być produkcyjną.

Prawdziwy problem leży jednak po stronie firmy. Dane do produkcyjnej bazy danych nie powinny być dostępne dla programistów. Konto użyte do zrobienia backupu produkcyjnej bazy nie powinno mieć prawa zapisu do bazy. Po takiej akcji CTO zdecydowanie nie powinien winić o to programisty. Oh boy, długo mozna wymieniać.

Knowing when to stop (DSP17)

You usually know the moment when you start forcing yourself to do something. When it’s a big goal of yours, you try to persevere and finish the whole thing. It’s not always about finishing, though. One of my colleagues that also took part in Get Noticed contest recently decided that he suspends his blogging activities.

Although I’m still able to finish the contest, I decided not to force myself to do it – I’ll still be working on Matchlogger (I actually implemented adding matches today), occasionally I might also write a technology post with the DSP2017 tag for the contest – but my goal isn’t those 20 posts anymore.

Why?

These are the main reasons:

I already achieved what I wanted

I wanted to push this blog into a more technology-oriented one, while still keeping my (mostly) travel and soccer blogging in Polish. That goal is certainly achieved – I’m quite proud of some posts, especially the ones related to Spring Boot. I’m not planning to stop writing about technology now, though I expect the frequency to be lower.

Posts were getting to be more low-quality

I have noticed that and you may have noticed that too – some posts were about resolving specific technical issues, some were just fillers that gave some information, but weren’t that helpful. There was an increase of the latter ones as I progressed with the contest. That was mostly related to lack of time for real development while still trying to succeed with the contest.

Other activities suffered

There are things that are more important than the others. I have an OCP exam scheduled for July which I completely stopped learning for. That’s a commitment that I did at work so I treat it as a more important one. I also have some other commitments or things that I just like doing more than trying to fit in two technology blog posts every week 🙂

Adapt and deprioritize

So I’ve reconsidered my priorities and will be focusing on other things instead. Was it worth trying? Certainly. I now have some new people that I started following and know more about which technologies are currently hot in the community. I’ve also started using Zenhub and Travis CI which will be helpful tools.

And for those of you that aim to finish the contest, good luck! Have fun in Warsaw!

15 things you learn about Java for OCA exam

It is fairly controversial to take Oracle’s Java certificates. One of the very vocal opponents of them in the local Java scene is Wojciech Seliga, who claims that he noticed that people with certificates performed worse at job interviews at Spartez than those that don’t have them (around 4:15). Even with such claims, I decided that I want to pass OCJP this year as I thought that it might be a good idea to review my Java fundamentals knowledge and make sure I don’t miss anything. What surprised me a bit is that with Java 8, the certification process is different than a few years ago and now one needs to first pass an OCA exam. At first, I was a bit dismissive – come on, this is just an exam that shows you how to write for loops – but then I took an example exam and scored… 25%.

So I learned. One important thing to notice is that Oracle’s Java exams test if you can be a compiler – there are multiple cases where they aim to trick you into parsing something that won’t compile. One can be surprised how much caveats there might be and that’s where a good point is to read a good book that prepares for an exam. I read a book by Jeanne Boyarsky and Scott Selikoff and highly recommend that one. The benefit of those preparations is that I structured some of the core Java knowledge – some things I haven’t yet seen in practice (thanks to Flying Spaghetti Monster!) as they aren’t usually good code examples – but at least when I see it in some legacy project in future I won’t run away screaming. Some of them are just something that’s easy and quite obvious, yet it can surprise.

Examples below aim mostly for junior/wannabe developers, but they might be a good refresher also for a developer with some experience. You can see all examples in my Github repository.

1. Can we use a different args name in main() method?

Yes, we can! The name can be any valid Java identifier.

2. Will that code compile?

Yes. We can have only one public class in the file and the file is named just as the public class.

3. What will be the output?

The output is 1. Number variable will be first assigned 2, then 3, then 1. With 3 you can see a static block initialization.

4. That’s a legal number?

A nice, fancy way to write 1000000.000222. Usually used to separate by three numbers, like int result = 1_000_000. Underscores cannot be at the beginning, end and around dot sign.

5. What will that code print?

A trick one. We abuse the fact that assigning a variable returns the variable. The result will be Hello!. It would work that way only with boolean. I highly discourage you from doing that.

6. Which statements are valid?

Line 5 is fine. Same with line 6 – we assign a value to s1 and s2 is declared without a value. Line 7 does not compile – we cannot declare variables with two types in one statement – even if the type is actually the same. Line 8 is okay, it’s just two statements in one line. Line 9 – does not compile. We didn’t provide a type for i4. If we declared i4 somewhere above with a type, it would be fine.

7. Why doesn’t this compile?

Division of two shorts gives us an int that cannot be implicitly cast to short. We need to explicitly do the casting:

8. Does this compile?

Nope. The first line is an int because it’s outside short’s range. Second one lacks an L sign at the end and is too large to be an int. BTW, did you know that you can add plus signs before numbers? Not that it’s helpful, but you can 😉

9. What will that code return?

Well, it won’t return anything because it won’t compile! In Java 8, one cannot switch on long and boolean types. In Java 7, switch on String was added that also previously wasn’t possible.

10. What will be the output?

The output here is:

Weekday
Saturday

Note that we use a static code block for the output. The main trick is that we put default not as the last one (as you would expect) and we forgot to put a break; there.

11. What does this code output?

1234
5

We use a label to break out of two for loops at once. Feels a bit like goto, but can be used only in certain cases.

12. Are these strings the same? What will be the output here?

true
false
true
true
true
false

In this case I’ll guide you to a good answer on string constant pool from StackOverflow. One more thing to notice is that this pool is an implementation of Flyweight design pattern.

13. Can we get a new dog? What will be the result of running this code?

Exception in thread "main" java.lang.UnsupportedOperationException
	at java.util.AbstractList.add(AbstractList.java:148)
	at java.util.AbstractList.add(AbstractList.java:108)
	at pl.mmkay.oca.Level13ListFromArray.main(Level13ListFromArray.java:12)

Arrays.asList creates an array-backed List implementation. In that one we cannot add or remove elements. We could only replace an element. That’s why we needed to create a new ArrayList to add a dog.

(That one is actually a real-life example, in a flat that I currently rent with my wife, we have a contract saying that we cannot have more than 2 pets at a time ;))

14. What will be the output here?

1
-2

It’s good to know what Arrays.binarySearch return values are. If an element is found, it will return an element’s index. If not, it will return a negative number that is

 -1 * index - 1 

where index is what would be the index of this element if it was in the array.

Important thing here is that array must be sorted beforehand! If it’s not, the results might be quite surprising.

15. Which element will stay in the list?

The only one that will stay will be 1. There are two remove methods in List class:

remove(int index)
remove(Object o)

For the first removal, the best match was the one with index. The second one looked for the right object.

That’s all, folks!

Hope you liked the examples. Now you know a bit more on how to act like a compiler.