aspose file tools*
The moose likes Meaningless Drivel and the fly likes curly brackets: which side are YOU on? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Other » Meaningless Drivel
Bookmark "curly brackets: which side are YOU on?" Watch "curly brackets: which side are YOU on?" New topic
Author

curly brackets: which side are YOU on?

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

sometimes i use code i get from the internet like this from Oracle

i always change it to look like this

curly braces should line up IMO


SCJP
Visit my download page
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4366
    
    8

I go with neither .

(Specifically, I use the first style by preference, if I have the choice, except that I'd put catch, else etc on a new line).
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4179
    
  21

I use the second, except when I have to use the first or some similar convention. Fortunately my IDE lets me configure different formats and switch between them whenever I like


Steve
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61063
    
  66

Matthew Brown wrote:I go with neither .

(Specifically, I use the first style by preference, if I have the choice, except that I'd put catch, else etc on a new line).


This is my style as well.

And I know that you used "side" tongue-in-cheek, but there are some people out there who make this religion. I say use whatever style you think brings the most clarity to the code (or the style dictated by your group's style recommendations), and be consistent in its use. We see so much code posted here that looks like it was indented by having a cat jump up and down on the keyboard, or perhaps by a roll of a 20d die.

Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2337
    
  28

How about I don't care? While reading code my brain skips over the curly braces anyways. As long as the indenting is correct, I don;t need the braces. The braces are for the compiler not me. The indentation is for the humans not the compiler. You need both. Also, while typing code, the braces come out of my hands automatically. I don't have to think about the braces. I think "block of code", and my hands automatically put braces yeah I'm that good wot wot

What drives me crazy is a) incorrect indentation(how do you read that shit) and b) not using the braces(because what you are telling the compiler might be different than what you will tell the human reader, and now I can't trust my eyes anymore). If I see any of these 2 things, the code review is automatic failure. Not even going to read it unless it;s formatted correctly.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

Jayesh,
they have a language, i think it is Jthon or something like that. it is just like java(more or less),and Python i guess, but the point is it uses no braces. only indentation. i like it in theory, but i don't know......................

Bear,
i didn't know you were into D&D
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3285
    
    7
Jayesh A Lalwani wrote:How about I don't care? While reading code my brain skips over the curly braces anyways. As long as the indenting is correct, I don;t need the braces. The braces are for the compiler not me. The indentation is for the humans not the compiler. You need both.

Python eliminates this redundancy I like it.

BTW, when you type code in MS Visual Studio, it is indented automatically. I like that. Is there any Java IDE that does it? (I know you can format it using a shortcut but I am talking about getting it indented automatically as you type and hit enter after a line.)


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

i am old school. i don't use IDE's.
i write in a fantastic program called Notepad++(and compile and jar from command prompt).
it is free.
it does the indentation for you most of the time after you hit enter.
sometimes you have to backspace though or hit tab. it is not perfect but it helps.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61063
    
  66

Randall Twede wrote:the point is it uses no braces. only indentation. i like it in theory, but i don't know

I'm not genrallly a fan of languages in which whitespace is significant.

i didn't know you were into D&D

At one point in the distant past, I was a DM.

Paul Anilprem wrote:Is there any Java IDE that does it?

IntelliJ does that (and configurably). I can't imagine that most Java IDEs do not.
Frank Silbermann
Ranch Hand

Joined: Jun 06, 2002
Posts: 1387
If your lines are so long or have so many levels of indentation that your indentations must be small, then put the opening brace on is own line.

Otherwise, put the opening brace at the end of the line, and the else/catch on the same line as the closing brace so you can see more on a CRT screen.

If your methods are very small and simple (as the gurus recommend), then it probably doesn't matter.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2337
    
  28

Yeah if you have so many indentations that you are running out of screen, your cyclomatic complexity is going off the chart.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4179
    
  21

Paul Anilprem wrote:Python eliminates this redundancy I like it.

I like Python, but the reliance on white space for defining a block I hate - probably the think I hate the most about the language.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

I prefer the style in your first code sample. It's the most common style used in Java programs (at least in the places where I've worked). I'm not religious about one style above another, however. I adapt to whatever style the place I'm working at is using.

Consistently using one style is more important than exactly what style you're using. Code becomes much harder and more annoying to read when it's written in an inconsistent a mixture of styles.

All those different styles have names. See: Indent style on Wikipedia.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5

curly brackets: which side are YOU on?



On the side of the closing one ;-)

