The following article, which talks about object orientation, mainly focuses on design over design patterns. Which is the enemy? Generic over used, incorrect solutions or well thought out and well constructed objects?
I don't think many, if at all any person has actually done oop and not come across design patterns.
Design patterns are bad design. They don't solve any problem they claim to. If anything they do the opposite. I use this site as reference http://www.oodesign.com/
Everyone knows the singleton was possibly the worst design pattern, and claimed to be an "anti-pattern" I don't think this is true, I think all are anti-patterns.
Example: A factory design, which is one of the most used patterns breaks SOLID. The factory depends on whatever it makes, hence if the thing it makes changed then the factory becomes useless, if the thing is deprecated or deleted then the factory should be the first thing to go. Anything which uses the factory suddenly falls apart. Yet the factory, or something which creates objects is integral to most programs.
I do not claim this proves anything, but it fuels this blog.
I think the solution is to use oop and not rely on patterns. To learn how to solve problems and not how to use "generic" solutions. To spend time in designing and creating a lasting solution rather than save time using a hashed, over used and let's be honest, dull solutions.
How is this done. I think the name says it all really. Object Oriented. When one creates an object they should ask themselves, "Is this really an object?" e.g. UserBuilder... are you really an object?
Example: I have a User object. When someone wants to create a new user how do I do this? Typically one might use a builder or factory to create the user and then process this using something such as a command design or a strategy design, dependent on how complicated the system is. But what is wrong in letting the User do the work?
What is better user.create() and user.save() or userFactory.create() and userStratergy.save().
One might argue that the factory and strategy do far more complicated things and should be segregated In this example I disagree.
If I change the user then my save and create method can changed at the same time. If I delete my user or replace my user than the create and save method both change at the same time. The user has to do more than one thing, but at the end of the day the user object and all information about the user are self contained.
I like this to think of this as objects doing their own dirty laundry. Beyond breaking the S in SOLID, I don't see anything wrong with it. Does it even break the S? The user can save itself and create itself... Is that a bad thing? If designed correctly then the User object serves more than 3 different objects all claiming to do different things, but all relying on the user even existing.
There is more to oop than solid, but this is something in which just about everybody remembers and something in which everyone uses to prove code is bad (I am guilty of this). In my opinion design which breaks techniques such as solid, but last longer and are easier to maintain and understand are better than patterns which fall apart, lead to arguments over names and what they do.
(Inspired by +martin odersky's +Coursera course with the Nothing object and the Nil objects, unless I have missed something that is a brilliant way to remove null pointer exception from my code)
One other thing oop should solve is the ability to remove nulls and null pointer exceptions from code. The only reason we worry about something being null is because we fear we have not created a new object. But imagine if the default behavior used was a useful object. Whilst there was nothing there, it can still be acted upon.
Similar to the null object pattern but with less nulls. The moment I introduce the word null, either in test or in code (outside of the object name) I should create the null object instead.
In the example above let's create the NullUser. If one wants to do user.save() and user was null object you would get a NullPointerException in java or in Scala a message about not being able to act upon Nothing or Nil (which ever is appropriate) . What if instead user.save() threw a "can not save the null user"? or user.save() "creating a user first as this is the null user." The latter might be a little dangerous, but at least your system didn't have a crippling exception which gets replaced with if(user != null) user.save(). Also if the latter is designed well then it is superior to even throwing an exception.
Maybe I missed the memo which cleared this up. But if people used object orientation, with the thought in mind "do your own dirty laundry" then you wouldn't need to spend time arguing about whether we should use an assembler, builder or factory, all of whom introduce debt and aren't good ways of oop.
If design patterns are the way to code oo languages then designing something write should reveal these patterns within the code. They are worth knowing, but are they worth applying?
151 Code
Saturday 4 May 2013
Saturday 15 December 2012
Project Tools
Tools
I have been using Dropbox for quite a while and hadn't really thought about using any other tools. Mostly used it to save things whilst changing OS to pick one I like best. Then I started useing Chromium and found Google Drive, Google+, Google Calander and all the amazing tools you get. Which is great because I have a Gmail account and Android device. It also works really well on the iPad. Then I found Acrobat which saves and creates PDFs.Too many tools and no one really using them to make programs (as far as I am aware). I thought I would just mention the tools I am using, would be using and how I would use them to work in a team. Most important things I need is it to be simple and easy to use. It also has to be free.
Dropbox
https://www.dropbox.com/
Has really nice interface, everyone knows how to use it just by looking at it. You can also download the folder program which makes them look pretty, and those who use git understand what the little pictures mean as well. You can also get a menu tool so you don't have to create a shortcut to that folder. as well letting you know when your files are uploaded and if anything changes you know straight away. Something great for teams who are working on the same project to have. There are also loads of apps for it, and some IDEs actually let you use it for your workspace. 2GB free initially but it can increase with the more people who you get to join Dropbox. Apparantly up to 8GB, but you shouldn't need the much for some projects. You also get history of files which is great for teams. I would use this to put the code which everyone uses frequently
Has really nice interface, everyone knows how to use it just by looking at it. You can also download the folder program which makes them look pretty, and those who use git understand what the little pictures mean as well. You can also get a menu tool so you don't have to create a shortcut to that folder. as well letting you know when your files are uploaded and if anything changes you know straight away. Something great for teams who are working on the same project to have. There are also loads of apps for it, and some IDEs actually let you use it for your workspace. 2GB free initially but it can increase with the more people who you get to join Dropbox. Apparantly up to 8GB, but you shouldn't need the much for some projects. You also get history of files which is great for teams. I would use this to put the code which everyone uses frequently
I called this title google and I am not going to add links. There is a lot that Google has to offer. If there was a bash terminal I would convert to it and probably never leave Google. There is Chrome which now has awesome plugins. Get a Gmail account and have loads of fun with it. But I want to talk about Google Drive and Google+. Google Drive has a free 5GB storage, but it comes with amazing office tools. You can do just about anything document wise. I would use this for any spreadsheets and presentations and documents. Nice place to put things. I haven't used it much but the potential of everyone putting in their evidence and teaching tools. Google+ is really nice. It is very similar to Facebook and Twitter. However you can create circles, share articles easily and upload pictures. If you are working on something like a whiteboard and take a picture, Goolge+ is really easy place to put the picture. Most phones come with it. Unlike Facebook and Twitter you can easily split up friends, family, work and anything else you are interested in.
Acrobat
https://files.acrobat.com/
I only recently found this. You can put up to 5GB of stuff on this as well. I find PDFs very useful and have plenty of ebooks. This is really great place to put magazines and tutorials. If you had a team who have some sort of standard like Clean Coding you could put that there and everyone could read it, language books would be great to put in here as well. You can also highlight and modify PDFs really easily. You can also read the PDFs on the train or bus or whever and not have to flood your devices with the same copy of your books.
I only recently found this. You can put up to 5GB of stuff on this as well. I find PDFs very useful and have plenty of ebooks. This is really great place to put magazines and tutorials. If you had a team who have some sort of standard like Clean Coding you could put that there and everyone could read it, language books would be great to put in here as well. You can also highlight and modify PDFs really easily. You can also read the PDFs on the train or bus or whever and not have to flood your devices with the same copy of your books.
Skype
https://www.skype.com
I have been using Skype for such a long time. Used it to replace MSN because for some reason the webcam wouldn't work on MSN. Skype has some really nice things, but what is nice about it is that it is simple. You can also call phone numbers which is good for a team. You can even talk to several people and conduct group meetings without having to leave your room. All for free as well. It works on loads of devices as well. Can't say no to free phone calls (if you are calling another skype account).
I have been using Skype for such a long time. Used it to replace MSN because for some reason the webcam wouldn't work on MSN. Skype has some really nice things, but what is nice about it is that it is simple. You can also call phone numbers which is good for a team. You can even talk to several people and conduct group meetings without having to leave your room. All for free as well. It works on loads of devices as well. Can't say no to free phone calls (if you are calling another skype account).
https://www.facebook.com
No one really uses Facebook for anything other than abusing their friends and family or putting pictures up. But you could use this for something like organising events, creating groups where you can put links to everything above. Everyone has Facebook as well, so if you already using it why not just create a group for everyone working on a project?
No one really uses Facebook for anything other than abusing their friends and family or putting pictures up. But you could use this for something like organising events, creating groups where you can put links to everything above. Everyone has Facebook as well, so if you already using it why not just create a group for everyone working on a project?
https://www.facebook.com
This is nice, using @ and # is pretty cool. It works nicely with Facebook. Unlike Facebook it isn't as big, which is good on the go. I would probably use this for really short group emails or something similar. It is nice and easy to use.
I would suggest that using all of these is better than just one. Imagine if Facebook got shut down, then you have Twitter as back up. So using all these tools is better than using just one. But if you use it correctly then you won't be overwhelmed with tools.
This is nice, using @ and # is pretty cool. It works nicely with Facebook. Unlike Facebook it isn't as big, which is good on the go. I would probably use this for really short group emails or something similar. It is nice and easy to use.
I would suggest that using all of these is better than just one. Imagine if Facebook got shut down, then you have Twitter as back up. So using all these tools is better than using just one. But if you use it correctly then you won't be overwhelmed with tools.
Cloud9
https://c9.io/
I don't actually like this that much because I like working from the command line and using vim. Though there is apparantly a terminal here. The other thing is if you aren't using Java, Ruby, Python or just designing a webapp then this isn't amazing. Don't let that put you off because the website is genuinly amazing. It is a workspace and you can have 1 free private workspace. I don't like Eclipse and Intelij but they do let you to do work fast. This is actually a little bit faster than either of them. Also you can run it on any laptop or computer, you don't need to install and download new tools. It does support C++ format and many other file types. But you can't run those programs like you can run the Java stuff. You can also deply things, which is nice. It looks really good. I tried a few others and didn't like them, they just looked annoying. This is good. I would like to move to it but it might take a little bit of time. Give it a go, if it works out for you great. It would be perfect for teams because you can share workspaces. Paried programming anyone?
https://bitbucket.org/
So this is a lot like GitHub. I like GitHub because you don't need git to use it. You can download the zip. But BitBucket lets you make up to 5 free private projects, it also gives you Jira (don't like it but it is good for teams to log stories and or bugs). When I finally get my project into action I will be using this more I should think.
There are loads more tools. If anyone knows of some I have missed which should be on here then leave a comment. But these tools let you be more agile because they are almost platform independent. You can use most of these tools on loads of mobile devices. You don't have to go to an office, if you have a laptop then everyone can get working straight away. You don't have to spend a week setting up your workspace. Just get Google chrome (or chromium) and get going. If you could use all of these things (easily) on the iPad then you don't even need a laptop! How amazing would that be!?
Successful Agile environments can deploy work fast and in iterative steps. These tools help with that and they are free. So you don't need to spend thousands of pounds, they also work well with the tools you already have, and you can choose the ones you like best.
So what are you doing? Just get on with your project. You have no excuse when it comes to technology.
I don't actually like this that much because I like working from the command line and using vim. Though there is apparantly a terminal here. The other thing is if you aren't using Java, Ruby, Python or just designing a webapp then this isn't amazing. Don't let that put you off because the website is genuinly amazing. It is a workspace and you can have 1 free private workspace. I don't like Eclipse and Intelij but they do let you to do work fast. This is actually a little bit faster than either of them. Also you can run it on any laptop or computer, you don't need to install and download new tools. It does support C++ format and many other file types. But you can't run those programs like you can run the Java stuff. You can also deply things, which is nice. It looks really good. I tried a few others and didn't like them, they just looked annoying. This is good. I would like to move to it but it might take a little bit of time. Give it a go, if it works out for you great. It would be perfect for teams because you can share workspaces. Paried programming anyone?
BitBucket
https://bitbucket.org/
So this is a lot like GitHub. I like GitHub because you don't need git to use it. You can download the zip. But BitBucket lets you make up to 5 free private projects, it also gives you Jira (don't like it but it is good for teams to log stories and or bugs). When I finally get my project into action I will be using this more I should think.
There are loads more tools. If anyone knows of some I have missed which should be on here then leave a comment. But these tools let you be more agile because they are almost platform independent. You can use most of these tools on loads of mobile devices. You don't have to go to an office, if you have a laptop then everyone can get working straight away. You don't have to spend a week setting up your workspace. Just get Google chrome (or chromium) and get going. If you could use all of these things (easily) on the iPad then you don't even need a laptop! How amazing would that be!?
Successful Agile environments can deploy work fast and in iterative steps. These tools help with that and they are free. So you don't need to spend thousands of pounds, they also work well with the tools you already have, and you can choose the ones you like best.
So what are you doing? Just get on with your project. You have no excuse when it comes to technology.
Wednesday 2 May 2012
Mockito Example
A Nice Glass Of Mockito Please
The question asked when using TDD or even writing tests is why would someonewant to use mocks? Well suppose you have the following class:
- package blog.code;
- public class Hello {
- public String hello() {
- return null;
- }
- public void
greeting(String greeting){ - }
- }
and the other is a function which returns a hello string. As you can see they don't.
But what if I didn't know about that? And now I want to use them in a new class.
Well I would need to mock them and make them spit back the correct answer.
Let's use TDD. Test first and using mockito, if you are using maven then grab
the mockito-all dependency, it is easily compatible with Junit. There are
plenty of other API's such as easymock, jmock and mockit but I like this one
as it is simple.
- package blog.code;
- import static org.junit.Assert.*;
- import static
org.mockito.Mockito.*; - import org.junit.*;
- public class HelloWorldTest {
- HelloWorld helloWorld;
- Hello mockHello;
- @Before
- public void before(){
- helloWorld =
new HelloWorld(); - mockHello =
mock(Hello.class); - when(mockHello.hello()).
thenReturn("Hello"); - helloWorld.
setHello(mockHello); - }
- @Test
- public void
testHelloWorldReturns
HelloAndGreets(){ - String helloWorldString =
helloWorld.helloWorld(); - assertEquals(
helloWorldString,
"Hello"); - verify(mockHello).
greeting(anyString()); - }
- }
First of all I have to import mockito (line 4).
Then in the Before You have to set up a mock.
In this case I have called it mockHello, after the class Hello. Look at line 14.
It was that simple to set it up "mockHello = mock(Hello.class)" This acts
almost as if you had called a new version of hello. Only when I ask it to do
something I can make it return a dummy version, which means Hello doesn't
have to do the things I want it to. You can then proceed to stub what you like
(almost, you can mock static things, new things, private things and final things) .
So whenever this any test sees mockHello.hello it will return the string "Hello"
(line 16). DON'T FORGET TO SET HELLO WITH THE MOCK.
That is pretty important, if you don't set it with the mock then it won't work.
That's the before, now on the the real test. couldn't be easier, like normal with
Junit all you have to do is assertEquals on the string you expect and on then function.
However what mockito will let you do, which you can't do with Junit without
changing voids to booleans. You can "verify" (line 23). you
"verify(mock).method" you can see here I also have a function called "anyString()".
This comes with mockito, anyString(), anyObject(), anyInt(). It will be the most
appropriate null value or be anyString/Object/Int. If you use anyObject you would
have to cast the object, e.g. "(User) anyObject()"
So now that this compiles but fails (well it will compile if you have the method shell) .
All we have to do is make the tests pass.
- package blog.code;
- public class HelloWorld {
- private Hello hello;
- public String helloWorld(){
- String helloString =
hello.hello(); - hello.greeting("NYtn H3r3")
- return helloString;
- }
- public setHello(Hello hello){
- this.hello = hello;
- }
- }
Note on line 9 I have put in anyString and the test will pass. I could actually make this
test pass even faster. If I were to just return "Hello" and call the method.
What you could do is verify that "hello" is called. You can also check if it is called in
the correct order. You can do loads of things to not only get 100% code coverage but
to also check that your functions are doing what you thought they were, as opposed to
just getting 100% code coverage (assuming you don't have static stuff...)
I was thinking I would make the next blog about powermock however I am still
trying to compose a good example. With this, power mock and Junit you can actually
get 100% code coverage. Well that is almost true, you should learn how to use reflection.
I am thinking that I will have to make the next blog on TDD. Because it is so awesome.
Enjoy mocking, Solid.Pope
Subscribe to:
Posts (Atom)