Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I'm going to add new check to existing functionality of class A (method foo()). And I think it would be better to create new class B inherited from A with new private method check() which would be called from foo().
From oop point of view it fits open-close principle, but I'm not sure about Liskov substitution principle.
What do you think? Is it right decision ?
If with check() you change the class behavior of foo() then it perhaps does not fit LSP. If check() makes a side-effect, I would create a new method in B like foo_with_check(). If it is just to check a pre-condition before foo() really does something, you can also do it or consider a pattern like proxy.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Other than the fact that the compiler automatically generates certain functions for Data Class, what is the fundamental difference between them?
In Kotlin, classes declared with the data class keywords simply get some extra methods generated:
equals
hashcode
toString
copy
componentX
Declaring a regular class and defining these methods manually yields exactly the same thing. There is no other difference at bytecode level.
You do however have some extra limitations (no non-property constructor arguments, limitations on inheritance...), but these are just compile-time limitations so that the generated methods behave in a predictable/non surprising way.
The official doc covers everything in detail about them.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
Do kotlin docs say anything about using the also() function just to reduce code lines? I mean doing some unrelated work in the lambda body and not using the it parameter.
For example instead of this:
fun togglePeriod() {
viewModel.togglePeriod()
showStatistics()
}
I've written this:
fun togglePeriod() = viewModel.togglePeriod().also { showStatistics() }
The code should be readable and express the intention. Reducing the line count for the sake of reducing line count rarely results in the readability improvement.
If toggling a period should result in displaying statistics it makes perfect sense to have a separate togglePeriod() method body, it shows the intention nicely. also() doesn't feel as readable.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I am fairly new to Objective C and wonder if most people remove or don't remove unused methods. For example, when I create a UIViewController, there are stub methods that I often don't use, and I want to remove them.
You can remove them if you'd like. However, if you leave them in, make sure a call to super is performed in each method so that you don't lose the default implementation. This means you want your empty (for now) method to extend, not override, the functionality of that same method in parent class.
Edit: Having a method implementation that does nothing other than call the same method on super is equivalent to removing the method all together. Super will then be called by default.
I remove them along with all dead code. They just clutter the working code and reduce readability.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
What reasons are there to pass an argument to an initializer in objective C? I can't find the information.
When a class operates on (and thus requires) an external object. An example of this is NSScanner, which iterates through substrings in an NSString.
When a class can be instantiated using already known information. An example of this is -[NSArray initWithArray:], which copies the contents of another array.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
There is so many option in each programming languages which can be mentioned in the code documentation.
I want to know what are the most important Items which we have to document?
I'd document contracts (this parameter is expected not to be null, this function never returns null, ...) as well as the meaning (this method does that, ...). Besides documenting the API, I'd add comments on pieces of code which are non-trivial but add a significant value to the application (cryptic but real fast, works around a framework bug).
What you document ultimately depends a lor on who will read that documentation...