(I am just kidding.)

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4366
    
    8

Steve Luke wrote:I like Python, but the reliance on white space for defining a block I hate - probably the think I hate the most about the language.

I'm not a big fan of that feature either, but I've come to the conclusion that it actually makes Python a good language for teaching introductory programming, because it forces beginners to indent their code consistently right from the very beginning.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

I do:


I hate python's use of white space instead of brackets. Its just personal taste, but my mind is made up, so don't bother to try to change it.
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1007
    
    3
Pat Farrell wrote:I do:

I hate python's use of white space instead of brackets. Its just personal taste, but my mind is made up, so don't bother to try to change it.


If you had to do SQL programming, would you use the analogous format:



I'm not a fan of tie fighter elses and catches. My SQL does follow my Java, C#, etc. style.



I still don't know how I feel about capitalizing reserved words in SQL.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Ryan McGuire wrote:I still don't know how I feel about capitalizing reserved words in SQL.


SQL was never intended to be used by people. It was a theoretically correct/complete system to prove that and RDBMS can have specific properties that were important at the time (mid-1970s). Even with today's extensions, no normal person can use it.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Pat Farrell wrote:SQL was never intended to be used by people.

Was it not? I remember seeing an old advertisement sometime somewhere from the 1970's, for some RDBMS system, explaining that with SQL you could easily find whatever you wanted in the database. The idea was that business people (not programmers or other technical specialists) would write SQL to get whatever data they needed for their business decisions. It's so easy, you know, it's almost like asking the computer in English what you want...
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Jesper de Jong wrote:I remember seeing an old advertisement....The idea was that business people (not programmers or other technical specialists) would write SQL to get whatever data they needed for their business decisions. It's so easy, you know, it's almost like asking the computer in English what you want...


You sure that it was in the 70s? I remember the early 80s as the time of big wars between hierarchical, indexed and relational systems. But it was a long time ago.

Right, it was an advertisement written by some slick mad-men. SQL was an attempt to have an English-like command front end to relational-calculus. When the RDBMS vendors started to steamroll all the other databases designs, even they knew that relational calculus was not usable. Not usable even by technical specialists, let along a manager's secretary.

Jane, please get me a list of the top salesmen by region who have exceeded their quotes by 25%.

I remember using Oracle in the mid-80s, they had something called UFI, User Friendly Interface. It was an amazing name for one of the most user (programmer) hostile pieces of software that i've ever been cursed with using.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

No, I'm not sure about the 1970's, could have been the 1980's.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7676
    
  19

Pat Farrell wrote:SQL was an attempt to have an English-like command front end to relational-calculus.

And it failed then; and it's failed for the 30-odd years since, despite which vendors still try to tell us that it's worth having.

Just try to write a piece of SQL that gives you the "top 10" or even the "first 10" of whatever you're trying to select. Back in the day, it was bloody difficult, which was what convinced me that SQL was not where I wanted to be.
Now? Easy as pie, provided you're willing to learn the 38 different proprietary ways of doing it.

And as an admin language? Perleeeese.

