PC SOFT

FORUMS PROFESSIONNELS
WINDEVWEBDEV et WINDEV Mobile

Accueil → WINDEV 2024 → Elegance de code
Elegance de code
Débuté par Dergen, 11 déc. 2018 09:15 - 4 réponses
Membre enregistré
329 messages
Popularité : +28 (32 votes)
Posté le 11 décembre 2018 - 09:15
Voici un bout de code que je trouve très laid, quel amélioration, je pourrais y apporter (pour éviter le pavé en double) !

Je suis preneur de vos idées ?
SI HExécuteRequête(REQ_Stock_En_Cours, hRequêteDéfaut) ALORS

// Archivage du contenu du résultat de la requête dans une table
SI HLitPremier(REQ_Stock_En_Cours) <> Faux ALORS

// Boucle sur tout les enregistrement renvoyé.
TANTQUE PAS HEnDehors(REQ_Stock_En_Cours)

// Recherche a partir de la clef composé pour créer l'enregistrement ou le mettre a jour.
SI HLitRecherchePremier(StatistiquesWMS, Stats_Stockeur_Date, [REQ_Stock_En_Cours.STOCKEURS, gdLaDateEnCours]) ALORS
StatistiquesWMS.Stats_Date = gdLaDateEnCours
StatistiquesWMS.Stats_Annee = gnLAnneeEnCours
StatistiquesWMS.Stats_JourAnn = gnLeJourDeAnnee
StatistiquesWMS.Stats_Semaine = gnLaSemaineEnCours
StatistiquesWMS.Stats_Mois = gnLeMoisEnCours
StatistiquesWMS.Stats_Journee = gnLeJourEnCours
StatistiquesWMS.Stats_JourSem = gnLeHebdoEnCours
StatistiquesWMS.Stats_Stockeur = REQ_Stock_En_Cours.STOCKEURS
StatistiquesWMS.Stats_Palettes = REQ_Stock_En_Cours.PALETTES
StatistiquesWMS.Stats_Colis = REQ_Stock_En_Cours.COLIS
StatistiquesWMS.Stats_UVCs = REQ_Stock_En_Cours.UVCS
HModifie(StatistiquesWMS)
SINON
StatistiquesWMS.Stats_Date = gdLaDateEnCours
StatistiquesWMS.Stats_Annee = gnLAnneeEnCours
StatistiquesWMS.Stats_JourAnn = gnLeJourDeAnnee
StatistiquesWMS.Stats_Semaine = gnLaSemaineEnCours
StatistiquesWMS.Stats_Mois = gnLeMoisEnCours
StatistiquesWMS.Stats_Journee = gnLeJourEnCours
StatistiquesWMS.Stats_JourSem = gnLeHebdoEnCours
StatistiquesWMS.Stats_Stockeur = REQ_Stock_En_Cours.STOCKEURS
StatistiquesWMS.Stats_Palettes = REQ_Stock_En_Cours.PALETTES
StatistiquesWMS.Stats_Colis = REQ_Stock_En_Cours.COLIS
StatistiquesWMS.Stats_UVCs = REQ_Stock_En_Cours.UVCS
HAjoute(StatistiquesWMS)
FIN

HLitSuivant(REQ_Stock_En_Cours)
FIN
FIN
SINON
ConsoleErreur(ChaîneConstruit("Exécution de la requête STOCK impossible: %1.%2", HErreurInfo(), RC))
FIN


--
———————————————————————————————————
Ce qui se conçoit bien se code clairement et se débogue facilement...

- Pastiche d’une citation de Nicolas Boileau -
Membre enregistré
96 messages
Popularité : +18 (20 votes)
Posté le 11 décembre 2018 - 09:23
Bonjour, je vous propose deux solutions :

L'utilisation d'une procédure interne pour factoriser le code dupliqué

ou alors le code suivant :

EnregistrementExistant est un booléen = HLitRecherchePremier(StatistiquesWMS, Stats_Stockeur_Date, [REQ_Stock_En_Cours.STOCKEURS, gdLaDateEnCours])

StatistiquesWMS.Stats_Date = gdLaDateEnCours
StatistiquesWMS.Stats_Annee = gnLAnneeEnCours
StatistiquesWMS.Stats_JourAnn = gnLeJourDeAnnee
StatistiquesWMS.Stats_Semaine = gnLaSemaineEnCours
StatistiquesWMS.Stats_Mois = gnLeMoisEnCours
StatistiquesWMS.Stats_Journee = gnLeJourEnCours
StatistiquesWMS.Stats_JourSem = gnLeHebdoEnCours
StatistiquesWMS.Stats_Stockeur = REQ_Stock_En_Cours.STOCKEURS
StatistiquesWMS.Stats_Palettes = REQ_Stock_En_Cours.PALETTES
StatistiquesWMS.Stats_Colis = REQ_Stock_En_Cours.COLIS
StatistiquesWMS.Stats_UVCs = REQ_Stock_En_Cours.UVCS
SI EnregistrementExistant ALORS
HModifie(StatistiquesWMS)
SINON
HAjoute(StatistiquesWMS)
FIN


