1Ci Support Help Center home page
Submit a request
Sign in
  1. 1Ci Support
  2. 1C:Enterprise Development Standards
  3. Code conventions
  4. Using 1C:Enterprise language structures

Using duplicate code

  • Using 1C:Enterprise language structures
    • General requirements to creating 1C:Enterprise language structures
    • Moving expressions
    • Using duplicate code
    • Using compilation directives and preprocessor commands
    • Defining variable value type
    • Getting objects metadata
    • Form module event handlers attached in code
    • Using global variables in modules
    • Local variables pre-initializing
    • Using event log
See more

Scope: managed applications, mobile applications, and ordinary applications.

1. Code duplication is a development approach when, to develop new features, the developer copys existing code fragments or whole procedures and functions.

Duplicate code might bring about some issues regarding application maintenance:

  • Duplicated code inherits the mistakes from the original. 
  • When you fix coding mistakes, you might overlook the same mistakes in duplicates. 
  • Debugging takes more time. 
  • The application architecture gets more complicated.

Usually, you have to duplicate code when you cannot address it from other parts of the applications. For example, the code is embedded in a form module, and you want another form to call it.

It is recommended that you avoid code duplications whenever possible. The best way to avoid code duplication is to refactor code. For example, to make form's procedures and functions accessible, move them form the form module to a common module.

2. It is recommended that you duplicate code only if later you plan to upgrade the duplicate to serve different feature.

How to avoid duplication for server/client functions

For example, in the CommonClientServer module, you want the MessageToUser function to process input parameters differently when it is called from server-side.

To do so, do not use preprocessor commands, like #If NOT ThinClient AND NOT WebClient. Incorrect:

Procedure MessageToUser(Val MessageToUserText, Val DataKey = Undefined, Val Field = "", Cancel = False) Export
 
 Message = New MessageToUser;
 Message.Text = MessageToUserText;
 Message.Field = Field;
 
 IsObject = False;
 
#If NOT ThinClient AND NOT WebClient Then
 If DataKey <> Undefined
   AND XMLTypeOf(DataKey) <> Undefined Then
  ValueTypeAsString = XMLTypeOf(DataKey).TypeName;
  IsObject = StrFind(ValueTypeAsString, "Object.") > 0;
 EndIf;
#EndIf
 
 If IsObject Then
  Message.SetData (DataKey);
 Else
  Message.DataKey = DataKey;
 EndIf;
 
 Message.Inform();
 Cancel = True;
 
EndProcedure

The proper solution would creating two procedures—one for client-side, one for server-side—with the same name, but different behavior. The common code, which is called by both procedures, place in a client/server common module:

i) Server-side procedure:

Procedure MessageToUser(Val MessageToUserText, Val DataKey = Undefined, Val Field = "", Cancel = False) Export
 
 IsObject = False;
 If DataKey <> Undefined
  And XMLTypeOf(DataKey) <> Undefined Then
  
  ValueTypeAsString = XMLTypeOf(DataKey).TypeName;
  IsObject = StrFind(ValueTypeAsString, "Object.") > 0;
 EndIf;
 
 CommonInternalClientServer.MessageToUser(MessageToUserText, DataKey, Field, Cancel, IsObject);
 
EndProcedure

ii) Client-side procedure:

Procedure MessageToUser(Val MessageToUserText, Val DataKey = Undefined, Val Field = "", Cancel = False) Export
 
 CommonInternalClientServer.MessageToUser(MessageToUserText, DataKey, Field, Cancel, IsObject);
 
EndProcedure

iii) Common client/server procedure in the CommonInternalClientServer module:

Procedure MessageToUser(Val MessageToUserText, Val DataKey, Val Field , Cancel = False, IsObject = False) Export
 
 Message = New MessageToUser;
 Message.Text = MessageToUserText;
 Message.Field = Field;
 
 If IsObject Then
  Message.SetData(DataKey);
 Else
  Message.DataKey = DataKey;
 EndIf;
 
 Message.Inform();
 Cancel = True;
 
EndProcedure

See also:

  • Developing configurations with commonly used code and metadata objects
© 2020-2022 1C INTERNATIONAL LLC www.1Ci.com Support policy