• (cs) in reply to RBoy
    RBoy:
    Quick! Let's dissemble his car!
    I will need to know more about this car before I can misrepresent it.
  • Inno (unregistered) in reply to pbean

    Comments anyone?

    if (number % 2 == 0){ // is number even?
      do something...
    }

    .... or just be wicked:

    if (number % 2 == 0){ // same as: if (number & 1 == 0)
      do something...
    }
  • (cs) in reply to TopCod3r
    TopCod3r:
    Using built-in functions are fine if you want to write code that is not portable and will probably be obsolete in 2 years. I on the other hand have taken measures to make sure my code lasts longer than other peoples. We disallow the use of the Imports statement (or "using" if you are a C# snob) because we DON'T OWN THE SOURCE CODE for those functions contained in those libraries. It is much more portable to write a StringConcat function that you have the ability to maintain than to use someone else's proprietary version of String Concatenation. Also, it lets you tailor the implementation to your specific system. I ngen all of my libraries so they are highly optimized versus the ones that come with .NET.

    So the moral is it is not a bad thing to write your own version of built in functions, because the built in ones are there more or less for demonstration purposes.

    Hehe, wonderful. I don’t look at the author’s name before reading a post. I managed to read two lines (up to “DON’T” :) before I realized that it had to be you. Welcome back. :)

  • Mike Rawsoft (unregistered)

    Why reinvent the whell? The static method Concat() of the class String does exactly what it sounds like :) string result = String.Conact( string1, string2, string3, ... ); http://msdn.microsoft.com/en-us/library/system.string.concat.aspx

  • Yanman (unregistered)

    Dude, where's my String?

  • addice (unregistered) in reply to Todd
    Sleepy:
    Hmmm... didn't really pay attention in programming class, because the teachers usually couldn't remember how to log in... but wasn't there one group that was always telling us to use functions wherever possible? Functional programming is better than procedural, or something like that?

    Functional programming has trade offs and benefits just like OOP. anyone who says it is 'better' without qualifying HOW and WHERE...yah there the same people who say you ALLWAYS need a finaly block even if it is empty.

    managers are not programmers; managers manage, programmers program. Managers who try to program, usually can't even manage.

  • MM (unregistered) in reply to Kermos
    Kermos:
    Game engine I'm working on has to be both DX9 and DX10 capable and which one to use is decided at runtime, not compile time. That way a Vista user can automatically get DX10 (if supported by hardware), XP user automatically gets DX9.

    Now DirectX does ship with the handy D3DX libraries with lots of functions needed for your basic 3D math (vectors, matrices, quaternions, etc.) However DX9 and DX10 both each have their own versions. DX10 libs may have functions DX9 libs don't. I can't also guarantee that DX9 libs may not have functions that aren't going to be replaced by something else in DX10 libs.

    So in my case, I actually do have my own Vector, Matrix, Quaternion, etc. classes because that way I'm API version independent. May also be of future benefit for cross-platform if OpenGL ever manages to fix their version 3.0 fuckup and becomes a viable alternate implementation.

    That's a valid reason for writing wrapper functions that check the current configuration and pass control to the appropriate library's corresponding function. It doesn't really justify re-writing what those library functions do.

  • (cs)

    I have to disagree with the first rule. It's perfectly OK to change something to a function call, even if the function call is longer, if doing so centralizes knowledge in the program and there is a chance that "fact" will change. In Ruby terms, "don't repeat yourself."

    It's also OK if it'll reduce coupling in a program (for example, accessors on classes)

  • ali (unregistered) in reply to jonny five
    jonny five:
    Sometimes it's better to extract a method when doing so provides better readability of code. The basic example they always use is to take this code:
    if (number % 2 == 0){
      do something...
    }
    
    and refactoring it to this:
    if (isEven(number)){
      do something...
    }
    ...
    boolean isEven(number){
      return number % 2 == 0
    }
    
    That's super nonsense IMO. Essentially you say to invent "isEven" to help those programmers who don't know what % does.

    In my opinion that's the same level as introducing "#define begin {" to help pascal programmers in programming C without learning the language.

    Moral: If you don't know what a symbol in your language does, look it up. Find one of those bulky things with thousands of non-empty sheets of paper glued together. One of those can tell you what the symbol means! Don't try to make the scary symbol go away by inventing new names.

  • (cs) in reply to MM
    MM:
    Kermos:
    Game engine I'm working on has to be both DX9 and DX10 capable and which one to use is decided at runtime, not compile time. That way a Vista user can automatically get DX10 (if supported by hardware), XP user automatically gets DX9.

    Now DirectX does ship with the handy D3DX libraries with lots of functions needed for your basic 3D math (vectors, matrices, quaternions, etc.) However DX9 and DX10 both each have their own versions. DX10 libs may have functions DX9 libs don't. I can't also guarantee that DX9 libs may not have functions that aren't going to be replaced by something else in DX10 libs.

    So in my case, I actually do have my own Vector, Matrix, Quaternion, etc. classes because that way I'm API version independent. May also be of future benefit for cross-platform if OpenGL ever manages to fix their version 3.0 fuckup and becomes a viable alternate implementation.

    That's a valid reason for writing wrapper functions that check the current configuration and pass control to the appropriate library's corresponding function. It doesn't really justify re-writing what those library functions do.

    While I completely agree with you, this is one exception. Can't do that with Vectors, Matrices or Quaternions. Those classes can't contain vtables because I need to be able to take an array of vectors and pass it to the video card. At that point in time, a 3 component vector class better only contain X,Y,Z and be 12 bytes. Also considering the amount of times these functions are liable to be called per second the overhead of a virtual call begins to matter.

    For higher level functionality such as D3DXFont or D3DXEffect I have exactly such an abstraction layer for. So when I need an Effect I just call Effect::create() with appropriate parameters and get either a D3DX9 or D3DX10 effect returned as necessary.

  • BJ Upton (unregistered) in reply to TopCod3r
    TopCod3r:
    Using built-in functions are fine if you want to write code that is not portable and will probably be obsolete in 2 years. I on the other hand have taken measures to make sure my code lasts longer than other peoples. We disallow the use of the Imports statement (or "using" if you are a C# snob) because we DON'T OWN THE SOURCE CODE for those functions contained in those libraries. It is much more portable to write a StringConcat function that you have the ability to maintain than to use someone else's proprietary version of String Concatenation. Also, it lets you tailor the implementation to your specific system. I ngen all of my libraries so they are highly optimized versus the ones that come with .NET.

    So the moral is it is not a bad thing to write your own version of built in functions, because the built in ones are there more or less for demonstration purposes.

    Let's be clear on one thing. TopCod3r isn't a troll. Trolls love to engage in personal attacks, and other disruptive argumentative tactics. Trolls also have no sense of humor and delight in outrage.

    This is satire.

    I, for one, would happily eat a baby for lunch with TopCod3r.

  • (cs) in reply to Yanman
    Yanman:
    Dude, where's my String?
    Plz send me teh concatz.
  • Shinobu (unregistered) in reply to Inno
    #define unless(x) if(!(x))
    
    unless(number & 1)
    {
        DoStuff();
    }
  • ali (unregistered) in reply to Shinobu
    Shinobu:
    #define unless(x) if(!(x))
    

    unless(number & 1) { DoStuff(); }

    Of course perl has the unless operator, and I actually totally love it. I do this all the time:

    die("Parameter x must be positive!") unless $x>0;
    

    It's really bad hacking, but I think I have a deep emotional relationship with unless by now...

  • Mogri (unregistered) in reply to BJ Upton
    BJ Upton:
    I, for one, would happily eat a baby for lunch with TopCod3r.

    This is ambiguous. Are you saying that you would:

    • at lunchtime, happily eat a baby with TopCod3r?
    • at lunchtime, happily share the baby you're eating with TopCod3r?
    • in exchange for lunch with TopCod3r, happily eat a baby?
    • in exchange for lunch with TopCod3r, eat a baby while happy?
  • Mizchief (unregistered) in reply to ali
    ali:
    Shinobu:
    #define unless(x) if(!(x))
    

    unless(number & 1) { DoStuff(); }

    Of course perl has the unless operator, and I actually totally love it. I do this all the time:

    die("Parameter x must be positive!") unless $x>0;
    

    It's really bad hacking, but I think I have a deep emotional relationship with unless by now...

    I have the same love affair with the ternary operator:

    string message = x>0 ? string.Empty : "Parameter x must be Positive1";

    Partly to run faster, mostly to confuse and infurate junior developers.

  • Frzr (unregistered) in reply to Inno
    Inno:
    if (number % 2 == 0){ // same as: if (number & 1 == 0) do something... }
    No wonder it didn't work for you. It should have been ((number & 1) == 0)...
  • (cs) in reply to BJ Upton
    BJ Upton:
    [ Let's be clear on one thing. TopCod3r isn't a troll. Trolls love to engage in personal attacks, and other disruptive argumentative tactics. Trolls also have no sense of humor and delight in outrage.

    This is satire.

    I, for one, would happily eat a baby for lunch with TopCod3r.

    Nope. While I'm as much a fan of TopCod3r as the next guy, he's still a troll. He's trolling for easy victims, which makes him a troll. Personal attacks may be part of the arsenal of the troll, but they are only used by juvenile and inexperienced trolls.

  • BJ Upton (unregistered) in reply to Bappi
    Bappi:
    BJ Upton:
    [ Let's be clear on one thing. TopCod3r isn't a troll. Trolls love to engage in personal attacks, and other disruptive argumentative tactics. Trolls also have no sense of humor and delight in outrage.

    This is satire.

    I, for one, would happily eat a baby for lunch with TopCod3r.

    Nope. While I'm as much a fan of TopCod3r as the next guy, he's still a troll. He's trolling for easy victims, which makes him a troll. Personal attacks may be part of the arsenal of the troll, but they are only used by juvenile and inexperienced trolls.

    You are redefining the meaning of the word "troll".

    You must think the existing definition is merely an example and suggestion

  • (cs) in reply to BJ Upton
    BJ Upton:
    Bappi:
    BJ Upton:
    [ Let's be clear on one thing. TopCod3r isn't a troll. Trolls love to engage in personal attacks, and other disruptive argumentative tactics. Trolls also have no sense of humor and delight in outrage.

    This is satire.

    I, for one, would happily eat a baby for lunch with TopCod3r.

    Nope. While I'm as much a fan of TopCod3r as the next guy, he's still a troll. He's trolling for easy victims, which makes him a troll. Personal attacks may be part of the arsenal of the troll, but they are only used by juvenile and inexperienced trolls.

    You are redefining the meaning of the word "troll".

    You must think the existing definition is merely an example and suggestion

    I think you're the one doing the redefining, bud:

    Remember, troll, with this meaning, significantly predates the web.

  • Bruno (unregistered) in reply to Bappi
    Bappi:
    BJ Upton:
    Bappi:
    BJ Upton:
    [ Let's be clear on one thing. TopCod3r isn't a troll. Trolls love to engage in personal attacks, and other disruptive argumentative tactics. Trolls also have no sense of humor and delight in outrage.

    This is satire.

    I, for one, would happily eat a baby for lunch with TopCod3r.

    Nope. While I'm as much a fan of TopCod3r as the next guy, he's still a troll. He's trolling for easy victims, which makes him a troll. Personal attacks may be part of the arsenal of the troll, but they are only used by juvenile and inexperienced trolls.

    You are redefining the meaning of the word "troll".

    You must think the existing definition is merely an example and suggestion

    I think you're the one doing the redefining, bud:

    Remember, troll, with this meaning, significantly predates the web.

    I am Impressed!!! A utilities company has a glossary including troll...I wonder if that somehow refers to other messages on their site

  • (cs) in reply to Bappi
    Bappi:
    BJ Upton:
    I think you're the one doing the redefining, bud: *snip*
    Remember, troll, with this meaning, significantly predates the web.
    Maybe you two should use "droll" instead of "troll".
  • (cs) in reply to ContraCorners
    ContraCorners:
    Larry:
    $ perl sub StringConcat { "@_" } print &StringConcat ("There's", "always", "another", "way"); There's always another way

    err... I'm not a perl programmer, but wouldn't this return "There'salwaysanotherway"?

    No, an array which is interpolated in a string has spaces placed between each element.

    A good example of why perl is awesome and frustrating at the same time.

  • Mr.'; Drop Database -- (unregistered) in reply to Kuba
    Kuba:
    > (concatenate 'string "foo" "bar" "baz") "foobarbaz"

    But also

    (+ 1 2 3 4 5) 15

    I always found that a bit ugly, personally.

    (shadow '+) (defgeneric + (a &rest b)) (defmethod + ((a number) &rest b) (apply #'cl:+ a b)) (defmethod + ((a string) &rest b) (apply #'concatenate 'string a b))

  • Mr.'; Drop Database -- (unregistered) in reply to savar
    savar:
    No, an array which is interpolated in a string has spaces placed between each element.
    They're separated by $", which defaults to a space.

    sub StringConcat { local $" = ";"; "@_"; } print &StringConcat ("There's", "always", "another", "way"); There's;always;another;way

  • (cs)

    FIRT!

    MM:
    Kermos:
    Game engine I'm working on has to be both DX9 and DX10 capable and which one to use is decided at runtime, not compile time. That way a Vista user can automatically get DX10 (if supported by hardware), XP user automatically gets DX9.

    Now DirectX does ship with the handy D3DX libraries with lots of functions needed for your basic 3D math (vectors, matrices, quaternions, etc.) However DX9 and DX10 both each have their own versions. DX10 libs may have functions DX9 libs don't. I can't also guarantee that DX9 libs may not have functions that aren't going to be replaced by something else in DX10 libs.

    So in my case, I actually do have my own Vector, Matrix, Quaternion, etc. classes because that way I'm API version independent. May also be of future benefit for cross-platform if OpenGL ever manages to fix their version 3.0 fuckup and becomes a viable alternate implementation.

    That's a valid reason for writing wrapper functions that check the current configuration and pass control to the appropriate library's corresponding function. It doesn't really justify re-writing what those library functions do.
    If you read the post, he's reimplementing functions that only exist in one version of the API, because it needs to work on both versions.

  • Jay (unregistered) in reply to Frzr
    Frzr:
    Inno:
    if (number % 2 == 0){ // same as: if (number & 1 == 0) do something... }
    No wonder it didn't work for you. It should have been ((number & 1) == 0)...

    Many years ago when I was young and stupid I wrote a BASIC program with the line

    if x=0 or 1 then ...
    

    I was thinking "(x=0) or (x=1)", but of course no matter what the value of x was, this test always evaluated to true.

  • Jay (unregistered)

    Clearly the right way to do this is with a proper object-oriented design:

    class StringCat
    {
      String string1;
      String string2;
      String string3;
      String string4;
      String string5;
    
      public void setString1(String string1)
      {
        this.string1=string1;
      }
      public void setString2(String string2)
      {
        this.string2=string2;
      }
      public void setString3(String string3)
      {
        this.string3=string3;
      }
      public void setString4(String string4)
      {
        this.string4=string4;
      }
      public void setString5(String string5)
      {
        this.string5=string5;
      }
      public String getConcat()
      {
        return string1+string2+string3+string4+string5;
      }
    }
    

    Then of course you would invoke it with:

    StringCat cat=new StringCat();
    cat.setString1("This ");
    cat.setString2("is ");
    cat.setString3("the ");
    cat.setString4("sentence");
    cat.setString5(".");
    String result=cat.getConcat();
    

    This then sets us up for the brilliant (or brillant) extension of allowing a variable number of parameters by having the setters keep track of which parameters were set and then only concatenating those actually given!

    The scary part is, there are probably plenty of programmers out there who would think this is a good idea.

  • Evan (unregistered) in reply to Jamie

    Haha, win!

  • delenit (unregistered) in reply to TopCod3r
    TopCod3r:
    So the moral is it is not a bad thing to write your own version of built in functions, because the built in ones are there more or less for demonstration purposes.
    Of course! And this applies to the language's syntax too. Much better to write your own.
  • speed3r (unregistered) in reply to ali

    I'd rather port jonny five's code to a new platform / language than yours. Imagine all the 100s / 1000s / millions of (x % 2)==0 you'd have to convert by hand, instead of reimplementing an isEven() function or macro.

    Ditto the StrConcat(). Your new language doesn't support the concatenation operator -- now whaddya do?

  • Aunt Linda (unregistered)

    I for one was getting tired of the signal outweighing the noise in the comments. I'm glad we can go back to having to put up with, define, explain, and reply to trolling. Hooray.

  • (cs) in reply to Jay
    Jay:
    Frzr:
    Inno:
    if (number % 2 == 0){ // same as: if (number & 1 == 0) do something... }
    No wonder it didn't work for you. It should have been ((number & 1) == 0)...

    Many years ago when I was young and stupid I wrote a BASIC program with the line

    if x=0 or 1 then ...
    

    I was thinking "(x=0) or (x=1)", but of course no matter what the value of x was, this test always evaluated to true.

    Surely that would evaluate to "if(x=(0 | 1))" -> "if(x=1)"? If it always evaluated to true, that would mean "if((x=0) || 1)", which is just retarded.
  • iso8859-1 (unregistered) in reply to ali

    there is a main difference between "#define begin" and "isEven(number)". #Define begin just replaces one symbol for another. It's pure renaming. Is even encapsulates the test wether a number is even or not. This can be done in multiple ways (as shown here in the forum) and can be messed up in ifs (as shown here as well). Once it is encapsulated, tested (or proven) and used everywhere, no one else can mess it up. And % is more complex than + - * or / and that means not everybody can grasp the concept and even transfer it to another concept like "even".

  • lomax (unregistered) in reply to Code Dependent
    Code Dependent:
    Bappi:
    BJ Upton:

    [...snip...]

    You are redefining the meaning of the word "troll".

    You must think the existing definition is merely an example and suggestion

    I think you're the one doing the redefining, bud: [..snip...] Remember, troll, with this meaning, significantly predates the web.
    Maybe you two should use "FLAME" instead of "troll". And leave the satire to the professionals.

    There, ftfy. ;) </flame>

  • wisi (unregistered) in reply to speed3r
    speed3r:
    I'd rather port jonny five's code to a new platform / language than yours. Imagine all the 100s / 1000s / millions of (x % 2)==0 you'd have to convert by hand, instead of reimplementing an isEven() function or macro.

    Ditto the StrConcat(). Your new language doesn't support the concatenation operator -- now whaddya do?

    Lyle told me he wrote a better grep to convert that exact code formation! And, er, then... a better sed!

  • Tei (unregistered) in reply to lomax
    lomax:
    Code Dependent:
    Bappi:
    BJ Upton:

    [...snip...]

    You are redefining the meaning of the word "troll".

    You must think the existing definition is merely an example and suggestion

    I think you're the one doing the redefining, bud: [..snip...] Remember, troll, with this meaning, significantly predates the web.
    Maybe you two should use "FLAME" instead of "troll". And leave the satire to the professionals.

    There, ftfy. ;) </flame>

    All of you got trolled. IMHO.

  • lomax (unregistered) in reply to Tei
    Tei:
    lomax:
    Code Dependent:
    Bappi:
    BJ Upton:

    [...snip...]

    You are redefining the meaning of the word "troll".

    You must think the existing definition is merely an example and suggestion

    I think you're the one doing the redefining, bud: [..snip...] Remember, troll, with this meaning, significantly predates the web.
    Maybe you two should use "FLAME" instead of "troll". And leave the satire to the professionals.

    There, ftfy. ;) </flame>

    All of you got trolled. IMHO.

    Not as much as... waitaminit!

    Nice try, [#REF TopCod3r]

  • Inno (unregistered) in reply to Frzr
    Frzr:
    Inno:
    if (number % 2 == 0){ // same as: if (number & 1 == 0) do something... }
    No wonder it didn't work for you. It should have been ((number & 1) == 0)...

    :-( oops I did it again!! (I fixed that bug dozens of times)

  • (cs) in reply to Jay
    Jay:
    The scary part is, there are probably plenty of programmers out there who would think this is a good idea.
    What is the license on your code? I've got this project where it would be a perfect fit...
  • asifyoucare (unregistered) in reply to speed3r
    speed3r:
    I'd rather port jonny five's code to a new platform / language than yours. Imagine all the 100s / 1000s / millions of (x % 2)==0 you'd have to convert by hand, instead of reimplementing an isEven() function or macro.

    Ditto the StrConcat(). Your new language doesn't support the concatenation operator -- now whaddya do?

    Hell yeah, and extend that further - what if the target platform doesn't support addition? Easy, just define

    int add(int n1, int n2) { return n1 + n2; }

    And so on .....

    You've got to be kidding, or trolling.

  • Level 2 (unregistered) in reply to Inno
    Inno:
    Frzr:
    Inno:
    if (number % 2 == 0){ // same as: if (number & 1 == 0) do something... }
    No wonder it didn't work for you. It should have been ((number & 1) == 0)...

    :-( oops I did it again!! (I fixed that bug dozens of times)

    Should have made a function. Then you'd only have to fix it once.

  • Zapakh (unregistered) in reply to jonny five
    jonny five:
    Sleepy:
    2. whatever the function does shouldn't be the same as a built-in operator
    I've been taught the idea that rule number 2 isn't always true. Sometimes it's better to extract a method when doing so provides better readability of code. The basic example they always use is to take this code:
    if (number % 2 == 0){
      do something...
    }
    
    and refactoring it to this:
    if (isEven(number)){
      do something...
    }
    ...
    boolean isEven(number){
      return number % 2 == 0
    }
    

    Unfortunately, readability is mostly in the eye of the beholder, and some of the people who are teaching are not good at reading. I think that if you find yourself eschewing most of the features of your chosen language in the name of readability, then the notion of readability that you're trying to support is doing you more harm than good.

    Back on topic: Not sure which language this is, but in some languages it's sometimes useful to define named functions to do dumb things, because the name of that function can be used as a callback.

    If I were bringing the benefit of the doubt, that's what I'd propose was the case :)

  • the amazing null (unregistered) in reply to A Nonny Mouse
    A Nonny Mouse:
    Sleepy:
    Hmmm... didn't really pay attention in programming class, because the teachers usually couldn't remember how to log in... but wasn't there one group that was always telling us to use functions wherever possible? Functional programming is better than procedural, or something like that?
    using functions in procedural programming != functional programming..

    thank you. i did not want to be the one to first bring this fact up. then again, this person admits to not paying attention in programming classes, i really doubt h or she was even awake in the theory classes.

    simplest explanation of functional vs procedural programming: functional programming allows a user to treat anything as a first class rope to hang themself with. procedural programming tends to give you enough atoms to make a good, strong gallows of your very own.

  • (cs)

    oh! i can see a few issues with that! the duplication! and it's error prone when you need to add more overloads for additional parameters!

    write yourself a code generator and you can easily extend this brillant function to support more parameters than you'll ever need! Without writing a single line of code!

            public static void ConcatGenerate(int maxParams) {
                const string param = "param"; // const to avoid duplication below!
                string nl = Environment.NewLine; // don't forget portability!
                string foo = "";
                System.IO.StreamWriter tehCodez = System.IO.File.CreateText("Optimizations.cs");
                tehCodez.WriteLine(
                    "namespace MyOptimizedStuff" + nl +
                    "{" + nl +
                        "\tpublic class NewStrCat" + nl +
                        "\t{");
                for (int i = 0; i < maxParams; i++) {
                    foo += "\t\tpublic string StringConcat(";
                    for (int j = 0; j <= i; j++) {
                        foo += "string " + param + j.ToString() + (j == i ? ") {" + nl : ", ");
                    }
                    foo += "\t\t\treturn ";
                    for (int j = 0; j <= i; j++) {
                        foo += param + j.ToString() + (j == i ? ";" + nl + "\t\t}" : " + ");
                    }
                    foo += nl;
                }
                tehCodez.WriteLine(foo);
                tehCodez.WriteLine(
                        "\t}" + nl +
                    "}");
                tehCodez.Close();
            }
    
  • ckelloug (unregistered) in reply to Inno

    Of course number&1 is likely 31 clock cycles faster.

  • (cs) in reply to wisi
    wisi:
    Lyle told me he wrote a better grep to convert that exact code formation! And, er, then... a better sed!
    And then, a better Spectate Swamp Desktop Search!

    (The quickest way to write a better SSDS is to wave a strong magnet over the (magnetic) disk it's stored on.)

  • mwo (unregistered)

    You know there is an advantage to this. Depending on how well the language handles strings, you may want to change the way all of your concats are done say for memory utilization and performance. For example, read the article on the .NET CLR handling of strings http://blog.briandicroce.com/2008/02/04/stringbuilder-vs-string-performance-in-net/ .

    Once you learn this vital lesson in .NET, you would have to find all string concats and replace them with the stringbuilder... with the function, you have only one place to make the change = NICE!!! :)

  • tbrown (unregistered) in reply to revenant
    revenant:
    It's such a pity C# doesn't support default parameters. Or else code author might have written
    public string StringConcat(string param0, string param1, string param2="")
    {
       return param0 + param1 + param2;
    }
    public string StringConcat(string param0, string param1,
       string param2, string param3, string param4, string param5="")
    {
       return param0 + param1 + param2 + param4 + param5;
    }
    thus supporting 2 and 4 parameters as well while reusing existing code. Best coding practices!

    But code reuse is so important it really should have been:

    public string StringConcat(string param0, string param1, string param2)
    {
       return param0 + param1 + param2;
    }
    
    public string StringConcat(string param0, string param1, string param2, string param3, string param4, string param5)
    {
       return StringConcat(StringConcat(param0, param1, param2), param4, param5);
    }

    (preserving the obviously intentional skipping of param3)!

  • Kuba (unregistered) in reply to mwo
    mwo:
    You know there is an advantage to this. Depending on how well the language handles strings, you may want to change the way all of your concats are done say for memory utilization and performance. For example, read the article on the .NET CLR handling of strings http://blog.briandicroce.com/2008/02/04/stringbuilder-vs-string-performance-in-net/ .

    Once you learn this vital lesson in .NET, you would have to find all string concats and replace them with the stringbuilder... with the function, you have only one place to make the change = NICE!!! :)

    Syntactic sugar and principle of least astonishment went to hell, obviously. A language platform requires you to read extra documentation because a commonly-expected-to-be-mutable type is in fact immutable -- that's no argument for inventing wrappers for everything...

    I personally think that immutable strings in .net are a mistake: I'd much rather have string generate an internal representation that's very compact and "hard" to modify upon initial construction, and then when it's mutated it would switch to a representation that's cheaper/more amenable to modification. If a string class is immutable, it should not have the += operator available, pure and simple - to do otherwise is misleading. A += on a .net string instance should cause a compile-time error. I have a constant string class that I use in C++, and it sure enough it does not try to construct new strings when abused via +=. operator += is declared private and left unimplemented, so that noone is ever under an impression that it ought to be available ;)

    Cheers, Kuba

Leave a comment on “Where'd param3 Go?”

Log In or post as a guest

Replying to comment #:

« Return to Article