Et si vous êtes dans une version assez récente, vous pouvez même faire

HLitRecherchePremier(StatistiquesWMS, Stats_Stockeur_Date, [REQ_Stock_En_Cours.STOCKEURS, gdLaDateEnCours])

StatistiquesWMS.Stats_Date = gdLaDateEnCours
StatistiquesWMS.Stats_Annee = gnLAnneeEnCours
StatistiquesWMS.Stats_JourAnn = gnLeJourDeAnnee
StatistiquesWMS.Stats_Semaine = gnLaSemaineEnCours
StatistiquesWMS.Stats_Mois = gnLeMoisEnCours
StatistiquesWMS.Stats_Journee = gnLeJourEnCours
StatistiquesWMS.Stats_JourSem = gnLeHebdoEnCours
StatistiquesWMS.Stats_Stockeur = REQ_Stock_En_Cours.STOCKEURS
StatistiquesWMS.Stats_Palettes = REQ_Stock_En_Cours.PALETTES
StatistiquesWMS.Stats_Colis = REQ_Stock_En_Cours.COLIS
StatistiquesWMS.Stats_UVCs = REQ_Stock_En_Cours.UVCS
HEnregistre(StatistiquesWMS)


Mais je viens de voir la fonction ConsoleErreur ce qui signifie que vous êtes en 24, la dernière syntaxe fonctionnera.

Bonne journée à vous.

--
Johjo aka Jonathan Laurent

Codez mieux ! Codez plus vite !

Mon blog sur WinDev : http://www.ytreza.org
Me contacter sur slack (wx-community) : https://frama.link/BoBD0SY0
Faîtes moi un ping : http://www.ytreza.org/fr/services/ping-sur-forum
Membre enregistré
329 messages
Popularité : +28 (32 votes)
Posté le 11 décembre 2018 - 09:51
Merci pour le "HEnregistre", que je ne connaissait pas du tout :merci:

--
———————————————————————————————————
Ce qui se conçoit bien se code clairement et se débogue facilement...

- Pastiche d’une citation de Nicolas Boileau -
Membre enregistré
34 messages
Popularité : +3 (3 votes)
Posté le 11 décembre 2018 - 13:18
// Eviter les "SI... SINON... FIN imbriqué
SI PAS HExécuteRequête(REQ_Stock_En_Cours, hRequêteDéfaut) ALORS
ConsoleErreur(ChaîneConstruit("Exécution de la requête STOCK impossible: %1.%2", HErreurInfo(), RC))
RETOUR
FIN

POUR TOUT REQ_Stock_En_Cours
SI PAS HLitRecherchePremier(StatistiquesWMS, Stats_Stockeur_Date, [REQ_Stock_En_Cours.STOCKEURS, gdLaDateEnCours]) ALORS
HRAZ(StatistiquesWMS)
FIN
StatistiquesWMS.Stats_Date = gdLaDateEnCours
StatistiquesWMS.Stats_Annee = gnLAnneeEnCours
StatistiquesWMS.Stats_JourAnn = gnLeJourDeAnnee
StatistiquesWMS.Stats_Semaine = gnLaSemaineEnCours
StatistiquesWMS.Stats_Mois = gnLeMoisEnCours
StatistiquesWMS.Stats_Journee = gnLeJourEnCours
StatistiquesWMS.Stats_JourSem = gnLeHebdoEnCours
StatistiquesWMS.Stats_Stockeur = REQ_Stock_En_Cours.STOCKEURS
StatistiquesWMS.Stats_Palettes = REQ_Stock_En_Cours.PALETTES
StatistiquesWMS.Stats_Colis = REQ_Stock_En_Cours.COLIS
StatistiquesWMS.Stats_UVCs = REQ_Stock_En_Cours.UVCS
HEnregistre(StatistiquesWMS)
FIN
Membre enregistré
329 messages
Popularité : +28 (32 votes)
Posté le 11 décembre 2018 - 21:32
Je suis arrivé au même résultat que toi Tim aprés m’être heurté à l’erreur me disant que l’enregistrement était introuvable

--
———————————————————————————————————
Ce qui se conçoit bien se code clairement et se débogue facilement...

- Pastiche d’une citation de Nicolas Boileau -