-  [WT]  [PS]  [Home] [Manage]

[Return]
Posting mode: Reply
Name
Email
Subject   (reply to 5638)
Message
File
Password  (for post and file deletion)
  • Supported file types are: GIF, JPG, PNG, TXT
  • Maximum file size allowed is 5120 KB.
  • Images greater than 200x200 pixels will be thumbnailed.
  • Currently 422 unique user posts. View catalog

  • Blotter updated: 2009-07-22 Show/Hide Show All

File 125707662910.png - (297.77KB , 448x751 , exploitable.png )
5638 No. 5638
WHY? Why in the world MUST I write this, in every fucking C-like language?

string hello() {
return "Hello, World!";
}

Why can't I write this?

string hello() return "Hello, World!";

TELL ME WHY!
Expand all images
>> No. 5640
How would you tell the compiler otherwise which statements belong to the function?

Your suggestion would work if all functions always had only 1 statement.

And if you're going to say "Yeah but the statement is behind the declaration, and thus I'm indicating that that is all that belongs to the function" you're going into the madness that is python I believe where whitespaces actually have meaning.
>> No. 5641
>>5640
Not OP, but I've wondered this.
For if statements, you can have:
if(x) y();

or
if(x){ y(); }

So, why not for functions? If you want more than one line in a function, you must add braces.
>> No. 5642
Python.

Also, it's done for readability. Makes it easy to see exactly what part of the code belongs to what header.
>> No. 5644
File
Removed
>>5641
OP here. Indeed. It appears to be quite arbitrary. There are clearly benefits, otherwise it wouldn't be kept for ``if'' statements. I might have an idea, there is the dangling else problem:

if (foo) if (bar) mu(); else zot();


or is it:

if (foo) if (bar) mu(); else zot();


Likewise, this could apply to try/catch:

try try foo(); catch bar();


It becomes confusing to a reader and parens must be stuck in to make it clear. BUT we recognize that there are benefits to not using braces. And we're not nesting them all the time. Anyhow, functions only have one branch anyway so this problem doesn't even apply to them.

What an annoying inconsistency.

Rage building...

>>5642
I call bullshit.
>> No. 5645
>>5644
Disregard the try/catch one, a try is meaningless without a catch or finally clause.
>> No. 5650
>>5642

OH HALP ME I CAN'T BE BOTHERED TO READ OTHER TWO LINES OF MAH CODE
>> No. 5652
>>5650
> I CAN'T BE BOTHERED TO WRITE OTHER TWO LINES OF MAH CODE
fix'd
>> No. 5653
You could just do this:
string hello() {return "Hello, World!";}
>> No. 5657
>>5650
>>5644
I stand by it. What's the pain in writing two additional characters? It helps ensure a page of code is unitized.
>> No. 5659
Write your own parser.

Enjoy the enlightenment that then occurs.
>> No. 5684
Blocks have their own variable scope, or stack if you will. Functions are constructs that take in parameters and hence must have their own scope otherwise pass-by-value semantics won't work.

That's the boring history-lesson version anyway.

My own version is: WTF ARE YOU DOING WRITING 1-LINE FUNCTIONS ANYWAY
>> No. 5688
>My own version is: WTF ARE YOU DOING WRITING 1-LINE FUNCTIONS ANYWAY

Indeed.. you can do it like this

#define hello() "Hello, World!"

strcpy(some_string, hello());
-or-
printf("%s\n", hello());

:)
>> No. 5698
>>5684
>>5641 here
Makes sense, but don't if statement blocks have their own scope too?
>> No. 5699
File 12571862055.png - (486.45KB , 800x532 , 125691534878.png )
5699
>>5657
> What's the pain in writing two additional characters? It helps ensure a page of code is unitized.

I don't think you know what "unitized" means.

There is pain because it takes up space, I have to write more and manage more code. Writing code isn't just about typing it out in one go you realise, code has to be edited. More syntactic characters is more time.

It is also inconvenient to read. If I am reading

if (foo) { someStuff(and(extra),stuff); }

vs

if (foo) someStuff(and(extra),stuff);

I'd prefer the latter because I have to inspect the former to count how many statements there are. With the latter I do not. I know syntactically that no braces means one statement.

> WTF ARE YOU DOING WRITING 1-LINE FUNCTIONS ANYWAY
Some functions are just one line of code. Get it?

