Function Names



  • Is there any certern way to name functions? i tend to  to call them what they do

    Removeinvalid()

    Comeupwitharandomnumber() 



  • First a general comment:  a question like this is almost certain to ignite a holy war; take every answer you get with a grain of salt.

    Second comment:  your instructor (if you're a student) or co-workers/institution (if you're working) may have coding rules that answer these questions for you.  (Fact)

    Third:  function names should be descriptive (which you've already figured out) and easy to read, and it's generally considered good form to use the same style across the project. (Opinions, but generally agreed irrespective of religion).

    To your specific names, I find them a bit difficult to read, I would consider going to the underscores_as_spaces model or the CamelCasing model:  remove_invalid() or ComeUpWithARandomNumber().  (Opinion, but part of point three above). 

    I'm not going to tell you [b]which[/b] to use ... 



  • More like I'm teaching myself good coding rules (Learning Python)

    so making EasyToRead() Functions is a good thing,

    Next Step, Should i use indentations or keep is somewhat flat



  • Also, is Python a good "starter" language, what would you recommend



  • Keeping everything flat is a nearly-guaranteed way to convince anyone else that has to read your code to want to cause you bodily harm, so, Yes, you should indent. 

    Indentation Style, however, is another religious subject, but it may be settled for you; I'll admit I don't know that much about Python, but isn't it "bracket-less", where indentation level is important for defining statement blocks?

     



  • I've fiddled with PHP with some success, if only there was a programming language like that (that isn't meant for websites)



  • Most modern languages have "conventions" about names.  Those aren't rules enforced by the language but they are "how we do things".  (For instance in the USA we avoid naming a boy "Sue.")  Python shows the influence of C and Java with class names starting with capital letters and functions starting with small letters, like GraphicsFile and saveToFile().

    Python uses indentations instead of braces or endIf so you have to use them:

      if x == 1:

        y = 2

        if v > 0:

          z = 2

        v = 0

      x = 2

    (in C that's:  if (x == 1) { y = 2; if (v > 0) { z = 2; } v = 0; } x = 2;  )

    It's kind of an annoyance but that's the rules.

     



  • ARG!, well python is out, I like using braces May php with the CLI interface will do?



  • I'd look at the style of whatever language you're using, as they all tend to be different.

    Java: toString()

    C#: ToString()

    Ruby: to_s



  • Here is the python style guide - that might help you a bit.

     I also prefer the reverse Hungarian notation scheme myself...  😛

    See also this article on naming conventions.
     



  • Each language has its own standard.

     Name your functions and your variables such that you can understand it if you come back after a week's vacation.

     For instance, I might have a function called BuildResponse() that's descriptive enough in the context of the class. Or, I might need something like SaveAgentDetails if I'm saving sections of a doc independantly and want to distinguish what's going on.

     

    Don't overdo it just to have long names, though... that can be just as annoying.



  • @jrwr00 said:

    I've fiddled with PHP with some success, if only there was a programming language like that (that isn't meant for websites)

    If you mean "similar syntax" - PHP is a cheap knockoff of Perl with less features and more stupid, so it looks pretty similar to Perl code.

    If, on the other hand, you mean "similar idiotic weirdness", then no, nobody else has duplicated the things that make PHP "special".

    http://search.cpan.org/~kudarasp/PHP-Strings-0.28/Strings.pm is a fairly effective way to understand the differences between PHP and Perl. This is just the string functions, but it's pretty much like that all the way.

    (If  you attempt to write non-trivial programs in Perl, you will most likely cut your own legs off. You have to be pretty good to use this language well)



  • i was always taught to capitalize function names (the first letter and the first letter of every word DoSomethingCool(), for example)

    And then variables are lowercase/uppercase (i think they call those camelcase...  dim someInteger as integer = 0 for example)

     

    Also i really like visual basic console apps as a starting point into the foray of programming, since it does teach OO and structured programming practices. Once you have the "programming paradigm" or whatever they want to call it now down pat, then you may want to try to tackle C++... with it's pointers, templates, and other such things.



  • @jrwr00 said:

    Also, is Python a good "starter" language, what would you recommend

    This is yet another question to which you will undoubtedly get a lot of passionate answers.  But there is a simple answer to it: the language you are comfortable using which is most appropriate for the job.

    For example, you wouldn't use PHP to write a GUI application like Notepad.  On the other hand, you would probably never use C to write a web application, any more than you would use Java to write a low-level math routine.

    Every language is different and is made for different purposes.  Some are high-level and object oriented (Java, C#, Python), while others are low-level, exposing intricate details of the process (C, assembly language).  Yet others are designed for very specific purposes (PHP, Scheme, Lisp).  Some are also more likely to be found with different operating systems (Perl and Bash under UNIX, VB/VBScript under Windows).

    Likewise, you will have languages that you are more comfortable with.  The differences between C# and Java are moot; either is a general-purpose, platform-independent, high-level, object-oriented language.  But you will undoubtedly be more comfortable working with one than the other, as you likely won't learn both at the same time.

    I would suggest picking a common language (like C or Java) and learning it well to start.  Then, once you have experience in how to program, you can begin learning other languages.  Sample several languages of different types--C, Java, Bash, Perl, and Python would be one suggestion that would give you a wide range of abilities.

    As an anecdote of why you might want to use different languages, consider what you might be working on.  I program web pages in PHP part-time while I study engineering.  My scripts for the web site were previously done in Perl (since it's a language commonly available on servers), but I have since switched to Python.  In my actual engineering projects almost everything is C or some specific assembly language, though I use math programs like Matlab and Maple which have their own languages.  When I want to interface a GUI so a user can interact with a device I build, I use C#.  There are lots of different languages and lots of reasons to use them.

    So I would say there is no one language that is best to learn.  Your best bet is to start with something popular (C, C++, Java) so that you can find lots of books and examples.  Beyond that, once you know a language (and how to write programs), those same skills work for any language.


     



  • @RevEng said:

    @jrwr00 said:

    Also, is Python a good "starter" language, what would you recommend

    So I would say there is no one language that is best to learn.  Your best bet is to start with something popular (C, C++, Java) so that you can find lots of books and examples.  Beyond that, once you know a language (and how to write programs), those same skills work for any language.

    Good point, rev. as my CS graduate friend says, "once you know how to program, the languages used are merely syntactical differences"



  • @RevEng said:

    On the other hand, you would probably never use C to write a web application

    Why not? If you need maximum performance, C is the right choice. In fact, web applications can be written in nearly every language.



  • @ammoQ said:

    @RevEng said:

    On the other hand, you would probably never use C to write a web application

    Why not? If you need maximum performance, C is the right choice. In fact, web applications can be written in nearly every language.

    I challenge you to write a simple guestbook page in INTERCAL.
     



  • @asuffield said:

    @jrwr00 said:

    I've fiddled with PHP with some success, if only there was a programming language like that (that isn't meant for websites)

    If you mean "similar syntax" - PHP is a cheap knockoff of Perl with less features and more stupid, so it looks pretty similar to Perl code.

    If, on the other hand, you mean "similar idiotic weirdness", then no, nobody else has duplicated the things that make PHP "special".

    http://search.cpan.org/~kudarasp/PHP-Strings-0.28/Strings.pm is a fairly effective way to understand the differences between PHP and Perl. This is just the string functions, but it's pretty much like that all the way.

    (If you attempt to write non-trivial programs in Perl, you will most likely cut your own legs off. You have to be pretty good to use this language well)

    <holy war alert>

    The hatred contained in this comparison is rather stupid. Most of these functions are no longer needed by anybody doing PHP, they're just left in for backward compatibility. The entries about how something can be implemented using regular expressions are not an evidence of Perl's superiority, because anyone can use precisely that regular expression in PHP via the preg_match function and others. Most entries complain aboout lack of Unicode support in PHP5, but there is an extension named mbstring that solves this problem, and PHP6 will be Unicode aware. I don't understand this guy in general. Why does he make a problem out of features that he is not forced to use (ie. magic quotes and some functions)?

    PS His counter-examples are wonderful. If you had no or very little programming experience, which one do you find more readable? s,$,<br />,mg; or $foo = nl2br($foo); ?

    PS 2 Why most major CMSes are written in PHP then? Why is Perl sometimes considered an esoteric language?
     



  • @Angstrom said:

    @ammoQ said:
    @RevEng said:

    On the other hand, you would probably never use C to write a web application

    Why not? If you need maximum performance, C is the right choice. In fact, web applications can be written in nearly every language.

    I challenge you to write a simple guestbook page in INTERCAL.
     

    g That's why I wrote "nearly". Anyway, some years ago, I've written several web applications in C, it's perfectly possible.



  • @Tweenk said:

    Most of these functions are no longer needed by anybody doing PHP, they're just left in for backward compatibility.

    They were never needed in the first place, and many of them are incredibly bad ideas (addslashes is one of the largest sources of security holes on the web, ever).

     

    The entries about how something can be implemented using regular expressions are not an evidence of Perl's superiority, because anyone can use precisely that regular expression in PHP via the preg_match function and others.

    Very few perl developers feel a need to provide evidence of perl's superiority. This guy is no different. This is about PHP's stupidity compared to the rest of the world - and the fact that you can implement them in PHP via preg_match is precisely why many of them are stupid. Imagine if C had a set of functions add_one_to_int(x), add_two_to_int(x), add_three_to_int(x), as well as having a perfectly functional + operator.

     

    Why does he make a problem out of features that he is not forced to use (ie. magic quotes and some functions)?

    This is documentation. It is for people whose brains have been eaten by PHP. It says quite clearly that its purpose is to answer questions of the form "What's the equivalent to PHP's X?", and the answer to that is very frequently "You don't want to use X, for these reasons, and here's what you want to use instead". You obviously did not bother to read it.

    If you had no or very little programming experience

    Then you should not be using perl and you absolutely should not, under ANY circumstances, be writing web-facing applications - so you shouldn't be using PHP either.

    Why most major CMSes are written in PHP then?

    A combination of good marketing from Zend corp (remember, PHP is commercial software), and the fact that cheap web hosting companies tend to charge less for PHP-only accounts than they do for full unix hosting. Applications which are primarily of interest to people who would want to use such hosting companies (web forums, CMSes, etc) tend to be written in PHP because that's what's on the hosting market.

     

    Why is Perl sometimes considered an esoteric language?

    Stupidity? Belief in the myth that unix is esoteric, because it only accounts for 40% of the market? If you're a unix admin, and you aren't at least moderately familiar with shell and perl, you're practically illiterate. They're the glue that hold the world together.



  • @Tweenk said:

    PS 2 Why most major CMSes are written in PHP then? Why is Perl sometimes considered an esoteric language?

    Two reasons. First. Critical mass... it's easy, it's approachable, you can get a hosting account quickly, it was marketed well. So, if you have more people writing in a given space with a given language, you'll have more people attempting to solve the same problem.

    Second, I'm not all that conviced that most of the PHP developers I've seen actually care about reuse. I realize this is an extremely broad brush, but I've seen more often than not where someone will go off and start writing simple one-off logging implementations that turns into a convoluted mess when they could have just as easily used log4php. So, you end up with CMS solutions that just start as a fork of an existing open source solution, which causes a massive proliferation of something like CMS.

    Also, the most major CMS I know of, Interwoven CMS, was written in Perl. Having worked with their platform, I wouldn't exactly hold them up as an example of how to build a system, but they are in the magic quadrant with Gartner and definately have market share.

    Any question about why a given language is better than another is sort of stupid. As is questioning why any language has more market share or is considered esoteric. Popularity can be a function of clever marketing, barrier to entry, feature set or any combination... most popular can, at best, mean best for a specific problem domain.

    If you follow Paul Graham at all, you'd know that his idea of a perfect language is one they wrote for ViaWeb that was a specialized DSL on top of a specialized version of Scheme. Surely, you'd be of the opinion that Scheme is esoteric, but, for at least one other person, it's the epitome of a great language.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.