JiiPee <
n...@notvalid.com> writes:
> int i; for(i=0; i<size; ++i)
That looks even better this way:
for (int i = 0; i < size; i++)
++i (instead of i++) looks backward and it is
confusing as it doesn't serve a clear purpose (or
any?).
You can put the declaration of i within the for loop
if you pass either of -std=c99, -std=gnu99, -std=c11
or -std=gnu11 to gcc.
> can be done in C++: for(auto i : vec)
>
> clearly c++ version is shorter and more elegant
> here. Its also easier to see what happens in the
> looop.
No, I think the traditional for-loop is much more
clear and, well, "elegant" is perhaps not the word I'd
pick but OK. That new C++ version looks like Java and
it isn't self contained or explicitly typed.
> So its better readable. plus the assembly code is
> the same in both, so they are equally fast
You don't need to worry about speed with for-loops
unless you nest them in crazy ways, which you
shouldn't do. Don't worry about speed. C, and C++, are
fast enough. Worry about clarity and the
restricted-but-efficient use of modularization and the
use of proper data structures and algorithms (sounds
fancy, but really it comes down to what has proven to
work, which is equally down-to-earth). If you do the
small things right, speed will come automatically.
> Or would you say the C code here is better? In what
> way?
It is not about better or worse. The definition of a
good program is a program that does its task. Often,
the choice language is secondary. The most important
thing is the choice of programmer, and his
understanding of the task and the techno-setting that
the problem-solver (the program) will interact with.
Being fluent in the syntax of C and C++, as well as
the workings of an editor (e.g., Emacs, vim), is an
advantage because then less brain-power has to be
allocated to the process of writing code. Being fluent
with the C and C++ libraries is a big advantage
because of the above reason (again), but also because
those libraries are themselves interfaces to the
system and computer beneath. If you understand all
this, you can focus all your mental and physical
strength to the actual problem and only think about
that, never what to type, how to kill or yank, what
library to use, etc., but instead it'll be all about
the problem at hand. If you know all that and still
can't solve the problem, perhaps you should find
another profession/hobby :) But seriously that has
never happened - and how could it?
> in C (and lets imagine that the vector item type is
> very long, thus auto makes the type short)?
Don't be afraid of typing! Put your hands in the
correct position: asdf for the left, jkl; for the
right, master all the keystrokes of a professional
editor (like get it into your muscle memory), set the
keyboard layout to US (to get all the /[]{}; close and
tight, never use the mouse, and then type, type, and
type!
--
underground experts united