java just curly braces

后端 未结 5 1246
一整个雨季
一整个雨季 2020-12-29 05:21

I was reading a book and there were a few example with programs that has just curly braces

for example

 public static void main(String args[]){
             


        
5条回答
  •  天涯浪人
    2020-12-29 06:20

    This idea of how to use curly braces as a coding construct is a debated issue in the Java world . There are several explanations people come up with when they see curly braces by themselves. So Im going to try to answer your question from a practical perspective.

    The implied question in your post here is, really - when/why are these used ? Practically speaking, the following cases might result in a lone code block :

    1) The programmer wanted additionally scoping to reuse variable names without fear of collisions for clarity (i.e. making several objects of the same type in a unit test or database insertion block).

    other possible reasons :

    2) Forgotten if/else/for/while loop code that is under development.

    3) Remaining artifact of a removed if/else/for/while clause.

    4) Autogenerated code uses scoping to simplify the creation of several similar components with identical variable names (i.e. consider a gui generator that needed to make code for 100 radio buttons - rather than incrementing variable names per button, it could use scoping).

    5) As a tiny, reusable, pastable logical block with minimal side effects : the programmer felt like a block of code in a method was so obscure, its variables and internal side effects should have minimal visibility to the outside world. That is, the programmer has used a code block as a poor-man's anonymous lambda function (albeit, one without a return value). In this pattern one might do something akin to the below :

    //lets say I want to make a primary key for a dogs name in a database. 
    String dogNameKey=null;
    {
        long time = System.currentTimeInMilliseconds();
        String theName = "spot";
        dogName=theName+"_"+time;
    }
    

    Clearly, the simple strategy for naming this record (dogNameKey) is not worthy of an external method - its too simple. But at the same time, the "time" variable should have no bearing or accessibility outside the logic for making this name up - i.e. it shouldn't even be relevant to the method which contains this tiny key generating block. So, by using braces, I've scoped it out . If a labmda were possible, than all of this scoping could be wrapped in a single, anonymous function.

    Now - I could paste several of these blocks, and the variable names would be identical, so it would be easy to scan them by eye.

    *Thus, when you see curly braces by themselves - they usually are pretty important - either they implement a specific custom-scoping, or they are an artifact of an error or potentially of autogenerated code. Scoping can also be used to "start" the refactoring of a method without actually writing a new method, by separating out its independant parts ... although IDEs are much better at this than humans. *

提交回复
热议问题