What if there is specific logic for one particular value of an enumeration class?

Background: I have seem some argument for using enumeration classes instead of enum in C#, in particular, this section from a book available at MSDN. On the references there is this “Enums are Evil” article which in turn makes quite a good point on the matter.

One of the reasons quoted is particularly convincing:

Imagine the following phone call between Frank, a customer, and Darren the developer:

Frank: Hi Darren; I need a new value for the person status about his financial standings. We have currently Low, Medium and High, but we need ‘undefined’ as a new value.

Darren: I see.

Frank: When can you implement that?

Darren: Uups Frank. We just finished our sprint this week. The earliest we can plan this is for the sprint after the current one. That takes about 5 weeks.

Frank: What? It’s just another value not a whole new functionality!

Darren: Calm down Frank; we have these cool enum’s in place that saved us a lot of development time. To extend them I have to add it in the code and create a new build. We have to plan it for the next sprint.

Frank: Are you nuts? I don’t know what you talking about. I’m an ordinary user and just want to have another value on the user interface.

Darren: Sorry for that. I can’t do it earlier.

I’ve been through that already and it really feels like it is much more natural to allow the user to persist to the database the values he uses, like CardTypes as shown in the first MSDN link I provided:

public class CardType : Enumeration {     public static CardType Amex = new CardType(1, "Amex");     public static CardType Visa = new CardType(2, "Visa");     public static CardType MasterCard = new CardType(3, "MasterCard");      public CardType(int id, string name)     : base(id, name)     {     } } 

The problem: All of this is fine, and using these “enumeration classes” instead of enum has a lot of advantage. But now suppose the user comes and says that there is some specific rule for a specific item on the enumeration (for example, there is a very specific rule just for the Visa card).

With enums this is fairly easy. We would just perform a very natural check if (cardType == CardType.Visa). The point is: the values are all there on code to be checked.

But with enumerations the values are dynamical and added by the user. So the best I could consider would be to check for a string, querying the database’s table containing the enumeration values for the specific item.

But I think this is too error prone – say the user alters the string for that enumeration, the query wouldn’t work anymore. Further if the user made any typo when writing, it also wouldn’t work.

Is there some better method?

In summary: if we use enumration classes instead of enum and if there is a business rule for a specific item that the user is adding dynamically, how can we verify and address that in a more reliable way than “checking a string”?

How much business logic should the database implement?

I’ve worked in some projects where most of the business logic was implemented on the database (mostly through stored procedures). On the other side, I’ve heard from some fellow programmers that this is a bad practice (“Databases are there to store data. Applications are there to do the rest”).

Which of these approaches is the generally better?

The pros of implementing business logic in the DB I can think of are:

  • Centralization of business logic;
  • Independency of application type, programming language, OS, etc;
  • Databases are less prone to technology migration or big refactorings (AFAIK);
  • No rework on application technology migration (e.g.: .NET to Java, Perl to Python, etc).

The cons:

  • SQL is less productive and more complex for business logic programming, due to the lack of libraries and language constructs the most application-oriented languages offer;
  • More difficult (if possible at all) code reuse through libraries;
  • Less productive IDEs.

Note: The databases I’m talking about are relational, popular databases like SQL Server, Oracle, MySql etc.


How to apply OOP to real world examples without putting all logic in Manager classes?

I’m lately trying to implement a specific problem using an object-oriented approach. I get the main principles and its advantages, but I fail to apply it to a real world problem. Most examples one could find consist of Cats or Dogs being implementations of Animal. These however don’t give me enough understanding how to model below problem regarding another frequent example: a school administration system.

Imagine a school having Students, Courses, Professors, and Notes. My implementation would be something like this:

