PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : If-Abfrage übersichtlicher gestalten


Kabelsalat
2006-04-18, 18:16:08
Hallo, wie würdet ihr folgende If-Abfrage übersichtlicher gestalten?


if (Link.Port != window.location.Port && !(
Link.Port == 80 && !window.location.Port && window.location.protocol == "http:" ||
!Link.Port && Link.protocol == "http:" && window.location.port == 80 ||
Link.Port == 443 && !window.location.Port && window.location.protocol == "https:" ||
!Link.Port && Link.protocol == "https:" && window.location.port == 443)
) epiFrame.LinkValidator.AdjustLink(Link);


Danke für eure Hilfe

Kabelsalat

GloomY
2006-04-18, 18:55:53
Hallo, wie würdet ihr folgende If-Abfrage übersichtlicher gestalten?Wie wär's damit?
if (Link.Port != window.location.Port && !(
(
Link.Port == 80
&& !window.location.Port
&& window.location.protocol == "http:"
) || (
!Link.Port
&& Link.protocol == "http:"
&& window.location.port == 80
) || (
Link.Port == 443
&& !window.location.Port
&& window.location.protocol == "https:"
) || (
!Link.Port
&& Link.protocol == "https:"
&& window.location.port == 443)
)
)
epiFrame.LinkValidator.AdjustLink(Link);
Ich hab' noch ein paar Klammern dazugesetzt. Imho ist beia && b || c nicht klar, ob das(a && b) || codera && (b || c)heißen soll. :)

GloomY
2006-04-18, 19:03:13
Ah, wenn man noch ein paar Mal de-Morgan anwendet, kommt man auf folgendes:if ( Link.Port != window.location.Port
&& ( Link.Port != 80
|| window.location.Port
|| window.location.protocol != "http:"
)
&& ( Link.Port != 443
|| window.location.Port
|| window.location.protocol != "https:"
)
&& ( Link.Port
|| Link.protocol != "http:"
|| window.location.port != 80
)
&& ( Link.Port
|| Link.protocol != "https:"
|| window.location.port != 443)
)
) epiFrame.LinkValidator.AdjustLink(Link);Das dürfte der Übersichtlichkeit dienen, weil jetzt die innere Klammer weg ist. :)

edit: Noch mal ein bisschen umsortiert...

Kabelsalat
2006-04-18, 19:10:15
Mmh, immerhin ein Ansatz ;) So ganz gefällt mir das ganze aber immernoch nicht. Vielleicht kann ich diese riesen If-Abfrage auch irgendwie rausschmeißen...

Kabelsalat
2006-04-18, 19:15:26
Danke für den zweiten Post. Sieht gut aus ;)

GloomY
2006-04-18, 19:15:55
Mmh, immerhin ein Ansatz ;) So ganz gefällt mir das ganze aber immernoch nicht. Vielleicht kann ich diese riesen If-Abfrage auch irgendwie rausschmeißen...

PS: Die Klammern sind überflüssig, da der Term ohne sie der Reihe nach abgearbeitet wird => a && b || c ergibt (a && b) || c.Stimmt dann meine obige Klammersetzung? Du hast oben etwas in der Form

a && b && c || d && e && f || ...

Soll das als

(((((a && b) && c) || d) && e) && f) || ...

ausgewertet werden oder wie von mir oben gemacht als

(a && b && c) || (d && e && f) || ... ?

Die beiden Ausdrücke sind nämlich nicht logisch äquivalent.

Coda
2006-04-18, 19:16:00
Äh wie wärs damit die einzelnen Teile einfach vorher in bools zu stecken? :|

Kabelsalat
2006-04-18, 19:19:53
@GloomY: Ja, deine Klammern stimmen. War ein Denkfehler von mir.

@Coda: Würde die Lesbarkeit auf alle Fälle steigern. Eigentlich bin ich auch kein Fan solch riesiger Terme, aber das ganze ist clientseitiges Javascript und ich mache mir durchaus ein paar sorgen um dessen Größe: Momentan bin ich bereits bei über 30KB angekommen und viel mehr als 50% Kompression erreiche ich nicht :(

Neomi
2006-04-18, 19:21:00
Statt "Port" oder "!Port" würde ich "Port != 0" bzw. "Port == 0" bevorzugen. Paßt viel besser zu Abfragen wie "Port == 80", sorgt also für mehr Konsistenz.

Kabelsalat
2006-04-18, 19:22:42
Port ist allerdings null bzw. undefiniert und nicht 0.

/edit: Port ist "", aber auch nicht 0.

Kabelsalat
2006-04-18, 20:09:24
Danke für eure Tipps, aber die Frage hat sich erledigt:

if (GetPortFromLink(Link) != GetPortFromLink(window.location)) AdjustLink(Link);


GetPortFromLink: function(Link)
{
var strProto = GetProtocolFromLink(Link);
var intPort = Link.port;

if (!intPort)
{
if (strProto == "http:") intPort = 80;
if (strProto == "https:") intPort = 443;
}

return intPort;
}