>>5698
Yes, they do. >>5684 doesn't have a clue what he's talking about or is trying to troll us.

Anyone actually have a good reason?
>> No. 5700
>>5699
Yes. Code has to be edited, re-visited, and seen by other people. That's why you don't write code like you did in your example.

A more important point: what are you guys writing that needs so many single-line functions that it saves any meaningful amount of time/effor? Single-liners rarely happen outside of get() and set() methods.
>> No. 5701
>>5700
> Yes. Code has to be edited, re-visited, and seen by other people. That's why you don't write code like you did in your example.

I just explained in my post why I thought it was more readable. So you have essentially said "I don't think that's true", well, whoop-de-do. How about some refutation and counterargument, if you are capable?

> A more important point: what are you guys writing that needs so many single-line functions that it saves any meaningful amount of time/effor?

Not really an important point, because...

> Single-liners rarely happen outside of [common occurrence]

Yup. Other examples include method overloading, mathematical functions, string manipulation, [any piece of code that shouldn't be repeated]. I can't believe I'm actually having to argue the case for a function of any short length. Short functions are good functions.

What's your problem with short functions? Is this some kind of desperate straw man to argue against to prove that the convenient and easy to read braceless function declaration is somehow a bad idea?
>> No. 5702
>>5701
Not short functions, single-line functions. As in print-functions and get/set methods.

Also, non-braced functions are not any more convenient or easy to read than braced functions. I've yet to see anyone offer evidence for that.
>> No. 5703
>>5701
Creating a single-line function to avoid repetition sort of misses the point, because instead of the single line in your function you are now repeating calls to your function.
>> No. 5705
Supposedly this has something to do with function definitions being "special" from the parsers poit of view, and not being part of the statement/expression division that takes part inside the functions.
>> No. 5706
>>5699
God damn, why does every /pr/ thread turn into high-school at some point? Back to your angsty blog please
>> No. 5708
File 125720392831.jpg - (35.75KB , 264x373 , baww.jpg )
5708
>>5703
> Creating a single-line function to avoid repetition sort of misses the point,

Speaking of missing the point...

You are either completely retarded or trolling me.

DebugMsg("Excellent work, Nigel."); vs. Console.WriteLn(String.Format("Debug message: Excellent work, Nigel."))


A one lined function.

> Supposedly this has something to do with function definitions being "special" from the parsers poit of view, and not being part of the statement/expression division that takes part inside the functions.

Yeah, we can see that the parser doesn't do it. Thanks. Go back to bed.

> God damn, why does every /pr/ thread turn into high-school at some point? Back to your angsty blog please

Pic related.

> Also, non-braced functions are not any more convenient or easy to read than braced functions. I've yet to see anyone offer evidence for that.

You're sitting around waiting for bullshit evidence that's about as likely as evidence that braced functions are any more convenient or easy to read than braced functions. I've yet to see anyone offer evidence for that.
>> No. 5710
>>5708
>>You're sitting around waiting for bullshit evidence that's about as likely as evidence that braced functions are any more convenient or easy to read than braced functions. I've yet to see anyone offer evidence for that.

Okay. Well let's pin it down to languages then, so we can see what looks better and what doesn't. Perl is a no-go, purely based on it being a procedural language. It also wouldn't work in Java and C# due to nested classes, anonymous classes, etc.

So um. What language are we talking about again?
>> No. 5711
>>5708
In C, a #define would generally be more suitable than a function for a onliner like that.
>> No. 5713
>>5711
Yeah, but single-line functions are totally negligible. It's adding extra effort to the parser for no gain.
>> No. 5723
Ok I'll just go ahead and assume OP is hinting at C as an extension of what >>5710 deduced.

As a good practice, each line should have as few sequence points as possible, preferrably 1 only.
http://c-faq.com/expr/seqpoints.html
This is to improve code readability, debugging, and avoid side effects. Clearly such a line will comprise little more than a handful of instructions. When you try to write a single line function, the overhead of context switching, copying parameters and so forth mean that you're practically incurring a 50% speed penalty, than just copying and pasting whatever one-liner that function was supposed to do. So again, why are you writing one line functions.

Now this is C we're talking about so get/set functions don't come into this. C# has the concept of properties which greatly simplifies writing getter/setters but still needs braces, because that makes the syntax consistent. Plus modern managed languages seem more verbose than C/C++ so two extra chars in their environment are even less significant.

>>5698
>Makes sense, but don't if statement blocks have their own scope too?
not if you don't include a block, which means a new scope is created. Single line if statements have no additional scope - why would they, when you can't declare variables and use them within one line.
Well ok, you can do
if(true) int a = 3;

but IIRC that'll trigger warnings if you compile with -anal -retentive
>> No. 5727
>>5708
sprintf("%d is a %s and needs 2 lurn 2 not use the console", 5708, "nigger");
</troll>
>> No. 5728
#define hello() "Hello, World!"
>> No. 5733
>>5723
> Ok I'll just go ahead and assume OP is hinting at C
Not sure how you got C from my various blatant C# snippets.

> When you try to write a single line function, the overhead of context switching, copying parameters and so forth mean that you're practically incurring a 50% speed penalty, than just copying and pasting whatever one-liner that function was supposed to do.

Typical C programmer; obsessed with CPU cycles. To you, I suppose, spending a load of time optimising every line of code is a better use of time, than, say, spending a short time writing big systems that actually do something.

I don't think "having a function incurs a speed hit" is a very good argument against a function that happens to contain one sequence point.

int max(int a, int b) { return a > b? a : b; }


void DebugMsg(string msg) { Console.WriteLn("Debug message: " + msg); }


Some thing just shouldn't be repeated. What if I want to change "Debug message: "?

Do you propose that for functions like this we ought to use macros because of the speed hit?

I'm not convinced that the speed hit of calling a function is particularly bothersome for most of the code of most of the software out there. Oh no, my debug messages aren't fast enough?

... but I do have a morbid interested in anyone advocating macros.

> So again, why are you writing one line functions[?]

I like abstract, maintainable, readable code, which demands means of abstraction like functions. I have no anal speed concerns, therefore the length of those functions is of no concern either. (If I had speed concerns, I'd be optimising hot spots anyway.)
>> No. 5734
>>5733
But again, how often do you write functions like that? Maybe one in two hundred lines?
>> No. 5760
>>There is pain because it takes up space, I have to write more and manage more code.

That's a stupid reason btw.. Go visit the obfuscation contest website (http://www.ioccc.org/main.html) and see how they put entire games into just a few lines of code.

It's not readable, but hey it's short right?

Manageable code isn't short, it's _understandable_. It's not short, it's _concise_. There is a big difference there.

So I think you are barking up the wrong tree here.

Also, for those who say that a one line function is too slow, that's what the __inline__ directive was made for.
>> No. 5763
Everyone in this thread is an idiot.

They probably require braces because they had a parser issue with old-style C functions.

[pre] int printline (x) char * x; { printf ("%s\n", x); } [/pre]


All this "force you to write beautiful code" shit is just unbearably stupid.
>> No. 5775
>>5760
> That's a stupid reason btw.. Go visit the
No.

> [A load of bullshit]. So I think you are barking up the wrong tree here.

I think you're barking up the wrong tree if you wish to convince me by strawman fallacy that braceless functions are neither manageable, understandable or concise because some code you saw that was short was unreadable. Short code is not necessarily unreadable. If it's equally as readable when shorter, why waste brain cycles, screen space and editing time on more syntax?

>>5734
> But again, how often do you write functions like that?
And again: often enough.

>>5763
More convincing than any of the other points in this thread.
>> No. 5778
>>5775
Neither C# nor Java could possibly have braceless functions.
>> No. 5782
sure is high-school in there
>> No. 5783
>>5782
Keep saying "high school" and eventually someone will think you're being insightful.
>> No. 5785
>>5708
what are you doing with a furry pic?
>> No. 5786
sure is high-school in here
>> No. 5789
This thread:

What OP said:
"Why do all functions need braces?"

What OP meant:
"I have this great idea and I want you all to admit that it's great and I'm great"

[Several reasons, some valid some speculative]

OP: Waaah you're all stupid

Another day in a programming chan board, I guess.
>> No. 5791
OP, you are strange.
On one hand, you need one-line functions in order to wirte readable code. On the other hand, you dislike the fact that C standart forces you to write readable code.
[Return]


Delete post []
Password  
Report post
Reason