class Person {     string name;     int age;      Person(string name, int age) {         this.name = name;         this.age = age;     } }  class Student extends Person {     double gpa;      Student(string name, int age) {         super(name, age);     } }  class Professor extends Person {     string roomNumber;      Professor(string name, int age, string roomNumber) {         super(name, age);         this.roomNumber = roomNumber;     } }  class Course {     string name;     Professor professor;     Students[] student;      Course(string name, Professor professor) {         this.name = name;         this.professor = professor;         this.students = new Student[];     }      void enrolStudent(Student student) {         students.add(student);     } }  class Note {     Course course;     Student student;     double value;      Note(Course course, Student student, double value) {         this.course = course;         this.student = student;         this.value = value;     } } 

Now the Student has a bunch of Notes and we want to calculate its GPA. This could be either straightforward averaging its Notes‘ values or more complex logic using weights and/or ignoring optional courses.

Now my question is: where do we put this logic? Ideally I would have a function double calculateGpa() on Student so you could call student.calculateGpa(), but having this logic on Student would break the SRP in my view. It also does not belong to any other class listed here. A class called GpaCalculator or NotesManager would be another guess but that seems to me too much like moving all the logic away from the domain and into classes that do not represent a real object but just actions (see also this answer).

If that would be the way to go here, why wouldn’t I then just write a pure, static, stateless function in a class called NotesHelper? Creating a manager class to just have one function double calculate(), and using its instance instead of a static function feels to me like making it look like OOP while it isn’t really. I feel like there should be a better approach, probably one I didn’t think of, or maybe I am wrong here. Could you guys give me some pointers?


Comparison between central logic or distributed logic in microservices?

I’m designing an application in a microservices architecture. The app has its own logic but also needs to collaborate with other services.

For example, the app is A, and other apps are B and C. Whenever there’s an event “X” happens in A, The B and C also need to do something like sth-B(), sth-C().

There are two ways that I can think of:

  1. Keep the logic in A. So A will know all the things to do when event “X” happens. It will do sth-A(), then call restful sth-B() and sth-C(). B and C are simply passive.
  2. Distribute logic to B and C. So A will only do sth-A() and publish the event "X" into a message queue. B and C will subscribe the event and do sth-B() and do sth-C() respectively. In this way, A doesn’t need to care about what subscribers do, but it’s hard to know what in the whole system are really being done when the event happens.

My questions are 1) are the above two design ways common? Do they have formal names? 2) Which one should I choose in a microservices architecture?

How do I adjudicate the logic modality of certain death regarding the Fast Friends spell?

When I cast the Fast Friends spell could I assign the afflicted target to a task that leads to a die roll that has a chance of 98% from death by falling damage without the spell ending immediately?

If the activity would result in certain death for the creature, the spell ends.

Does the task have to necessarily lead to death for the spell ending condition to take place?

How do I adjudicate death probabilities for the purpose of the duration of the Fast Friends spell?

how to implement the following logic using java spark

consider a dataframe


============================================================  number|date|label|opendate|closedate|updateddate  1|1/1/2019|a|||  2|1/1/2019|a|||  3|1/1/2019|a|||  1|1/1/2019|b|||  ============================================================ 

i need to check the following condition and update the columns of the open close and updated date

condition1—————-> =>For all rows the open and updated date is default set as date

condition2—————-> =>when number is same we have to check the label if label differs then we have to set the close date and updated date of the first occuring number and set updated date alone for the second occuring number

expected output dataframe:

==============================================================  number|date|label|opendate|closedate|updateddate  1|1/1/2019|a|1/1/2019|2/1/2019|2/1/2019  ---->first occuring number  2|1/1/2019|a|1/1/2019||1/1/2019  3|1/1/2019|a|1/1/2019||1/1/2019  1|2/1/2019|b|2/1/2019||2/1/2019  ----> second occuring number   =============================================================== 

condition3——–> =>when number is same we have to check the label if the label is same then set the first occuring updated date as date of the second occuring and delete the second row. example dataframe that matched above condition:


========================================================  number|date|label|opendate|closedate|updateddate  1|1/1/2019|a ||| ---->first occuring number  2|1/1/2019|b|||  3|1/1/2019|c|||  1|2/1/2019|a ||| ----> second occuring number with same label   ========================================================== 

