Other Programming in statically and strongly typed languages



Reaction score: 56
Messages: 86

I have been a sysadmin for over 20 years, working on AIX, Solaris, and Linux in the enterprise which has included government (DoD, CMS) and 3 Fortune 400 companies, among others in online retail, financial services brokerage and online.

When I need to script something I prefer the Korn Shell and it is my first choice. I also use Rexx at times because of the parse command. Of course, awk and sed have their uses too. On a scale of 1-10, I'd consider myself a 8/9 in scripting, because there is always someone who knows more.

When it comes to dynamically and weakly typed languages such as Perl, Lua, Rexx, or whatever, I usually can pick them up fairly easily and do what I need quickly. However, when it comes to statically and strongly typed languages, it takes me longer. I can code in C, but it takes me longer because I'm always searching for something since I have a wrong cast, etc.

I'd like to become more proficient with C and Go. Does anyone have any tips or clues to why I seem to have more difficulty with statically and strongly typed languages, and how I can overcome it?



Reaction score: 616
Messages: 1,570

C in particular is a little awkward with text because it treats them as raw bytes pretty much. This is why C++ is popular with std::string and std::stringstream. Whereas shell scripting in particular treats most things as text (actually one of the main flexibilities of how UNIX "tools" work). So I imagine that there will be some slight changes you might need to make to your approach with C.

Some tips:
As you load in data, try to store it as the correct type as soon as possible. There is no point having i.e Age as a string variable anywhere.

If you do plan to work with strings a lot, then possibly make a few reusable helper functions to help conversion between i.e float and string easily.

Make the most of structures. In many cases you pass round groups of data (i.e age, name, year) and so long as they are put in that structure as a variable with the most logical type in the first place, I very rarely see places where you would need to convert them (other than to string for a console / debug output).

I understand the point of C rather than C++ but if you can be flexible, then give that a play. Unfortunately with C++ you get a lot of additional complexity along with the few valuable facilities so isn't always worth it.

You mentioned Lua as a language. I am quite fond of this but I try to approach it the other way round, and have a C program which runs Lua code (via an embedded Lua interpreter). That way you get a massive benefit of being able to integrate with native C libraries and the OS but also retain the power of string handling, safety, memory management that Lua provides. Only really worth it for larger projects though. For small scripts, C isn't really my first choice.

Nicola Mingotti

Well-Known Member

Reaction score: 183
Messages: 451

Hello pyret,

In my opinion, if you want to become more proficient with C you should focus on building something that, for a reason or another, actually requires C.

I was not writing C code for a long time, I used extensively R and Mathematica during phd, Java for Android programming, I sold a few programs in Python, created a few web app in Javascript/Node and for general purpose scripting now my love is Ruby.

Rephrasing kpedersen, C thinks in bytes. What you can easily do in e.g. Ruby with a bunch of lists, dictionaries and regexp becomes substatially more difficult in C because lists, hashs and regexp are not concept exmbedded in the C language, you need to use libraries and make your own structs to represent objects. In my opinion it is not static typing creating you the most difficulties, it is the lack of pre-packaged (very useful) abstraction layers.

If you want to improve your static typing programming maybe you should give Java a shot.

2 years ago I became interested in embedded, in particular Arduino and BeagleBone; for those things C is the master tool so I restarted to write some C code. And i had some fun.



Well-Known Member

Reaction score: 377
Messages: 486

I don't have any real tips or clues for you, but let me describe my own situation. Maybe this is somewhat insightful.

Until about 10 years ago, I wrote most things in either POSIX shell (actually the subset of POSIX shell supported by FreeBSD's /bin/sh) or in C. And then I decided to make myself more familiar with Python – and that changed everything. I already knew Python before (as I knew Perl, Ruby, Java and others), but hadn't done anything serious with it. But then I discovered it is actually the perfect language, at least for myself.

Python is dynamically and strongly typed. Because of its type system, friendly syntax, functional features (like list comprehensions) and extensive standard library, it is well suited for rapid prototyping. You get results very fast, much faster than when using C. Interfacing with C code is so simple, it's almost ridiculous – both embedding Python in a C program and calling C code from a Python program. For example, I have written a video processor framework in Python that handles 1080p MPEG4 video in realtime; only the core frame functions are written in C (this is like 2 % of the code). The first working version of the whole thing took me one weekend to write.

As of today, I write simple things in /bin/sh. Almost everything else I write in Python, and if there are any parts that are performance-critical, I write just those in C.