Guter Code (oder besser: kein zu schlechter Code)

Django, Flask, Bottle, WSGI, CGI…
Antworten
mgraf
User
Beiträge: 34
Registriert: Donnerstag 14. Juni 2007, 11:46

Hallo,
ich wollte nur fragen, ob folgender Code akzeptabel ist, oder ob und was man noch verbessern kann:

Code: Alles auswählen

n = context.REQUEST.get('promocode')
c = context.REQUEST.get('conference_fee')

b = int(c)*0.9 #Die conference_fee abzüglich Gutschein 10%
a = int(c)*0.1 #Der Gutschein 10% Preis

#entscheidet welcher der Radio-Buttons ausgewählt wurde und übergibt die Bezeichnung, anders möglich??
if c == '350':
  o = 'Conference registration for NOC'
elif c == '135':
  o = 'Conference registration for NOC (Student rate)'
elif c == '190':
  o = 'Conference registration for OC&I'
elif c == '390':
  o = 'Conference registration for both NOC and OC&I'
else:
  None

#Entscheidet ob ein Gutscheincode eingegeben wurde und gibt den Status zurück und die Input Werte
if n !='':
  if n == 'noc08krems':
    state.setError('promocode', None, new_status='success')
    state.setKwargs( {'portal_status_message':'Promotion Code is ok: ' + n + ' || ' + c + ' -10% (Promotion Code) = ' + str(b) + '€ Your Fees'} )
    context.REQUEST.set('option', o)
    context.REQUEST.set('optionprice', c)
    context.REQUEST.set('conference_fee', b)
    context.REQUEST.set('promocodefee', a)
  else:
    state.setError('promocode', 'Invalid Entry', new_status='failure')
    state.setKwargs( {'portal_status_message':'Promotion Code is invalid'} )
    context.REQUEST.set('option', o)
    context.REQUEST.set('optionprice', c)
else:
    context.REQUEST.set('option', o)
    context.REQUEST.set('optionprice', c)

# No errors. Always make sure to return the ControllerState object
return state
Und bitte nicht hauen, auch wenn der Code sehr schlecht ist. Ich lerne ja noch ;-)

lg
miichi
Leonidas
Python-Forum Veteran
Beiträge: 16025
Registriert: Freitag 20. Juni 2003, 16:30
Kontaktdaten:

Du kannst etwa soas machen:

Code: Alles auswählen

selections = {
  '350' : 'Conference registration for NOC'
  '135' : 'Conference registration for NOC (Student rate)',
  '190' : 'Conference registration for OC&I',
  '390' : 'Conference registration for both NOC and OC&I'
}
o = selections.get(c, None)
Außerdem den Variablen brauchbare Namen geben, das wäre schon mal ein guter Anfang. Von Inline-Kommentaren würde ich auch abraten, wenn ``context.REQUEST`` ein dict ist, würde ich da via ``[]`` drauf zugreifen.

``if n != '':`` kannst du durch ``if not n:`` ersetzen. In Zeile 24 würde ich eher auf String-Formatting zurückgreifen.
My god, it's full of CARs! | Leonidasvoice vs (former) Modvoice
Benutzeravatar
gerold
Python-Forum Veteran
Beiträge: 5555
Registriert: Samstag 28. Februar 2004, 22:04
Wohnort: Oberhofen im Inntal (Tirol)
Kontaktdaten:

Leonidas hat geschrieben:wenn ``context.REQUEST`` ein dict ist, würde ich da via ``[]`` drauf zugreifen.
Hallo!

Was aber den Nachteil hat, dass es einen Fehler wirft, wenn es den Eintrag nicht gibt.

mfg
Gerold
:-)
http://halvar.at | Kleiner Bascom AVR Kurs
Wissen hat eine wunderbare Eigenschaft: Es verdoppelt sich, wenn man es teilt.
mgraf
User
Beiträge: 34
Registriert: Donnerstag 14. Juni 2007, 11:46

Danke, für Eure Hilfe.
Das mit den selections = ... hab ich eingefügt, macht ja den Code schlanker und übersichtlicher.

Das mit den Dictornary hatte ich zuvor eh, aber wenn es leer ist....

naja, danke auf jedn Fall, ich geh jetzt Heim :wink:

lg
michi
Antworten