code-design

How do you pass an executable block as a parameter in Java?

故事扮演 提交于 2019-11-30 12:47:05
问题 Is there a way to pass an executable block as a parameter to a static method? Is it possible at all? For example I have this method public static void someMethod(boolean flag, Block block1, BLock block2) { //some other code if(flag) block1.execute(); else block2.execute(); //some other code } or something like that. It's actually more complicated than this, I just simplified the question. I am trying to refactor my project and I created a generic utility class that contains static methods

Should you avoid static classes?

孤街醉人 提交于 2019-11-30 08:01:59
are static classes considered bad practice? I read an article about this a couple days ago (can't find it, sorry) which basically said that having static classes (especially those 'helper' classes) are typically a sign of bad code. Is this correct, and if so, for what reasons? The abuse of static classes can be considered bad practice. But so can the abuse of any language feature. I make no distinction between a non-static class with only static methods and a static class. They are effectively the same thing, except that static classes allow the compiler to enforce the developers intent (no

When is a class too long?

核能气质少年 提交于 2019-11-30 03:05:24
When is a function too long? is a subset of this question, I think. What are a few good metrics for determining that a class is too long? I'm rerevising a set of code acceptance guidelines for a project with external contractors, and realized I haven't covered this in the past, but should cover this in the future. When it has more than one responsibility. Let me quote Robert C. Martin's Clean Code here: The first rule of classes is that they should be small. The second rule of classes is that they should be smaller than that. ... With functions we measured size by counting physical lines. With

How do you pass an executable block as a parameter in Java?

断了今生、忘了曾经 提交于 2019-11-30 03:03:37
Is there a way to pass an executable block as a parameter to a static method? Is it possible at all? For example I have this method public static void someMethod(boolean flag, Block block1, BLock block2) { //some other code if(flag) block1.execute(); else block2.execute(); //some other code } or something like that. It's actually more complicated than this, I just simplified the question. I am trying to refactor my project and I created a generic utility class that contains static methods that my classes use. You can use Runnable objects: public static void someMethod(boolean flag, Runnable

When is a class too long?

空扰寡人 提交于 2019-11-29 00:44:46
问题 When is a function too long? is a subset of this question, I think. What are a few good metrics for determining that a class is too long? I'm rerevising a set of code acceptance guidelines for a project with external contractors, and realized I haven't covered this in the past, but should cover this in the future. 回答1: When it has more than one responsibility. Let me quote Robert C. Martin's Clean Code here: The first rule of classes is that they should be small. The second rule of classes is

C# How to execute code after object construction (postconstruction)

别说谁变了你拦得住时间么 提交于 2019-11-28 02:43:47
问题 As you can see in the code below, the DoStuff() method is getting called before the Init() one during the construction of a Child object. I'm in a situation where I have numerous child classes. Therefore, repeating a call to the DoStuff() method directly after Init() in the constructor of each child wouldn't be an elegant solution. Is there any way I could create some kind of post constructor in the parent class that would be executed after the child's constructor? This way, I could call to