• ray10k (unregistered)

    Aww, it's baby's first function.

  • frist (unregistered)

    Aww, it's baby's frist comment.

  • Little Bobby Tables (unregistered)

    Standard brainfart that happens when you have a lot of repetitive getters and setters, and you don't have tools to generate them automatically.

    The other good joke is:

    public String getUserName() {
        return this.getUserName();
    }
    

    I've seen this stuff.

  • bvs23bkv33 (unregistered)

    real wtf is opening curly bracket on a new line

  • beef (unregistered) in reply to bvs23bkv33

    Curly on a newline for functions is so common in C that I wouldn't blame anyone for doing it...

  • isthisunique (unregistered)

    Oddly, it behaves unpredictably, as if you pass it a postal code, it returns the postal code, and doesn't complain that it isn't a username.

    Please not with the value objects. They rarely add value.

  • Mike Rosoft (unregistered)

    Of course, we need a version which verifies that the value passed to it is a username.

    public string GetUsername(string username, bool isUsername) { if (!isUsername) throw new ArgumentException("The string is not a username!") return username; }

  • (nodebb)

    I'm shit at C++ and I've never tried Java programming, which seems to be where this strange idiom is from. Why is calling a function on <thing> better than just using one of its properties? I don't get why I have to wait around for a function call to return, which just contains "return referencetothing.value" when I can just say "thing.value" and be done with it.

    Is it because MOAR LOC which make it more enterprisey, or something?

  • David Jackson (unregistered) in reply to beef

    Indeed. I'd say is was the correct way to do it and always hated code which did things the 'wrong' way. I always found it much easier to follow code that had the opening { on a new line, but it's a matter of taste I suppose and not worth making a fuss about.

  • Raj (unregistered) in reply to David Jackson

    Same here. It's easier to quickly scan code blocks if the opening curly brackets are in the same column as the closing.

    I've always seen same-line curly bracket people as artists more than engineers; the kind of people who use cursive fonts rather than monospace and who put little hearts rather than a dot above their i.

  • doubting_poster (unregistered) in reply to bvs23bkv33

    curly bracket on a new line is the only sane way to code.

  • Ruts (unregistered) in reply to doubting_poster

    Indeed it is. K&R C book has a lot to answer for

  • The candlestick (unregistered) in reply to Raj

    Meh. Maybe it's because I came up with both that it doesn't really bother me. Curly brace on the previous line works just as well, since the indentation level of that line and the end brace will still match (as long as the code is actually formatted). I think it's better to match what the rest of the codebase has, so that everything is consistent.

  • Mr. TA (unregistered) in reply to bvs23bkv33

    Nope, real WTF is not doing it. Particularly in Java and JavaScript people like keeping opening bracket on the same line which drives me crazy. More difficult to read, and what are you saving, 2 bytes of file size?

  • Ruthless_Proofreader (unregistered)

    internalsto? What's that?

  • Little Bobby Tables (unregistered) in reply to Raj

    Just as easy to follow if the closing bracket is on the same column as the "if" that opens it.

  • Dan Bugglin (google)

    What probably happened is the function was originally intended to take a unique identifier and return a username. Perhaps it was eventually decided that the unique identifier should be the username itself. Rather than completely remove this function and change everything calling it (maybe it was an API set in stone) the function was updated to do nothing but return the identifier passed in as the username.

  • Dan Bugglin (google)

    What probably happened is the function was originally intended to take a unique identifier and return a username. Perhaps it was eventually decided that the unique identifier should be the username itself. Rather than completely remove this function and change everything calling it (maybe it was an API set in stone) the function was updated to do nothing but return the identifier passed in as the username.

    Addendum 2018-08-22 08:58: Why does a comment system that doesn't check for accidental duplicate comments not allow you to delete comments?

  • Regret (unregistered)

    "Oddly, it behaves unpredictably, as if you pass it a postal code, it returns the postal code, and doesn't complain that it isn't a username." is not a proper sentence, it took me a while to figure out what it meant.

  • Mark H (unregistered) in reply to Little Bobby Tables

    If you have so many getters and setters that you need them automatically generated, that seems like a symptom of bad encapsulation. You might as well make every field public and save the effort.

  • (nodebb)

    You can't always get what you want, but if you try sometime, you just might find, you can get what you have.

  • ZB (unregistered)

    Opening braces on a line all by themselves will always look idiotic. Strangely, there are no common styles that consistently keep the opening brace on the same line and the closing brace on a separate line. Stroustrup is close, but inexplicably makes an exception for function definitions.

  • (nodebb) in reply to ZB

    Strangely, there are no common styles that consistently keep the opening brace on the same line and the closing brace on a separate line.

    It depends on what language you are talking about.

  • Supersonic Tumbleweed (unregistered) in reply to Ruthless_Proofreader

    it'sa operator akinto isinstance

  • Eric Gregory (github)

    They should replace this with a more generic "GetString" method that could be used for any type of string, not just usernames.

  • Richard (unregistered) in reply to bvs23bkv33

    If the only thing you can comment about you either are running a real good shop or have your priorities mixed.

  • Supersonic Tumbleweed (unregistered) in reply to ZB

    There is also a convention that puts both curly braces on the same line, like this:

    void getString(String s) { return s;

    } void getUsername(String username, boolean isUsername) { if (isUsername) { return username; } else { return "ERR1 : Not a username!"

    }} // barrage of closing braces

    but it's not in common use, due to neverending disputes amongst coders where to put the final semicolon. Should it be in the next file? In what order?

  • Supersonic Tumbleweed (unregistered) in reply to Supersonic Tumbleweed
    • semicolon -> curly brace
  • Perri Nelson (unregistered) in reply to doubting_poster

    Try that in JavaScript... thanks to implied semicolons it can mean something completely different from what you intend...

  • Perri Nelson (unregistered) in reply to Perri Nelson

    That is, in JavaScript the following:

    return { userName: "userName" };

    will return undefined, but

    return { userName: "userName" };

    will return an object.

  • Perri Nelson (unregistered)

    Man I HATE this comment system's automatic re-formatting.

    return

    {

    userName: "userName"

    };

    vs...

    return {

    userName: "userName

    };

  • Appalled (unregistered)

    How come none of you idiots can deduce that this was "stub" code to be filled in later but a need for it never arose. How many times was it called from the application, precisely 0. And anyone who doesn't but both beginning and ending braces on a new line (unless prohibited by the language which sucks) is an illegible dork.

  • Appalled (unregistered) in reply to Appalled

    doesn't put

  • (nodebb) in reply to Raj

    I very much agree--blocks should look like blocks. Thus braces go on their own line. Other styles might use fewer lines but they're not as clear to the eye.

  • I Saw a Robot (unregistered)

    I'm reminded of this "report" on how to not write CRAP code.

    http://www.cs.man.ac.uk/~pjj/cs1011/crap

  • Jackson (unregistered) in reply to Ruthless_Proofreader

    Internals to. Its a typo I believe

  • Tim! (unregistered) in reply to bvs23bkv33

    The most significant advantage of open-brace-on-new-line style IMO is that you can comment or #ifdef out the control statement and be left with a syntactically valid standalone code block.

  • Nic H (unregistered)

    We actually have a function that has almost that exact code, including dealing with usernames. The system originally had a separate unique ID for each user besides their username, so we had a function static string getUsername(string userId). Eventually we shifted to using the usernames as the unique key, so for the sake of not rewriting every usage it now just does return userId.

    The key difference there, of course, it was a static method. Sure, it was never GOOD code, but it was at least reasonable.

    We're currently refactoring to a much more standard DAL, so the entire class is going to be deleted pretty soon (hence not bothering to fix every usage, since we'll just have to do a much larger rewrite of the same area soon)

  • (nodebb)

    Braces? What language uses THAT. That is what whitespaces are used for.

    Oh, comments have a 'C' in the first column don't they?

    In other words, each to his own. Just be consistent!

  • Developer Dude (google) in reply to Appalled

    It may not be used now, but it very well could be.

    A dev should not rely on how code is used now, but rather on how it can be used in the future. I just had this debate with another dev about his code who assumed the code would only be used the way he used it and never called by any other code (yes, it is a public API).

  • greg (unregistered) in reply to Ruthless_Proofreader

    "internals to". He just missed a space.

  • Jeff Grigg (unregistered)

    It's the perfect most generic "getter" function! All "get" methods in the system should be replaced by this!!!

    (By doing copy-paste of the code, of course! "Inheritance is too complex for maintenance programmers to understand.")

    ;->

  • Gramir man (unregistered) in reply to Regret

    It is a proper sentence. The "as" serves to separate both independent clauses; it is not connected to the "if."

  • Crikey (unregistered) in reply to Mark H

    I could not agree more. The only times I ever create getters or setters for a member are when I want to make it read-only from outside the object, or when I want to do some extra stuff before getting/setting it. I have no need for my IDE to generate getters and setters because I don't use them often.

  • sad panda (unregistered) in reply to gordonjcp

    You are obviously a beginner. It has nothing to do with your programming language of choice. It's called encapsulation and it's ubiquitous. Also check out the Law of Demeter.

  • Neveralull (unregistered)

    Why would it behave unpredictably? It should always return the calling parameter username and never return this.username. If those happen to be the same value, it's a coincidence, not unpredictability.

  • (nodebb) in reply to gordonjcp

    gordonjcp wrote:

    ““Why is calling a function on <thing> better than just using one of its properties?””

    You are confusing object fields and properties. That's not your fault, but Java’s; the overwhelming popularity of that crap language has led to most people labouring under the misapprehension that “properties” means what Java uses them to mean.

    gordonjcp again: ““I don't get why I have to wait around for a function call to return, which just contains "return referencetothing.value" when I can just say "thing.value" and be done with it.””

    Because if you declare some field — variable — of your class publicly accessible, then calling code can_ both read and write_ it. But often you don't want that: you want a property readable but not writeable, read-only. Or writeable only in some circumstances, or with some checks or side effects.

    So you want to be able to write, say: private FSomesetting: boolean; procedure SetSomesetting (Value); public property Somesetting: boolean read FSomesetting write SetSomesetting ;

    Just the way Delphi 1 implemented it in 1995.

    Bravo, Sun Microsystems, what a lovely half-assed way to bungle up that concept!

Leave a comment on “You Can Only Get What You Have”

Log In or post as a guest

Replying to comment #498718:

« Return to Article