SQL - if it actually even qualifies as a "language" - is the invention of the Devil (who I've always thought was probably in Marketing) and, to my knowledge, is still the only one so far that requires a Rosetta Stone.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Yet, SQL still exists and it used everywhere where there's a relational database, apparently nobody in 30+ years has come up with something that's good enough to replace SQL.

I rarely have to write anything in SQL these days, usually I use Hibernate, but ofcourse there's still SQL underneath that (and HQL is also a kind of SQL dialect).
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3285
    
    7
I find sql to be quite intuitive (for the most part). Not sure why it is being criticized so much. It may not be perfect but I think if it hasn't been replaced in 30 yrs, it must be doing something right
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7676
    
  19

Jesper de Jong wrote:Yet, SQL still exists and it used everywhere where there's a relational database, apparently nobody in 30+ years has come up with something that's good enough to replace SQL.

Well, I worked on Progress databases for quite a while, and they only supported SQL from about version 5 (I think), and it wasn't until about version 8 that anyone who actually used the db would even consider it, because the native language was so much better (and still is, IMO). AFAIKT, SQL was basically bolted on to provide access to "outsiders" (pesky things, those).

And just FYI: Progress was also one of the first companies to use bytecode; back in the days when it wasn't even an established term, and Java was still a glint in the milkman's eye.

Winston
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7676
    
  19

Paul Anilprem wrote:I find sql to be quite intuitive (for the most part). Not sure why it is being criticized so much. It may not be perfect but I think if it hasn't been replaced in 30 yrs, it must be doing something right

VHS wasn't replaced until DVDs came along; but would anyone actually claim that it was the best?

Somebody (possibly you) said that this is entirely down to marketing; and I suspect they're right. SQL covers most of the bases it was intended to; and for those it doesn't, vendors have written their own proprietary add-ons, so you can usually get it to do more or less what you want.

And the value of SQL is not in the language itself, but that it gives you access to a huge datastore that is specifically designed to manage and hand you vast amounts of information quickly. Hell, I'd re-learn COBOL if it was needed for that sort of power.

Winston
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3285
    
    7
Winston Gutkowski wrote:
Paul Anilprem wrote:I find sql to be quite intuitive (for the most part). Not sure why it is being criticized so much. It may not be perfect but I think if it hasn't been replaced in 30 yrs, it must be doing something right

VHS wasn't replaced until DVDs came along; but would anyone actually claim that it was the best?

Somebody (possibly you) said that this is entirely down to marketing; and I suspect they're right. SQL covers most of the bases it was intended to; and for those it doesn't, vendors have written their own proprietary add-ons, so you can usually get it to do more or less what you want.

And the value of SQL is not in the language itself, but that it gives you access to a huge datastore that is specifically designed to manage and hand you vast amounts of information quickly. Hell, I'd re-learn COBOL if it was needed for that sort of power.

Winston

I did not say that SQL is the best. Just that I don't find it as complicated as it is being made out to be.

VHS and DVD analogy doesn't apply here because DVD fulfills a need that VHS simply cannot i.e. the size and accessibility of the data. If there is a need that SQL cannot fulfill then it is up for replacement. No doubt about that.

I also do not think that SQL is pure marketing. If it were just marketing, I am sure an easier and more intuitive way to access data would have come along in 30 yrs. That is not to say that something will not come along in the future but just that as of now, I find it ok for the job that it is doing.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Paul Anilprem wrote: Just that I don't find it as complicated as it is being made out to be.


Ok, SQL drives me nuts. The problem is that its not a linear language, simple things are simple, but slightly more complex things are much more complex. Really complex things are often impossible with the "standard" SQL so you use implementation specific extensions which are not portable. You end up with really complex things that are next to impossible to understand, let alone optimize.
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3285
    
    7
Pat Farrell wrote:
Paul Anilprem wrote: Just that I don't find it as complicated as it is being made out to be.


Ok, SQL drives me nuts. The problem is that its not a linear language, simple things are simple, but slightly more complex things are much more complex. Really complex things are often impossible with the "standard" SQL so you use implementation specific extensions which are not portable. You end up with really complex things that are next to impossible to understand, let alone optimize.

I agree with that. Though the same applies to many things such as perl or regular expressions.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7676
    
  19

Paul Anilprem wrote:VHS and DVD analogy doesn't apply here because DVD fulfills a need that VHS simply cannot

You obviously misunderstood what I was trying to say. VHS was the dominant cassette and VCR technology for more than 10 years, despite the fact that pretty much everybody agrees it was vastly inferior to its competition. You could say the same about Windows - in the opinion of many (including me), the worst of the major desktop systems around, yet still the market leader.

The fact is that there's no real incentive to write a replacement for SQL. It does the job adequately, if clunkily, and people are "familiar" with it. It's also the standard on so many major databases that you can't really get away from it if you want to use them. Progress tried, and I'd say that their language is vastly superior to SQL for applications development; but it never "caught on", so it's just another proprietary language.

Just to give you a taste:That "of" is a fabulous construct: totally relational and extremely compact; and the mere fact that you can write a procedural loop for a table without having to set up all sorts of stupid cursors is very intuitive. Trouble is, it's still a single-db language.

With SQL (as opposed to (ugh) PL-SQL), even these days, you still have to get the data and then decide what you want to do with it, which is why we end up with monstrosities like ResultSet.

Winston
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

Well, it is more compact, but not fundamentally different from a PL/SQL's equivalent:

Moreover, in Oracle Forms you can use PL/SQL on the client side too (hopefully I'm not mistaken, I've never used Oracle Forms), so you could use this construct even on the client, not having to mess with cursors or result sets.

Still ugh?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7676
    
  19

Martin Vajsar wrote:Still ugh?

Well, maybe not quite so 'ugh' as I thought, but I still find PL/SQL syntax pretty clunky.

TBH, I suspect both languages are showing their age, and with the new wave of NoSQL and graph bases coming up, maybe I'll get my wish to be rid of the yoke of SQL. There must be a better way than the current "fetch, then process" paradigm.

However, while its still in place, I tend to agree with Jesper: give me Hibernate any day of the week.

Winston
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4366
    
    8

I think the problem with both examples is that they are trying to bolt on an imperative language onto a declarative language, and that mismatch is always going to be a problem. SQL works fine when the action isn't "fetch then process" but "fetch matching these conditions and I'll leave the details up to you". Once you go beyond things that model is good at it gets messy.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7676
    
  19

Matthew Brown wrote:I think the problem with both examples is that they are trying to bolt on an imperative language onto a declarative language, and that mismatch is always going to be a problem. SQL works fine when the action isn't "fetch then process" but "fetch matching these conditions and I'll leave the details up to you". Once you go beyond things that model is good at it gets messy.

You may be right, but for anything other than bulk updating, the chances are virtually 100% that the results of an SQL statement are likely be used in some imperative way, so you get hybrid languages like PL/SQL. Progress, bless their cotton socks, at least tried to make the database look like part of an imperative whole. For the rest of us, and particularly for "foreign-ware" like Java apps, we're reduced to dealing with SQL in the raw, and then handling its results via monstrosities like ResultSet (140-odd methods at last count, if memory serves).

And as for an admin language? I used to suffer from severe GRANT fatigue.

Winston
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1658
    
  14

Oh goody, a language war!
Braces:I used to put them on a separate line because my first programming language was Pascal with BEGIN/END around blocks. These days I put the opening brace on the same line as the "if..." or function declaration, partly because I read somewhere (Bear's book?) about how Javascript interprets a function/closure differently depending where you put the braces. NB I may have completely misunderstood this point, so my apologies in advance to anybody who knows better! And I like Python and find the whitespace thing easy enough provided I tell my editor not to use tabs.

SQL: What on earth is the problem here? Sure, SQL is not Turing-complete and it's not a perfect implementation of relational algebra, but so what? I started using it in my first IT job in 1987 and I've used it most working days since then, and I can't think of another language I've used that is as well-suited to its purpose, so easy to learn and easy to read. Most people I encounter who have problems using SQL are really having problems breaking out of their imperative/procedural mindset and failing to think about what they are trying to achieve in relational/set-oriented terms: they're thinking about the process rather than the data. Just look around the database forums and you'll see plenty of evidence of this. Don't complain that a screwdriver makes a lousy hammer.

As for PL/SQL, obviously it's a proprietary procedural extension to SQL, so if you don't want that kind of thing, don't use it. But I remember the pain and tedium of coding Oracle applications in C with Pro*C to handle the SQL calls, and it was a huge relief when Oracle finally came up with an alternative. The syntax is dead simple, it's easy to write/read, offers great advantages through tight DB integration, and I can almost invariably beat my Java colleagues for conciseness, performance and scalability on any process requiring SQL over large data volumes plus procedural logic on the server. Like I say, if you want avoid using proprietary tools, or having to persuade your developers to use different languages for different purposes, then PL/SQL is clearly not for you. But it's pretty good at what it does, and turns the Oracle RDBMS into a virtual machine for database applications.

I dunno, Javaranch is full of really smart people, so if a relative idiot like yours truly can master SQL and PL/SQL, you guys should have no problems!


No more Blub for me, thank you, Vicar.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4366
    
    8

Winston Gutkowski wrote:
You may be right, but for anything other than bulk updating, the chances are virtually 100% that the results of an SQL statement are likely be used in some imperative way


Actually, even as a developer I reckon the majority of SQL queries that I write produce results that are just pasted into a spreadsheet and emailed to somebody. Most of them never go near any other code.
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1658
    
  14

Martin Vajsar wrote:Moreover, in Oracle Forms you can use PL/SQL on the client side too (hopefully I'm not mistaken, I've never used Oracle Forms), so you could use this construct even on the client, not having to mess with cursors or result sets.

Still ugh?

Yes, PL/SQL has been part of Forms since the early 90s. It was sorely needed because until then the only way you could do "if....else..." in Forms was with some nasty kludges based on "SELECT...FROM dual WHERE..." returning/not returning a dummy value based on your WHERE condition. Ouch!
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1658
    
  14

Matthew Brown wrote:
Winston Gutkowski wrote:
You may be right, but for anything other than bulk updating, the chances are virtually 100% that the results of an SQL statement are likely be used in some imperative way


Actually, even as a developer I reckon the majority of SQL queries that I write produce results that are just pasted into a spreadsheet and emailed to somebody. Most of them never go near any other code.

Many of the queries I've been writing recently are trawling through large volumes of data, constructing new records on the fly from multiple sources, and passing this data on for more processing in various statistical tools. There's more to serious database applications than shopping carts and payroll. ;-)
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7676
    
  19

chris webster wrote:Oh goody, a language war!


Braces:...

I didn't say anything because I don't have any strong opinions on the subject, except: be consistent. I personally put my opening ones on the same line, but I can quite understand why somebody might not.

SQL: ...Most people I encounter who have problems using SQL are really having problems breaking out of their imperative/procedural mindset and failing to think about what they are trying to achieve in relational/set-oriented terms: they're thinking about the process rather than the data.

Well, having spent a couple of years on a project where they used data-directed design, all I can tell you is that I don't like it (or rather, I didn't like it; it was back in the mid-eighties). And the perverse thing is: I love databases. I think they're absolutely brilliant at what they were designed for; and I've been a DBA, on and off, for more than twenty years, so I do actually spend quite a lot of time thinking about 'the data'.

