Click here to Skip to main content
15,891,633 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
AnswerRe: Duplication vs. Complexity Pin
Sander Rossel12-Oct-21 7:09
professionalSander Rossel12-Oct-21 7:09 
AnswerRe: Duplication vs. Complexity Pin
Gerry Schmitz12-Oct-21 8:22
mveGerry Schmitz12-Oct-21 8:22 
AnswerRe: Duplication vs. Complexity Pin
BillWoodruff12-Oct-21 18:14
professionalBillWoodruff12-Oct-21 18:14 
AnswerRe: Duplication vs. Complexity Pin
Mike Winiberg12-Oct-21 21:14
professionalMike Winiberg12-Oct-21 21:14 
GeneralRe: Duplication vs. Complexity Pin
Jacquers12-Oct-21 22:12
Jacquers12-Oct-21 22:12 
AnswerRe: Duplication vs. Complexity Pin
Niels Holst12-Oct-21 22:11
professionalNiels Holst12-Oct-21 22:11 
AnswerRe: Duplication vs. Complexity Pin
Kirk 1038982113-Oct-21 4:13
Kirk 1038982113-Oct-21 4:13 
AnswerRe: Duplication vs. Complexity Pin
Dave B 6813-Oct-21 6:17
Dave B 6813-Oct-21 6:17 
I think you already have some good answers here. The following are my general rules for this sort of situation.

No code, string, or logic that ever needs to be kept in sync should ever be duplicated via copy and paste. It will get out of sync if you do. And trying to keep it in sync will drive up the cost of the development/debug cycles despite the "increased complexity" of someone updating a single pasted copy.

To facilitate this, apply the following in order:

When 'magic strings' are required, they are put into constants and referenced so one can't be changed without breaking compilation.

If you KNOW that you are only going to have 2 or 3 versions of whatever you are maintaining, simple if then else logic is perfectly fine as you haven't violated the primary rule of not duplicating any code/logic/markup that needs to be kept in sync and more importantly in all likelihood, you want a future developer to consider the impact on the other uses of the logic rather than being able to ignorantly get things out of sync by updating one form and not the other.

When code can be placed in a function and effectively called from each required location, do this.

When composition/aggregation can be used to capture the logic into a reusable component, do this.

When the above are not possible, consider inheritance and/or a metadata driven approach.

NOTE that all of these rules only apply assuming you are coding in a language that can be compiled or linted (well), such as a traditional language (C,c++,java,c#,Typescript,...), annotated python, ... These would not ALL apply to vanilla JS, non-annotated python, lua, or other similar languages that only break at runtime and require developers to create tests to replace what a compiler and linter mostly does for a good code base.

Further, they assume that performance is not a concern.
Dave B


modified 13-Oct-21 12:40pm.

AnswerRe: Duplication vs. Complexity Pin
englebart13-Oct-21 9:40
professionalenglebart13-Oct-21 9:40 
AnswerRe: Duplication vs. Complexity Pin
Member 1409260513-Oct-21 23:22
Member 1409260513-Oct-21 23:22 
AnswerRe: Duplication vs. Complexity Pin
Martin ISDN14-Oct-21 7:14
Martin ISDN14-Oct-21 7:14 
AnswerRe: Duplication vs. Complexity Pin
Felix Collins14-Oct-21 10:04
Felix Collins14-Oct-21 10:04 
GeneralCCC 2021-10-12 Pin
Peter_in_278011-Oct-21 22:00
professionalPeter_in_278011-Oct-21 22:00 
GeneralRe: CCC 2021-10-12 Pin
NeverJustHere11-Oct-21 22:30
NeverJustHere11-Oct-21 22:30 
GeneralRe: CCC 2021-10-12 - WINNER! Pin
Peter_in_278011-Oct-21 22:39
professionalPeter_in_278011-Oct-21 22:39 
GeneralRe: CCC 2021-10-12 Pin
pkfox11-Oct-21 23:38
professionalpkfox11-Oct-21 23:38 
GeneralRe: CCC 2021-10-12 Pin
Peter_in_278011-Oct-21 23:45
professionalPeter_in_278011-Oct-21 23:45 
GeneralRe: CCC 2021-10-12 Pin
pkfox12-Oct-21 0:22
professionalpkfox12-Oct-21 0:22 
GeneralIf you are toward the poles... Pin
David O'Neil11-Oct-21 13:11
professionalDavid O'Neil11-Oct-21 13:11 
GeneralLightning points up Pin
David O'Neil11-Oct-21 13:09
professionalDavid O'Neil11-Oct-21 13:09 
GeneralRe: Lightning points up Pin
Nelek11-Oct-21 20:44
protectorNelek11-Oct-21 20:44 
GeneralRe: Lightning points up Pin
Chris C-B11-Oct-21 21:19
Chris C-B11-Oct-21 21:19 
GeneralRe: Lightning points up Pin
RickZeeland11-Oct-21 22:16
mveRickZeeland11-Oct-21 22:16 
GeneralIt's a great day ... until it might not have been. Pin
OriginalGriff11-Oct-21 9:31
mveOriginalGriff11-Oct-21 9:31 
GeneralRe: It's a great day ... until it might not have been. Pin
Member 1532961311-Oct-21 9:53
Member 1532961311-Oct-21 9:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.