expected output dataframe:


number|date|label|opendate|closedate|updateddate  1|1/1/2019|a|1/1/2019||2/1/2019  ----> first occuring number updated date is setted as second occuring numbers date  2|1/1/2019|a|1/1/2019||1/1/2019  3|1/1/2019|a|1/1/2019||1/1/2019                                                                   ----> second occuring number row  deleted  ============================================================================================= 

i have done the same logic using plain java using arraylist and pojo classes and i need to know how to implement above logic using spark kindly help me with it

i tried updating it using spark udf functions but it works for only one column

an example code will be usefull

thanks in advance ………

How to test business logic in isolation when it depends on input validation logic

Hello I am building a demo project to teach myself about clean architecture and unit testing. My setup is very similar to this github repo https://github.com/mmacneil/CleanAspNetCoreWebApi

The important parts for this question are the following 2

  • The Core project which should contain the business logic
  • The Api project that handles receiving the requests and the presentation logic.

In my Api project (and also every other API that I have worked with) there is validation logic on the incoming request. For example: The request contains an Age field. I have a validation rule to check that Age > 0 as I don’t want to go any further if the Age is invalid.

In my Core project there is a Use Case that has the following logic. If Age < 18 then the user is a child. Now in this use case I have not made any checks to ensure that Age < 0 returns an error as I know this has already been checked and I don’t want to duplicate the logic.

I am writing unit tests for this Use Case and here is the thing. If I write a unit test with input Age = -1 and expect an error from the Use Case the test will naturally fail.

The reason for this problem is that the validation logic and the business logic are in two separate locations and this raises my question(s).

Is validation logic actually business logic and if so is it wrong to have validation logic anywhere outside the Core project? This does not feel correct because this means that all requests will pass to the Use Cases unfiltered (and I have seen in many web projects validations made as early as possible)

How do I test the above scenario if I don’t move the validation logic to the Use Case? It doesn’t feel right to just not write the Age = -1 test and trust that someone else before has made that check. Should I test this case with a different “system under test” at a higher level?

It is worth to use back end protection for API when client (web browser) access logic are all visible?

A web browser communicate to an open API endpoint: the communication require, let’s say a custom key and/or a custom header.

But, the key is visible from the client (inspecting the traffic) during the request so this doesn’t add any level of protection (or a very weak one).

However I use this approach when the client is not a web browser (e.g. two services in a microservices context), having a correct key means that one can ask informations to the other correctly The advantage is that the other can cut communications with others changing its own key.

Specifically, I’m following this quote from Building Microservices: Designing Fine-Grained Systems:

So consider each microservice having its own set of credentials. This makes revoking/changing access easier if the credentials become compromised, as you only need to revoke the set of credentials that have been affected.

Supposing that I have an API gateway that can be reached by a web browser, a key should be still required even if its visible to anyone?

Reset default track zoom in Logic Pro X

I somehow made a false move, and Logic Pro X changed the default track zoom for a project to “half screen height”. I can’t imagine any use for that feature, but there it is. It’s a long tedious manual process to fix all the tracks, and when I create a track stack in that project it’s created at half the height of the screen, and must be manually fixed.

Is there any way to make my project normal again?

I’ve read this page, and the part that claims to address my issue is simply wrong. The UI does not work that way.

The shift-drag suggestion in the documentation has no effect at all. The “Individual Track Zoom Reset for All Tracks” Key Command sounds promising, but it does the exact opposite: It sets all of my painstakingly corrected track heights back to half the height of the screen.

I found some practical-joke forum posts which suggested shift-double-click on the lower border of a track header. That of course sets all of my painstakingly corrected track heights back to half the height of the screen.

Even if there’s some remote corner case where somebody, somewhere, might want a track to be half the screen height once a year, why would they do this to the user? It seems insane. This is not freeware. They charge money for this product.