Questions tagged [single-responsibility-principle]

For questions about the Single Responsibility Principle in object-oriented programming, one of the SOLID principles coined by Robert C. Martin. It states that a module should have only one reason to change.

Robert Martin was inspired by David Parnas, Edsger Dijkstra (who coined the term Separation of Concerns) and Larry Constantine (who coined the terms Coupling and Cohesion). During the late 1990s, Martin consolidated their ideas into the Single Responsibility Principle.

Martin's definition of the SRP evolved to become,

Gather together the things that change for the same reasons. Separate those things that change for different reasons.

In keeping with the previous authors who inspired him, Martin notes that,

...this is just another way to define cohesion and coupling.

In contrast with the principle, the SRP is focused on people rather than functionality.

As you think about this principle, remember that the reasons for change are people. It is people who request changes. And you don’t want to confuse those people, or yourself, by mixing together the code that many different people care about for different reasons.

The SRP later became the first of Martin's .

445 questions
0
votes
1 answer

Where to run parameter validation

Lets say we have a function, and a caller to that function function baz(){ myVar = null; foo(myVar); } function foo(bar){ //do stuff } where should validation on the parameters happen? It could be function baz(){ myVar = null; …
0
votes
1 answer

Is there a way to adhere to the Single Responsibility Principle without invoking multiple database calls in this code?

I have a portion of code that basically retrieving a Bean object from the database through Hibernate, making some changes to the values of two or three properties before storing it back into the database. Let's look at a real example. My…
Ayrx
  • 2,092
  • 5
  • 26
  • 34
0
votes
2 answers

How to avoid convoluted logic for custom log messages in code?

I know the title is a little too broad, but I'd like to know how to avoid (if possible) this piece of code I've just coded on a solution of ours. The problem started when this code resulted in not enough log information: ... var users =…
0
votes
1 answer

Single Responsibility Principle and classes

I hope I can make clear what I'm struggling with :-) Here goes. I'm wondering how to implement the SRP on the following case: There's a project. When finished, a contact has to be mailed with a survey in which he gives feedback on how the project…
user2660051
0
votes
0 answers

Filter an event and notify designated subscribers

I'm new to TDD and I'm trying to apply Single Responsibility Principle to an API. I'm using an API that returns multiple feed subscriptions in a single event. Each subscription can be differentiated from another because the Event Handler contains an…
stromms
  • 161
  • 5
0
votes
2 answers

is it bad for a mapper to fetch data from db?

I'm wondering: how complex a mapper abstraction can be? Let's say I have a controller's action ActionResult Find(QueryInputModel query) with the model looks like this class QueryInputModel { public string Text {get;set;} public…
vorou
  • 1,869
  • 3
  • 18
  • 35
0
votes
2 answers

Single Responsibility Principle Violated or Not

I have a class which adds some functionality to a windows form textbox. For example, it handles textbox key down event and based on some sort of logic, if 'Enter' was pressed then an special grid is shown to let the user select one entity from a…
Alireza
  • 10,237
  • 6
  • 43
  • 59
0
votes
1 answer

Single responsibility principle in ViewModel

I'm coding an editor for graphs (Graph Theory).Let's imagine vertex needs these properites : class Vertex{ int ID {get;} Color color {get; set;} Point point{get; set;} } But it's violating of SRP(single responsibility principle). So I created…
bidacek
  • 101
  • 1
  • 6
0
votes
3 answers

Is this a violation of the single responsiblity principle?

I have the following method and interface: public object ProcessRules(List rules) { foreach(IRule rule in rules) { if(EvaluateExpression(rule.Exp) == true) return rule.Result; } //Some error handling here for not…
LJM
  • 6,284
  • 7
  • 28
  • 30
0
votes
1 answer

On "Single Responsibility" and coding practice for object validation

Possible Duplicate: What does the single responsibility principle mean for validation Case A: Validate object -> Send to Method -> Method assumes valid object -> Method executes Case B: Send to Method -> Method checks object validity -> Method…
blitzcrag
  • 21
  • 2
0
votes
1 answer

Does the class CommaDelimLog in the following code violates single responsibility principle?

The program parses log files - each log file may have different kind of field format (fixed width, comma delimited, etc). Also each log file are mixed of several different kind of logs - each kind have different field definition). For example, the…
0
votes
1 answer

is this a good approach to create a logging system

I want to create a log system not to log exceptions , but user activities and what they do upon data . for example when a user deletes a record , I want to log username , records id , date and ... . first I decided to create a log class and use its…
0
votes
1 answer

I know of three ways in which SRP helps reduce coupling. Are there even more?

I'd like to figure all the possible ways SRP helps us reduce coupling. Thus far I can think of three: 1) If class A has more than one responsibility, these responsibilities become coupled and as such changes to one of these responsibilities may…
user1483278
  • 929
  • 1
  • 9
  • 17
0
votes
3 answers

Why is a table with too many columns a smell?

Recently I had this discussion with some other developers about how too many columns in a table , or too many attributes on a model is a code smell . Some argue that a Model with too many Attribute is doing too many things , and should be split .…
Emil
  • 1,240
  • 1
  • 15
  • 27
0
votes
1 answer

code adherence to single responsibility principle & unit testing

I have lately been reading on the Single Responsibility Principle concept, and in theory I agree a lot with it. I am having difficulty coming to terms on which code can be exactly classified as violating the principle. I would like to implement…
Karl Cassar
  • 6,043
  • 10
  • 47
  • 84
1 2 3
29
30