• DQ (unregistered)

    frist = comment().count > 0 ? false : true;

  • Little Bobby Tables (unregistered)

    Should be this:

    if (viewport().width > 400)
        not_mobile = viewport().width > 400 ? true : false
    else if (viewport().width < 400)
        not_not_mobile = viewport().width > 400 ? false : true
    else if (viewport().width = 400)
        not_not_mobile = viewport().width > 400 ? false : true;
    

    I think that covers it.

  • (nodebb)

    You see, it's very important to distinguish between mobile, not_mobile and FILE_NOT_FOUND.

  • NotTheFrist (unregistered)

    The real WTF is calling this ternary abuse. The use of the ternary operator is totally fine here.

  • Ruts (unregistered) in reply to NotTheFrist

    Re NotTheFrist - it is ternary abuse when it is used to set 'true' or 'false' boolean result. No need for ternary then, just simple boolean logic

  • Hasseman (unregistered) in reply to NotTheFrist

    not_mobile = (viewport().width > 400) is just fine in many languages.

  • nnmn (unregistered) in reply to Little Bobby Tables

    else if viewport().width <= 0 throw ViewNotFound

  • ZeeD (unregistered) in reply to Little Bobby Tables

    But what if not_mobile should be FILE_NOT_FOUND ?

  • commentbot (unregistered)

    Gah! Another WTF is the name "not_mobile". It should be "desktop" or something that does not include the "not_" prefix. The "not_" prefix leads to bug-prone code like "If (not_mobile == FALSE) { /* mobile code / } else { / desktop code */ }". It's awful.

  • Anonymous') OR 1=1; DROP TABLE wtf; -- (unregistered) in reply to commentbot

    Yep. Use the positive sense, not the negative sense. See also:

    https://devblogs.microsoft.com/oldnewthing/20070314-00/?p=27623

    https://devblogs.microsoft.com/oldnewthing/20080624-00/?p=21863

  • Little Bobby Tables (unregistered) in reply to ZeeD

    Always happy to learn from a master.

    if (viewport().width > 400)
        not_mobile = viewport().width > 400 ? true : false
    else if (viewport().width < 400)
        not_not_mobile = viewport().width > 400 ? false : true
    else if (viewport().width = 400)
        not_not_mobile = FILE_NOT_FOUND;
    
  • Raj (unregistered)

    At least JavaScript supports ternary operators. For instance in Python the same code would be:

    not_mobile = True if viewport.width() > 400 else False

  • NotTheFrist (unregistered) in reply to Ruts

    Ah, if that is what the author meant with abuse, then he is of course correct. I would also have written it like Hasseman. I was under the assumption that the author meant that one should have written it with if and else instead of with the ternary operator. Seems like I was mistaken.

  • (nodebb)

    Of course, with missing parens to clarify matters, the author could have meant to compare the width with true or false, depending on the truth value of 400...

    not_mobile = viewport().width > (400 ? true : false);

  • Tom Passin (unregistered) in reply to Raj

    @Raj: "At least JavaScript supports ternary operators. For instance in Python the same code would be:

    not_mobile = True if viewport.width() > 400 else False"

    Come on, don't do the same thing in Python. This is enough:

    not_mobile = viewport.width() > 400 # right-hand side is already a boolean

    or avoiding the negative name (better):

    is_mobile = viewport.width() <= 400

  • Andrew (unregistered) in reply to commentbot

    Perhaps simply "mobile" i.e. mobile = viewport().width <= 400

  • King (unregistered) in reply to Little Bobby Tables

    viewport().width = 400 ? Hmmm

  • Coyote (unregistered)

    I, for one, would prefer sites go to a non-mobile design when my phone is in landscape mode.

  • chrtol (unregistered) in reply to Raj

    A reasonable Python version would be:

    not_mobile = viewport().width > 400

    Or, better...

    mobile = viewport().width <= 400

  • felpy (unregistered) in reply to DQ

    if (frist != false) not_frist = frist ? frist : false; else not_frist = false;

  • sizer99 (google) in reply to Raj

    If you wanted to write perl in python, yes.

  • löchlein deluxe (unregistered)

    Yeah, for future expandability, this should be a switch statement with approx. 1024 cases, generating an enum value, accompanied by a code generator to create it.

    (ETA: I have apparently become a robot. reCaptcha accepted my idea of palm trees on the first try. That's how these tests work, isn't it?)

  • Ryan of Tinellb (unregistered) in reply to Coyote

    I've done that for a bit of mine. Tables appear in full form in landscape, but as a list in portrait. My favourite part is a link that says "Period Table" in landscape, and "List of Elements" in portrait, so as long as the viewport width doesn't change while it's loading, it's consistent.

  • (nodebb)

    Well, on the other hand, there are still many web sites out there which assume that I use the total width of my 3 monitors for viewing them ...

  • Wizard (unregistered)

    Without seeing the rest of the code I can guess why it's called "not_mobile". Somewhere this would have been added to an existing system - perhaps a purely DeskTop system - and as the default value for "not_mobile" would be "false" its the route of least breaking changes with minimum effort. Certainly not an excuse but it is a reasonable reason.

  • David C. (unregistered)

    I despise code that makes assumptions about being mobile based on the window size. I generally open my web browser on a 1080p display in portrait orientation, and find way too many web sites assume I'm on a mobile device because the browser window, when maximized, is "only" 1080 pixels wide, and is even smaller when it's not maximized.

    Furthermore, all these sites assume you keep your browser window maximized. I almost never do, and tons of sites switch to their mobile view if I don't make the window more than half the width of the screen. Maybe I'm the only person left on the Internet who likes to see multiple partially-overlapping windows on a desktop, but this is really annoying.

    If you can't figure out a reliable way to determine if the device is mobile (can you just check to see if the browser's OS is iOS or Android?) then don't do it at all. Making assumptions based on the size of a browser window is just wrong.

  • (nodebb)

    This code looks like it was written in a rush for a system with, let's just say, a long history, IYKWIM...

  • Anon (unregistered) in reply to David C.

    Good luck in your campaign against Responsive Web Design (at least the way any CSS framework I've seen seems to implement it).

Leave a comment on “Tern Your View”

Log In or post as a guest

Replying to comment #504755:

« Return to Article