Btw, remember that boss I had who told me not to test my code? Just started in a new job. I have a week or two until my first proper project, so I can spend them learning whatever I want. I told them I wanted to spend everything only on learning to test properly, and they were ecstatic about it. I love it here already :D If anyone has any good reads, guides, whatever on the subject I'm grateful.
The Big Programming Thread - Page 958
Forum Index > General Forum |
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. | ||
Excludos
Norway7678 Posts
Btw, remember that boss I had who told me not to test my code? Just started in a new job. I have a week or two until my first proper project, so I can spend them learning whatever I want. I told them I wanted to spend everything only on learning to test properly, and they were ecstatic about it. I love it here already :D If anyone has any good reads, guides, whatever on the subject I'm grateful. | ||
Acrofales
Spain17179 Posts
On May 03 2018 03:00 Manit0u wrote: Private static is fine for the most part. Public static is where things get hectic. Here's a nice and brief explanation: https://dzone.com/articles/why-static-bad-and-how-avoid Edit: Also, speaking of DI and encapsulation I think that Symfony project in PHP world is doing a great thing. They're basically forcing you to do nice and clean DI as they won't allow typehinting arguments to constructors with concrete classes, you can only specify interfaces and then inject whatever you want that implements this interface. Very neat. You used php and great thing in the same sentence. And it wasn't "it'd be a great thing if php burned in everlasting hell"... | ||
Manit0u
Poland17041 Posts
On May 03 2018 03:23 Excludos wrote: You can make anything look horrible if you really try. Static has its place in programming. I've had classes with thousands of lines being a static only class, as the alternative would be to repeat those thousands of lines throughout the entire project. Like everything else it's just a tool; If you use it badly it's going to make bad code. Doesn't really have anything to do with the language being oop or not. Btw, remember that boss I had who told me not to test my code? Just started in a new job. I have a week or two until my first proper project, so I can spend them learning whatever I want. I told them I wanted to spend everything only on learning to test properly, and they were ecstatic about it. I love it here already :D If anyone has any good reads, guides, whatever on the subject I'm grateful. https://www.obeythetestinggoat.com/pages/book.html#toc http://www.growing-object-oriented-software.com/toc.html One is for Python, the other one for Java, but they're great books in general to get you going. As a bonus, you'll be creating real, working software while going through them. On May 03 2018 03:42 Acrofales wrote: You used php and great thing in the same sentence. And it wasn't "it'd be a great thing if php burned in everlasting hell"... I worked with PHP for 3 years. This language has a lot of shitty features but nowadays the bad rep it's accruing is because of several reasons: 1. It's easy to pick up so plenty of people who have no idea what they're doing are producing a lot of shit code. 2. Because of the above there's a ton of shitty websites that run it. 3. WordPress, which seems to be the goto framework for people who just want to put up their website quickly is everywhere and it's complete garbage (it can't be good if it has over 20 years of accumulated technical debt, compare that to Symfony which has 2 weeks of tech debt...) But despite all that it's still going strong. There are some really good frameworks for it that let you do enterprise-grade software and promote good practices - just check out Symfony and its derivatives, like Silex and API Platform, there's a reason why Symfony components are becoming the de-facto standard library for PHP (practically every single project out there is using Symfony console and all the web frameworks are using their HTTP components like Request, Response, Session etc.). PHP has gone a long way even during the 3 years I was working with it. It has improved vastly in many areas, from performance to code quality (they even added almost static typing to it if you wish to use it). Seriously, I'd pick PHP over say JavaScript any day of the week (but I hate JavaScript and its ridiculous features so I'm biased). | ||
TheEmulator
28057 Posts
On May 02 2018 01:33 nunez wrote: this is me almost. except i use i3 instead of xmonad, and emacs instead of spacemacs. i switched from vim recently, very fun and painful. the way i use emacs tends to make me rely a bit less on the windows manager and the shell, which maybe is a bit worriesome, and un-unix like. maybe if used emacs server / client instead this would not be an issue. however i chose the easier path. i try to homogenize hotkeys (f.ex. navigation) and theme across the programs i use in a programming context (windows-manager, text-editor, shell), but i don't bother customizing other software. i have a nice background photo that reminds me of relaxing in nature, the opposite of working with programming, but my tiling window manager makes sure i never get to see it. i am planning to transition to guixsd from arch linux. but i did not find the time yet. i really do like arch, but i want to try something new. hopefully it will not be as short-lived as my excursion into gentoo. I dual boot Windows 10 and Arch/i3. Working with i3 ( or any good WM tbh) feels so fucking good. I've tried bspwm and Awesome as well, but I think i3 has the simplest config to get started with. On the other hand I can never get into emacs. Still use vim and vscode. I was really into "ricing" i3 for a bit, but it became too much of a chore that took away from important shit (if you don't know what that means check out r/unixporn) | ||
WarSame
Canada1950 Posts
On May 01 2018 13:44 berated- wrote: I haven't played around with dagger a whole lot, but, I'm a pretty big spring fan boy, so maybe that makes me qualified to help? Dependency injection is most often used to help implement the Inversion of Control pattern. The entire point is that one does not know where or how their dependencies are constructed, just that they need the service. The base example I always go back to is a print service in java. I don't care how the printer implementation is done, I just know that I need someone that can print, so I'm going to request such a service with my @Inject. The reason I point this out, is when you say you just need a key generator with certain parameters, it feels like it doesn't line up with IoC in the way previously mentioned. IoC would work best if you just said, hey,I need a key generator, not with certain parameters. That's where in the Java world where we love our patterns, the factory pattern comes in. In your case you don't just need a keygenerator, you need one who knows how to make key generators. You could inject that in, and then use that method. All of this is kind of eclipsed by the fact that you then mention statics. This isnt' to say that statics are bad, it's just that, they aren't often used with DI. One of the reasons that DI is so popular is that when you aren't specifying your implementation, but yet the interface that you need -- one can provide a completely different implementation under test that can skip hard integration and allow for easier unit testing with mocks or test doubles. Specifying a concrete implementation into a static field doesn't help that much for test-ability in most cases. If you would like to get into more specifics I would be glad to ramble more. My problem is that I need to test a function that uses a KeyGenerator in a subfunction. Automated testing means I can't authenticate. Therefore, I wanted to pass in a KeyGenerator that did not use user authentication when I was testing, and one that did require authentication when I was using the app. I thought DI was useful for what I wanted, but maybe I was barking up the wrong tree. My code is below, feedback is welcome. I have a ton of statics because I thought that made sense. My last job was C so I guess I was just used to it. Git repo/branch AddEthActivity, one place I use it. Look at authenticateMobileUser EncryptionTest is one test that involves it Encryption is where it actually happens On May 02 2018 05:24 Blisse wrote: For your case, your Application needs to store an instance of the AppComponent object, and you'll do something like `Application.getInstance.inject(this)`. I've never tried injecting into static fields, but I wouldn't be surprised if that doesn't work. You're basically subverting the whole point of using a dependency injection framework. IMO this (https://www.techyourchance.com/dagger-tutorial/) is a simple Dagger tutorial that should get you running. Vasiliy has good ideas about Dagger. I would also check r/androiddev if you have any Android specific questions, more actives there. Thanks, I'll look into it. On May 02 2018 07:14 Manit0u wrote: It depends on how you use them, but most of the time static properties (methods, variables, attributes, you name it) break encapsulation and introduce global state, which you usually want to avoid. Do you really, really need a method to be globally accessible? Every time you want to use the static keyword in an OOP language you should ask yourself some questions first: 1. Do you really need a method that can be called without instantiating an object? 2. If your answer to #1 was "yes" then you must ask yourself if this breaks the single responsibility principle? 3. In any case, maybe this code could be moved to a different class altogether and not make it static? The only reasons I've found to use static props was for some helper classes, preferably making them abstract so that they're never instantiated (man, sometimes I really wish you could use both final and abstract at the same time). Usually they're used for storing some kind of system setting stuff that has to be hardcoded. Perhaps I'm dead wrong, but that's my take on it I'll consider this but I don't think I know enough to correct my architecture to what you're saying. | ||
Blisse
Canada3710 Posts
If you're just writing unit tests, your problem isn't DI here, you're just missing abstractions. If you need to provide different behaviors in tests, you need to create abstractions that you have control over (your classes vs Android classes). You need to tell Android's KeyGenerator to give you normal values in the app, and XYZ values in tests. Since you can't do that, all you need to do is abstract away the usage of KeyGenerator. In this case, instead of using KeyGenerator, you create an interface, then 2 implementations, 1 calls the Android KeyGenerator, 2 is a test implementation that responds with whatever values you want, so that you can test the other parts of how your encryption code behaves. You should really invest in applying any kind of Android architectural patterns. They provide a framework for solving these kinds of problems (testing really). Especially if you also want to write UI tests. | ||
WarSame
Canada1950 Posts
| ||
Manit0u
Poland17041 Posts
On May 07 2018 05:19 WarSame wrote: The problem is that I need to access KeyGenerator a few function levels down, so I can't simply call 2 different interfaces. Once again, I face the problem that I need to get access to an object levels down the tree from something above it. That was where I thought DI came in. What do you mean by "few functions down"? To me it looks like a case of broken Law of Demeter... | ||
WarSame
Canada1950 Posts
| ||
Manit0u
Poland17041 Posts
On May 07 2018 06:31 WarSame wrote: Yes, it is because the KeyGuard relies on some "authenticated" state being set. I don't know of any good way around that. Is this a part of some Open Source? Is the API public? It would help immensely if you could perhaps point us to this KeyGuard class documentation/definition. | ||
WarSame
Canada1950 Posts
KeyGuardManager createDeviceConfirmCredential starts an Intent that prompts the user to authenticate using their normal phone login credentials. | ||
Manit0u
Poland17041 Posts
On May 07 2018 09:51 WarSame wrote: Sorry, I did way back. KeyGuardManager createDeviceConfirmCredential starts an Intent that prompts the user to authenticate using their normal phone login credentials. Well, yes, but you can completely ignore this Intent if you wish. You can either do the standard startActivityForResult(Intent, int) or wrap it around your own stuff and do with it as you please (so you can mock user passing authentication or failing authentication easily). At least that's how I see it. | ||
Blisse
Canada3710 Posts
Using a standard Android architectural pattern gets you a long way towards writing testable code. Again, I would highly suggest doing that. | ||
Hanh
146 Posts
On May 06 2018 05:05 WarSame wrote: ... I'll consider this but I don't think I know enough to correct my architecture to what you're saying. If you are not attached to Java/Android, you could try using React Native/Redux. Java/Android does not favor any app architecture nor does it have a dominant design pattern so you are pretty much left on your own. RN on the other hand is mainly either redux or mobx. You could quickly try them both and see for yourself the advantages of reactive programming or mvvm. Both have excellent testability because they separate business logic from UI. Today you could fix your problem with DI and introducing interfaces, but it seems that you would be only patching rather than attacking the bigger issue. | ||
Blisse
Canada3710 Posts
Java/Android does not favor any app architecture nor does it have a dominant design pattern Android has favoured MVP for the last half decade, moving slowly towards MVVM in the last year. | ||
Hanh
146 Posts
On May 08 2018 04:51 Blisse wrote: Android has favoured MVP for the last half decade, ... Any reference? AFAIK, Android does not have any particular facilities for MVP, unlike WPF has for MVVM and I haven't heard about an official recommendation about it from Google either though now they have some pages on MVVM . | ||
Blisse
Canada3710 Posts
At the very least it's been some sort of MVP, MVI or MVVM the last while. One of the Android leads (forgot the name) said that she designed Android to be essentially architecture free, so it makes sense Android itself doesn't force any specific architecture, but recently all the docs are definitely leaning MVVM. I think it'd be really invaluable learning the ins and outs of at least one kind of app architecture, whatever you choose. | ||
WarSame
Canada1950 Posts
| ||
Manit0u
Poland17041 Posts
On May 10 2018 06:14 WarSame wrote: Could you link some examples of MVP and MVVM? I have not spent much time in my life looking at architecture in practice. I had a shitty Software Architecture course in university. https://android.jlelse.eu/android-mvp-for-beginners-25889c500443 MVP is a pattern derived from MVC for the web. It's been around for a long time and for a reason. It is also a good idea to read some on Hexagonal Architecture while you're at it: http://fideloper.com/hexagonal-architecture https://herbertograca.com/2017/11/16/explicit-architecture-01-ddd-hexagonal-onion-clean-cqrs-how-i-put-it-all-together/ | ||
WarSame
Canada1950 Posts
| ||
| ||