Thursday, October 1, 2015

Bad coding only?

Long time not seen, been around doing things i may publish at a later stage.
Lately i am maintaining an existing Delphi software and strangling with some piece of code.
To who ever writes such code: Please do not do that, my eyes are in pain and my brain f@...d.

  ChargeIdx := TSupportedContract(TSupportedCarrier(TariffList.Objects[Carrieridx]).
                 FContracts.Objects[ContractIdx]).FChargeList.IndexOf(ChargeCode);
  if (ChargeIdx = -1) then
  begin
    TSupportedContract(TSupportedCarrier(TariffList.Objects[Carrieridx]).
      FContracts.Objects[ContractIdx]).FChargeList.AddObject(ChargeCode, TSupportedCharge.Create);

    ChargeIdx := TSupportedContract(TSupportedCarrier(TariffList.Objects[Carrieridx]).FContracts.Objects[ContractIdx]).FChargeList.IndexOf(ChargeCode);
  end;

  ServiceIdx := TSupportedCharge(TSupportedContract(TSupportedCarrier(TariffList.Objects[Carrieridx]).
                  FContracts.Objects[ContractIdx]).FChargeList.Objects[ChargeIdx]).FServices.IndexOf(ServiceCode);
  if (ServiceIdx = -1) then
  begin
    TSupportedCharge(TSupportedContract(TSupportedCarrier(TariffList.Objects[CarrierIdx]).
      FContracts.Objects[ContractIdx]).FChargeList.Objects[ChargeIdx]).FServices.AddObject(ServiceCode, TSupportedService.Create);
    ServiceIdx := TSupportedCharge(TSupportedContract(TSupportedCarrier(TariffList.Objects[Carrieridx]).
                    FContracts.Objects[ContractIdx]).FChargeList.Objects[ChargeIdx]).FServices.IndexOf(ServiceCode);
  end;

  PackageIdx := TSupportedService(TSupportedCharge(TSupportedContract(TSupportedCarrier(TariffList.Objects[Carrieridx]).
                  FContracts.Objects[ContractIdx]).FChargeList.Objects[ChargeIdx]).FServices.Objects[ServiceIdx]).FPackages.IndexOf(PackageCode);
  if (PackageIdx = -1) then
  begin
    TSupportedService(TSupportedCharge(TSupportedContract(TSupportedCarrier(TariffList.Objects[Carrieridx]).
      FContracts.Objects[ContractIdx]).FChargeList.Objects[ChargeIdx]).FServices.Objects[ServiceIdx]).FPackages.AddObject(PackageCode, TSupportedPackage.Create);
    PackageIdx := TSupportedService(TSupportedCharge(TSupportedContract(TSupportedCarrier(TariffList.Objects[Carrieridx]).
                  FContracts.Objects[ContractIdx]).FChargeList.Objects[ChargeIdx]).FServices.Objects[ServiceIdx]).FPackages.IndexOf(PackageCode);
  end;

  ZoneIdx := TSupportedPackage(TSupportedService(TSupportedCharge(TSupportedContract(TSupportedCarrier(TariffList.Objects[Carrieridx]).
              FContracts.Objects[ContractIdx]).FChargeList.Objects[ChargeIdx]).FServices.Objects[ServiceIdx]).FPackages.Objects[PackageIdx]).FServiceZones.
                IndexOf(IntToStr(qry.FieldByName('CHARGE_LINK_ID').AsInteger));
  if ZoneIdx = -1 then
  begin
    ServiceZone := TSupportedServiceZone.Create;
    ServiceZone.ServiceZone := ReadServiceZone(qry);

    TSupportedPackage(TSupportedService(TSupportedCharge(TSupportedContract(TSupportedCarrier(TariffList.Objects[Carrieridx]).
      FContracts.Objects[ContractIdx]).FChargeList.Objects[ChargeIdx]).FServices.Objects[ServiceIdx]).FPackages.Objects[PackageIdx]).FServiceZones.
        AddObject(IntToStr(ServiceZone.ServiceZone.ChargeLinkID), TObject(ServiceZone));

    ZoneIdx := TSupportedPackage(TSupportedService(TSupportedCharge(TSupportedContract(TSupportedCarrier(TariffList.Objects[Carrieridx]).
                FContracts.Objects[ContractIdx]).FChargeList.Objects[ChargeIdx]).FServices.Objects[ServiceIdx]).FPackages.Objects[PackageIdx]).FServiceZones.
                  IndexOf(IntToStr(qry.FieldByName('CHARGE_LINK_ID').AsInteger));
  end;

  Charge := TCharge.Create(FSessionID);
  Charge.FDiscount := qry.FieldByName('DISCOUNT').AsFloat;
  Charge.Charge := ReadCharge(qry);
  Charge.LoadChargeBreaks(qry.Database);

  TSupportedServiceZone(TSupportedPackage(TSupportedService(TSupportedCharge(TSupportedContract(TSupportedCarrier(TariffList.Objects[Carrieridx]).
    FContracts.Objects[ContractIdx]).FChargeList.Objects[ChargeIdx]).FServices.Objects[ServiceIdx]).FPackages.Objects[PackageIdx]).FServiceZones.
      Objects[ZoneIdx]).FCharges.AddObject(IntToStr(Charge.Charge.ID), TObject(Charge));

No comments:

Post a Comment