Duplicate code headache

My company enforced the code duplication under 3% and sonar will give me error over a code which may have a level of duplication but in human sense it is not duplicate at all, and merging this code just make it a whole of spaghetti code:

purchaseProformaHeaders: [
        { text: locale.translate('purchase.proforma.columnHeaders.serial'), value: 'invoiceNoDisplay', sortable: false },
        { text: locale.translate('purchase.proforma.columnHeaders.dealSide'), value: 'personDisplay', sortable: false },
        { text: locale.translate('purchase.proforma.columnHeaders.date'), value: 'invoiceDateTime', sortable: false },
        { text: locale.translate('purchase.proforma.columnHeaders.currency'), value: 'currencyCode', sortable: false },
        { text: locale.translate('purchase.proforma.columnHeaders.changeRate'), value: 'exchangeRate', sortable: false },
        { text: locale.translate('purchase.proforma.columnHeaders.total'), value: 'totalAmount', sortable: false },
        { text: locale.translate('purchase.proforma.columnHeaders.createdDate'), value: 'createdAt', sortable: false },
        { text: locale.translate('purchase.proforma.columnHeaders.createdBy'), value: 'createdBy', sortable: false },
        { text: '', align: 'end', value: 'id', sortable: false }
      ]
headers: [
        { text: locale.translate('purchase.invoice.columnHeaders.serial'), value: 'invoiceNoDisplay', sortable: false },
        { text: locale.translate('purchase.invoice.columnHeaders.dealSide'), value: 'personDisplay', sortable: false },
        { text: locale.translate('purchase.invoice.columnHeaders.date'), value: 'createdAt', sortable: false },
        { text: locale.translate('purchase.invoice.columnHeaders.currency'), value: 'currencyCode', sortable: false },
        { text: locale.translate('purchase.invoice.columnHeaders.changeRate'), value: 'exchangeRate', sortable: false },
        { text: locale.translate('purchase.invoice.columnHeaders.total'), value: 'totalAmount', sortable: false },
        { text: locale.translate('purchase.invoice.columnHeaders.createdBy'), value: 'createdBy', sortable: false },
        { text: '', align: 'end', value: 'id', sortable: false }
      ]

or:
Here I have a JsDoc which gives this binding a meaning I want to be accessible in the code…

computed: {
    appId () {
      return '$APPId$'
    },
    newRoute () {
      return {
        name: '$APPId$-purchase-proforma-new'
      }
    },
    isRTL () {
      const { locale } = this.$props.context
      return isRTL(locale)
    },
    ...mapState('proformas', {
      totalCount: state => state.totalCount,
      /**
       * @param state
       * @returns {Array<module:invoice.PurchaseProformaView>}
       */
      items: state => state.items,
      loading: state => state.loading,
      message: state => state.message,
      error: state => state.error
    })
  },
  computed: {
    appId () {
      return '$APPId$'
    },
    newRoute () {
      return {
        name: '$APPId$-purchase-order-new'
      }
    },
    isRTL () {
      const { locale } = this.$props.context
      return isRTL(locale)
    },
    ...mapState('orders', {
      totalCount: state => state.totalCount,
      /**
       *
       * @param state
       * @returns {Array<module:invoice.PurchaseOrderView>}
       */
      items: state => state.items,
      loading: state => state.loading,
      message: state => state.message,
      error: state => state.error
    })
  },

as you can see there are few things that are differ other than the JsDoc and it mark the whole block and a few more as duplicated.
I may be able to extract the code, but It’s just make the thing worse… it decrease code maintainability and Sonar proudly says my code is A grade in maintainability, cause only god knows what we are doing to solve some of these sonar issues. and unfortunately the one who configure this, believe in sonar more than his life. that even though we are using PSQL and sonar uses PL/SQL, and we get PLSQL warning, he still believes in it…
Either don’t create a tools that some day people think they need it, or do it right, cause some people put all their believe in a code or a talk, which is not 100% percent right, or has drawbacks in some cases.


Update

Also note that interestingly it mark things that meant to be redundant as duplicate, but it doesn’t detect duplicate code if the user misplace variable, I noticed my co-worker do that trick to bypass code duplication wherever they can
{a:x, b:y}
duplicate with
{a:z, b:y}
but not with
{b:y, a:x}


Second Update

my block is way bigger than this, but we expect sonar to find duplication like this:

      saleOrdersHeaders: [
        gp.createColumnHeader('sale.order.columnHeaders.orderNo', 'invoiceNoDisplay'),
        gp.createColumnHeader('sale.order.columnHeaders.dealSide', 'personDisplay'),
        gp.createColumnHeader('sale.order.columnHeaders.date', 'invoiceDateTime'),
        gp.createColumnHeader('sale.order.columnHeaders.total', 'totalAmount'),
        gp.createColumnHeader('sale.order.columnHeaders.createdDate', 'createdAt'),
        gp.createColumnHeader('sale.order.columnHeaders.createdBy', 'createdBy'),
        gp.createActionColumnHeader('id')
      ],

but it actually do it like:

        gp.createColumnHeader('sale.order.columnHeaders.orderNo', 'invoiceNoDisplay'),
        gp.createColumnHeader('sale.order.columnHeaders.dealSide', 'personDisplay'),
        gp.createColumnHeader('sale.order.columnHeaders.date', 'invoiceDateTime'),
        gp.createColumnHeader('sale.order.columnHeaders.total', 'totalAmount'),
        gp.createColumnHeader('sale.order.columnHeaders.createdDate', 'createdAt'),
        gp.createColumnHeader('sale.order.columnHeaders.createdBy', 'createdBy'),
        gp.createActionColumnHeader('id')
      ],
      dataTableFooterProps: {

Hi,

What language is this?

The docs aren’t clear on this, but string literals are ignored during duplication detection. that’s why you’re experiencing this.

Would specifying an exclusion work for you? Or is this too broadly spread in your project?

 
Ann

JavaScript - With Vue framework

1 Like