Different placement of curly braces [closed] - c

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Sometimes when I look up videos at Youtube for better explanation and usually these people places the curly braces differently.
Note that , I'm new to the coding world.
1st placement.
for(int i=1 ; i < 6 ; i++)
{
//Statements
}
2nd placement
for(int i=1 ; i < 6 ; i++){
//Statements
}
These 2 codes actually functions the same , it just has different placement of the curly braces.
What's the difference of these 2 placements? Why do some others use the 2nd placement instead of the first one? I actually prefer the first one, because its easier to spot errors and looks neat.

1st and 2nd are the same.
The only difference is the coding style.
While you work for a company X they tend to use one coding style just for the sake of consistency. It does not matter if you use the first or second style as long as you are writing it, in the same way, all the time.
It just might confuse other developers who will have to read your code at some point.

Related

In Go's for range statement, Why the life cycle of loop body variables doesn't limit to a single loop [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I am new to Go and used to be a javaer. Recently I came along such a problem,like the code below, the first snippet will cause assignment problem while the second will not.
But I don't know why.
desiredAcls := make([]*kafkav1alpha2.Acl, 0)
for _, acl := range instance.Spec.Authorization.Acls {
desiredAcls = append(desiredAcls, &acl)
}
desiredAcls := make([]*kafkav1alpha2.Acl, 0)
for _, acl := range instance.Spec.Authorization.Acls {
cpyAcl := acl
desiredAcls = append(desiredAcls, &cpyAcl)
}
In constrast to Java,there will be no problem and I am very free to write code cause the life cycle of loop body variables limits to a single loop.
List<Object> acls = new ArrayList<>();
List<Object> desiredAcls = new ArrayList<>();
for (Object acl : acls) {
desiredAcls.add(acl);
}
So why doesn't golang limit the life cycle of loop body variables to a single loop? this kind of optimization can reduce a lot of coding burden for engineers.
But I don't know why.
Because in the first example, you're taking a pointer to the loop variable. Each iteration doesn't create a new variable, it overwrites the value of the same loop variable, so every iteration you're taking a pointer to the same memory.
So Why didn't Go abolish Pointers?
Pointers are useful. But to really answer this question, you'd have to ask the Go language designers.

Should I put the game logic inside the draw loop? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I'm making a simple game using CSFML and am wondering if I can make my code more efficient.
Right now the code looks like this inside the main function's game loop:
// game logic
for (int i = 0; i <= perf.arramount; i++)
{
UpdateBody(&body[i]);
}
// clear
sfRenderWindow_clear(window.window, window.bg);
// draw
for (int i = 0; i <= perf.arramount; i++)
{
sfRenderWindow_drawRectangleShape(window.window, body[i].rect, NULL);
}
// display
sfRenderWindow_display(window.window);
I was told to do things in the order: logic, clear, draw, display. However, would there be any issues with me putting the logic inside the draw loop like this?
// clear
sfRenderWindow_clear(window.window, window.bg);
// draw
for (int i = 0; i <= perf.arramount; i++)
{
UpdateBody(&body[i]);
sfRenderWindow_drawRectangleShape(window.window, body[i].rect, NULL);
}
// display
sfRenderWindow_display(window.window);
Would it slow down the draw calls or make the code more efficient?
I'm not sure if this specific library may be different from others, so even a general answer is appreciated.
How to structure your game is your design decision.
You will be the first to notice problems with it.
But here is my experience. I learned that in the game logic loop you have calculations which are definitly needed, they also tend to need information on how much time has passed. In the drawing loop you just need to visualise the result of the calculations. As soon as you are going to split into threads, the timing of the need to calculate and to visualise will change. You are likely to want to calculate more often than you draw, while you want to draw always completly (except for more advanced FPS-based level of detail rendering....).
So there are multiple aspects which might make you want to keep those two things apart.
What I did was having a calculation step (which by my design gets real time information as a parameter of elapsed seconds) and a separate drawing step. That way you can have them in one parent loop, but you are free to move them at any point AND you are protecting yourself from confusing those two steps.

How can I write an array of arraylist in the uml? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
This is my array of arraylist: Arraylist[] a = new Arraylist[SIZE];
I’m struggling with writing it in my UML diagram, how can I write it?
With or without the <>?
The simplest way is to define it this way:
a is of type Arraylist (after the colon) with multiplicity 0..* and its default (after the equal sign) is Arraylist[SIZE].
As commented by #bruno the default value is a bit of interpretation. UML basically should be held language agnostic, but sometimes you just want to point out implementation details (for whatever reason). So you can add the new keyword right in front of the Arraylist[SIZE]. What that actually means is language dependent (and so out of a general scope I like to stick to).

Which code style is better in terms of readability and performance? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I am wondering which code style is better for
human readability
program performance
Let's imagine we have 2 functions:
// first one
void foo(void) {
if (error)
exit(1);
....
}
// second one
void bar(void) {
if (!error) {
....
}
else
exit(1);
}
Both of them work in the same way in terms of execution, but which code style is preferable?
If I had to choose out of these two only, I'd choose the first one.
Reason:
It's simple. (does not use any operator like !)
It does not need comments to explain what happens inside.
(self-readable code)
It avoids extra pair of { } which makes the code more readable
Both performs nearly the same, I highly doubt there will be a
difference in performance.
Hence, first one is preferable.

Accessing multidimensional arrays without using loop? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Hi friends I was working on a project where we need to use quite a few multidimensional arrays. I am using for loops to access different array elements, then I just thought what if I don’t have a liberty to use looping? How am I going to access array element?
I am new to C, so thought of discussing here, I am sure there might be thousans of people who could have thought the same way, and hopefully found the solution.
Below example of multidimensional array is give, please guide me.
Thanks
static int t[3][2][4] = { {2,4,3,6,
1,6,7,9,},
{8,2,1,1,
2,3,7,3,},
{1,6,2,4,
0,7,9,5,},
};
Please Help me...thanks!
If you need to go through all of the values inside the loop without manual handling (i.e. x = t[1][1][1] then x = t[1][1][2] etc) then you want to use loops, enhanced loops or iterators. However since you're using C the only of those three options available are standard loops, which you're trying not to use. So... there's mo straight forward way to do that really.
If you're willing to use some other C libraries however then there may be more options for you. Iterator libraries probably exist.
A non-straightforward way to do it (if you're looking for one) could be through recursion, however that's really quite wasteful. I advise you just use loops :P
What are you trying to prove with loops and without loops should be first thought.
If u want to access all the elements and not use loop is like writing a lot of code manually and waste of memory(in your case 3 * 2 * 4 no of lines instead of few ).
Instead of showing the array if you had put in your code how and where you accessing elements it would have been more clear to tell what you wanted .

Resources