For queries, and some (generally straightforward) updates, SQL is fine; for processes - and I hate to say, but most applications do involve them - it's crap. It's just not crap enough to make people spend a lot of time and money looking around for an alternative. Maybe the next generation of db's will change that.

And I say again: as an admin language, it sucks big time.

As for PL/SQL, obviously it's a proprietary procedural extension to SQL, so if you don't want that kind of thing, don't use it.

Actually, if I had to, I probably would; I'm just not wild about the syntax. Oracle used to trumpet their Java support all the time, but I don't see any sign of a Pro*Java anywhere. Seems to me like that might be a good place to start, rather than forcing us to plough through JDBC and ResultSets. The closest I've seen to what I'd like is Hibernate, which does hide a lot of the basic grind from you; but for more involved stuff, you're still reduced to the "fetch and process" paradigm, just with HQL rather than SQL.

I dunno, Javaranch is full of really smart people, so if a relative idiot like yours truly can master SQL and PL/SQL, you guys should have no problems!

Mastery isn't the issue. Hell, I "mastered" COBOL; doesn't mean to say I liked it. Even back in the 70's, I envied the Fortran bods.

Winston
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

Winston Gutkowski wrote: Oracle used to trumpet their Java support all the time, but I don't see any sign of a Pro*Java anywhere.

Sorry for playing the devil's advocate here, but have you ever heard about SQLJ? Oracle took part in designing it and even supports it in its JDeveloper IDE (and perhaps in SqlDeveloper as well - not sure about this). I had high hopes for this when I started learning Java, but it turned out that major IDEs don't support it (and I profoundly dislike JDeveloper). So, the tool does exist, but the demand apparently doesn't.

I'm still not sure whether your issue with SQL is the relational algebra in itself, or the way SQL encapsulates it?

I don't have any idea what could be done about GRANTs. After all, it is just GRANT (rights) ON (objects) TO (dudes). All but the three keywords just name the objects. Which better way of doing it would you imagine?
 
permaculture playing cards
 
subject: curly brackets: which side are YOU on?